YOMEDIA
ADSENSE
Chương 2: Core C# Programming Construct
96
lượt xem 6
download
lượt xem 6
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
This chapter surveys the C# language syntax. I introduce you to the two fundamental kinds of types within the CLR: value types and reference types. This chapter also describes namespaces and how you can use them to logically partition types and functionality within your applications.
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Chương 2: Core C# Programming Construct
- Chapter 2. Core C# Programming Constructs Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology 1
- Objectives “This chapter surveys the C# language syntax. I introduce you to the two fundamental kinds of types within the CLR: value types and reference types. This chapter also describes namespaces and how you can use them to logically partition types and functionality within your applications.” Microsoft 2
- Roadmap 2.1. C# Is a strongly Typed Language 2.2. Expression 2.3. Statements and Expressions 2.4. Types and Variabless 2.5. NameSpaces 2.6. Control Flows Microsoft 3
- 2.1. C# Is a strongly Typed Language Every variable and object instance in the system is of a well-defined type This enables the compiler to check that the operations to perform on variables and object instance are valid It is always best to find bugs at compile time rather than run time Example: • Method ComputeAvg(): computes the average of two integers and returns the result Microsoft 4
- ComputeAvg accepts two integers and returns a double. If passing an instance of Apple type, the compiler will complain and stop double ComputeAvg( int param1, int param2 ) { return (param1 + param2) / 2.0; } object ComputeAvg( object param1, object param2 ) { return ((int) param1 + (int) param2) / 2.0; } object keyword: an alias of Convert objects into System.Object class integers Object is not a numeric type Passing an instance of Apple type causes an exception( the instance can’t be convert into integer Microsoft 5
- Roadmap 2.1. C# Is a strongly Typed Language 2.2. Expression 2.3. Statements and Expressions 2.4. Types and Variabless 2.5. NameSpaces 2.6. Control Flows Microsoft 6
- 2.2. Expressions Expressions in C# are identical to expressions in C++ and Java. Are built using operands, eg. variables or types within an application, and operators Operators can be overloaded Operators can have different meaning in different contexts • Eg: the + operator can mean string concatenation using with string operands C# operator precedence: • When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated • Entries at the top of the table have higher precedence • Operators within the same category have equal precedence. Microsoft 7
- Category Expression Description x.m Member access Primary x(...) Method and delegate invocation x[...] Array and indexer access x++ Post-increment x Post-decrement Object and delegate creation new T(...) new T(...){...} Object creation with initializer new {...} Anonymous object initializer new T[...] Array creation Obtain System.Type object for T typeof(T) checked(x) Evaluate expression in checked context unchecked(x) Evaluate expression in unchecked context Obtain default value of type T default(T) delegate {...} Anonymous function (anonymous method) Microsoft 8
- Category Expression Description +x Identity Unary x Negation !x Logical negation ~x Bitwise negation ++x Pre-increment x Pre-decrement (T)x Explicitly convert x to type T x * y Multiplication Multiplicative x / y Division x % y Remainder x + y Addition, string concatenation, delegate combination Additive x – y Subtraction, delegate removal Shift x y Shift right Microsoft 9
- Category Expression Description x y Greater than Less than or equal x = y Greater than or equal x is T Return true if x is a T, false otherwise x as T Return x typed as T, or null if x is not a T x == y Equality Equal x != y Not equal x & y Logical AND Integer bitwise AND, boolean logical AND Logical XOR Integer bitwise XOR, boolean logical XOR x ^ y Logical OR Integer bitwise OR, boolean logical OR x | y Microsoft 10
- Roadmap 2.1. C# Is a strongly Typed Language 2.2. Expression 2.3. Statements and Expressions 2.4. Types and Variabless 2.5. NameSpaces 2.6. Control Flows Microsoft 11
- 2.3 Statements and Expressions The actions of a program are expressed using statements Several different kinds of statements: A block: consists of a list of statements written between the delimiters { and } {x=69;y=96} Declaration statements: are used to declare local variables and constants int a; int b = 2, c = 3; Microsoft 12
- Statements and Expressions(2) Epression statements are used to evaluate expressions Console.WriteLine(“Goodbye”); Selection statements are used to select one of a number of possible statements for execution based on the value of some expression – if, switch if(continue == true) {x=69;} else {x=96;} Iteration statements are used to repeatedly execute an embedded statement – while, do, for, foreach while (i ++< Length) { Console.WriteLine(args[i]); } Microsoft 13
- Statements and Expressions(3) Jump statements are used to transfer control - break, continue, goto, throw, return, and yield 1. while (true) { 2. int n = Console.Read(); 3. if (n == 69) break; 4. Console.WriteLine(n); 5. } The try...catch statement is used to catch exceptions that occur during execution of a block, and the try...finally statement is used to specify finalization code that is always executed, whether an exception occurred or not Microsoft 14
- Statements and Expressions(4) The checked and unchecked statements are used to control the overflow checking context for integral-type arithmetic operations and conversions The lock statement is used to obtain the mutual-exclusion lock for a given object, execute a statement, and then release the lock The using statement is used to obtain a resource, execute a statement, and then dispose of that resource Microsoft 15
- Roadmap 2.1. C# Is a strongly Typed Language 2.2. Expression 2.3. Statements and Expressions 2.4. Types and Variabless 2.5. NameSpaces 2.6. Control Flows Microsoft 16
- Value Types and Reference Types Value types: • Living places: On the stack On the heap: only if they are members of reference types or if they are boxed • Are copied by value by default when passed as parameters to methods or assigned to other variables Reference types: • Living place: on the heap • Variables used to manipulate them are references to objects on the managed heap Microsoft 17
- Value Types Contain directly their value and are customarily created statically Initialization: using the new statement Derive from System.ValueType Primitives: int, float, char and bool Others: enum, struct Microsoft 18
- Reference Types The lifetime of the resulting object is controlled be garbage collection services provided by CLR The reference holds the location of an object created on the managed heap Derive from System.Object and created with the new keyword Types: interfaces, arrays and delegates Microsoft 19
- Example: Value Type int i = 123; string s = "Hello world"; i 123 s "Hello world" Reference Type Microsoft 20
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn