Hoang Anh Viet [email protected] Hà Nội University of Technology Chapter 1. Introduction to C#...

31
Hoang Anh Viet [email protected] Hà Nội University of Technology Chapter 1. Introduction to C# Programming

Transcript of Hoang Anh Viet [email protected] Hà Nội University of Technology Chapter 1. Introduction to C#...

Page 1: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Hoang Anh [email protected]

Hà Nội University of Technology

Chapter 1. Introduction to C# Programming

Page 2: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Objectives

“This chapter gives a quick glimpse of what a simple C# application looks like, and it describes some basic differences between the C# programming environment and the native C++ environment.”

Page 3: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Roadmap

1.1.Differences between C# and C++

1.2. Example of a C# program

1.3. Overview of Features Added in C# 2.0

1.4. Overview of What’s new in C# 3.0

Page 4: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Language

C#: Is a completely an Object-Oriented Language Every program is class Every work is done through objects

C++: Remains some features of procedural language

Example: free functions

Page 5: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Compiling

C#: C# source code compiles into managed code, an intermediate language(IL) At runtime, the Common Language Runtime (CLR) compiles the code by using

Just In Time(JIT) compiling The JIT compiler compiles a function or method only the first time and it

produces machine code native to the platform on which it’s running Pros:

The working set of the application is reduced( the memory footprint of intermediate code is smaller

The CLR can optimize the program’s execution on the fly at run time C++:

C++ code compiles into native code( the machine code that’s native to the processor)

Page 6: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Garbage Collection

C#: One of the key facilities in the CLR is the garbage collector GC automatically handles memory allocation and deallocation

C++: Not support Programmers have to handle memory explicitly

Page 7: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Programming

Generally, C# language is similar to C++ because it is developed from C++ and Java. However, it’s added many new features allowing programmers to program easier and friendlier.

Example:

• Statement: foreach

• Properties: set and get method

Page 8: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Roadmap

1.1.Differences between C# and C++

1.2. Example of a C# program

1.3. Overview of Features Added in C# 2.0

1.4. Overview of What’s new in C# 3.0

Page 9: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

1.2 Example of a C# Program

1. // Welcome.cs 2. // A first console program in C#. 3. using System; 4. class Welcome 5. { 6. static void Main( string[] args ) 7. { 8. Console.WriteLine( "Welcome to C#

Programming!" ); 9. } 10. }

9

Each application must have exactly one

Call a method like C++

Page 10: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Roadmap

1.1.Differences between C# and C++

1.2. Example of a C# program

1.3. Overview of Features Added in C# 2.0

1.4. Overview of What’s new in C# 3.0

Page 11: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

1.3. Overview of Features Added in C# 2.0

Generics

Iterators

Partial types

Anonymous method

Page 12: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Generics Generics?

Similar Templates in C++ Type checking, no boxing, no downcasts Increased sharing (typed collections)

How are C# generics implemented? Instantiated at run-time, not compile-time Checked at declaration, not instantiation Work for both reference and value types Exact run-time type information

Page 13: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Generics (2)

Collection classes

Collection interfaces

Collection base classes

Utility classes

Reflection

List<T>Dictionary<K,V>SortedDictionary<K,V>Stack<T>Queue<T>

IList<T>IDictionary<K,V>ICollection<T>IEnumerable<T>IEnumerator<T>IComparable<T>IComparer<T>

Collection<T>KeyedCollection<T>ReadOnlyCollection<T>

Nullable<T>EventHandler<T>Comparer<T>

Page 14: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Iterators

C# provides interfaces IEnumerable<T> that abstract the ability to enumerate a collection

C# 2.0 introduces iterators, easing task of implementing IEnumerable e.g.

We can use the foreach construct:

New keyword yield

static IEnumerable<int> UpAndDown(int bottom, int top) { for (int i = bottom; i < top; i++) { yield return i; } for (int j = top; j >= bottom; j--) { yield return j; } }

foreach (int x in SomeList) { Console.WriteLine(x); }

Page 15: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Partial Types

New keyword partial Separate the definition of a class, a struct, an interface

over two or more source files//first file (MyClass_1.cs)public partial class MyClass{

private int nCount;. . . . .

}

//second file (MyClass_2.cs)public partial class MyClass{

private bool isPresent. . . . .

}

Page 16: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Anonymous methods

Delegates are clumsy: programmer has to name the function and “closure-convert” by hand

So C# 2.0 introduced anonymous methods No name Compiler does closure-conversion, creating a class

and object that captures the environment e.g.

bool b = xs.Exists(delegate(int x) { return x>y; });

Local y is free in body of anonymous method

Page 17: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Other New Features

Static classes Property accessibility control External aliases Namespace alias qualifiers Inline warning control Fixed size buffers

Page 18: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Roadmap

1.1.Differences between C# and C++

1.2. Example of a C# program

1.3. Overview of Features Added in C# 2.0

1.4. Overview of What’s new in C# 3.0

Page 19: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

1.4. Overview of What’s new in C# 3.0 Implicitly Typed Local Variables Object and Collection Initializers Extension Methods Partial Methods Anonymous Types Query Keywords Lambda Expressions

Page 20: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Implicitly Typed Local Variables

Use the new var keyword to implicitly declare a variable Useful in cases where you do not know the exact type of

data and you need the compiler to determine for you Examples

var i = 5;var s = "Hello";

Are equivalent to:int i = 5;string s = "Hello";

20

Page 21: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Object and Collection Initializers

Enables you to combine declaration and initialization one object in one stepEx: class A

public class A { public int x ; public string y; }

Then could declare and initialize an A object:

var myA = new A{ x = 0, y= “some text”} ;

Collection Initializer is similarList<string> animals = new List<string>();

animals.Add("monkey"); fg

animals.Add("donkey");

animals.Add("cow"); Can replace by:var animals = new List<string> {"monkey", "donkey", "cow”} ;

Page 22: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Extension Methods

Enable you to extend various types with additional static methods

Can be declared only in static classes and are identified by the keyword "this“

This allows you to take advantage of the extensible nature of various built-in or defined types and add newer methods to them

22

Page 23: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Anonymous Types

Create an instance of a class without having to write code for the class beforehand

Example:

var A = new {x=9,y=“hello”}

A has two properties: x=9 and y=“hello”

23

Page 24: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Lambda Expressions

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

Implicitly or explicitly typed parameters

24

Page 25: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Lambda Expressions(2)

Examples:x => x + 1 // Implicitly typed, expression bodyx => { return x + 1; } // Implicitly typed, statement body(int x) => x + 1 // Explicitly typed, expression body(int x) => { return x + 1; } // Explicitly typed, statement body(x, y) => x * y // Multiple parameters() => Console.WriteLine() // No parameters

25

Page 26: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Query Expressions

26

Language-Integrated Query (LINQ) A part of C# 3.0 language Allows you to write SQL-like syntax in C#

Page 27: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Query Keywords

27

Clause Description

from Specifies a data source and a range variable (similar to an iteration variable)

where Filters source elements based on one or more Boolean expressions separated by logical AND and OR operators ( && or || )

select Specifies the type and shape that the elements in the returned sequence will have when the query is executed

group Groups query results according to a specified key value

Page 28: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Query Keywords(2)

28

Clause Description

into Provides an identifier that can serve as a reference to the results of a join, group or select clause

orderby Sorts query results in ascending or descending order based on the default comparer for the element type

join Joins two data sources based on an equality comparison between two specified matching criteria

let Introduces a range variable to store sub-expression results in a query expression

Page 29: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Expression Trees

New type: System.Expressions.Expression<T> Simply an in-memory representation of a lambda

expression Allows expressions to be treated as data at runtime Can modify and inspect lambda expressions at runtime Example:

Expression<DemoDelegate> filter = () => Console.WriteLine("Hello!!");

29

You easily can inspect the contents of the tree by using various properties on the filter variable

Page 30: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Partial Method Definitions

Partial types may now contain partial methods Partial methods must adhere to the following rules:

They must begin with the partial keyword and the method must return void. They can have ref parameters but not out parameters. They are implicitly private and, therefore, cannot be virtual. They cannot be extern, because the presence of the body determines whether

they are defining or implementing. They can have static and unsafe modifiers. They can be generic; constraints are put on the defining partial method

declaration, and may optionally be repeated on the implementing declaration. Parameter and type parameter names do not have to be the same in the

implementing declaration as in the defining declaration. They cannot make a delegate to a partial method.

30

Page 31: Hoang Anh Viet VietHa@it-hut.edu.vn Hà Nội University of Technology Chapter 1. Introduction to C# Programming.

Microsoft

Summary

Differences Between C# and C++ Sytax, Framework,…

C# 2.0 Enhancements Generics, Iterators, Partial types, Anonymous method And much more…

New in C# 3.0 Implicitly Typed Local Variables, Object and Collection

Initializers, Extension Methods, Anonymous Types, Lambda Expressions, Query Keywords, Expression Trees, Partial Method Definitions ,…

31