Table of Contents
1. Introduction.......................................................................................................................................... 1
1.1 Getting started..................................................................................................................................... 1
1.2 Types................................................................................................................................................. 2
1.2.1 Predefined types........................................................................................................................... 4
1.2.2 Conversions.................................................................................................................................. 6
1.2.3 Array types.................................................................................................................................. 6
1.2.4 Type system unification................................................................................................................. 8
1.3 Variables and parameters.................................................................................................................... 9
1.4 Automatic memory management........................................................................................................ 12
1.5 Expressions....................................................................................................................................... 14
1.6 Statements........................................................................................................................................ 15
1.7 Classes............................................................................................................................................. 18
1.7.1 Constants.................................................................................................................................... 20
1.7.2 Fields.......................................................................................................................................... 20
1.7.3 Methods..................................................................................................................................... 21
1.7.4 Properties................................................................................................................................... 22
1.7.5 Events........................................................................................................................................ 23
1.7.6 Operators................................................................................................................................... 24
1.7.7 Indexers..................................................................................................................................... 25
1.7.8 Instance constructors................................................................................................................... 26
1.7.9 Destructors................................................................................................................................. 27
1.7.10 Static constructors..................................................................................................................... 28
1.7.11 Inheritance................................................................................................................................ 28
1.8 Structs.............................................................................................................................................. 29
1.9 Interfaces......................................................................................................................................... 30
1.10 Delegates........................................................................................................................................ 31
1.11 Enums............................................................................................................................................. 32
1.12 Namespaces and assemblies............................................................................................................. 33
1.13 Versioning....................................................................................................................................... 35
1.14 Attributes........................................................................................................................................ 37
2. Lexical structure................................................................................................................................. 39
2.1 Programs.......................................................................................................................................... 39
2.2 Grammars......................................................................................................................................... 39
2.2.1 Grammar notation........................................................................................................................ 39
2.2.2 Lexical grammar......................................................................................................................... 40
2.2.3 Syntactic grammar...................................................................................................................... 40
2.3 Lexical analysis................................................................................................................................. 40
2.3.1 Line terminators.......................................................................................................................... 41
2.3.2 White space................................................................................................................................ 41
2.3.3 Comments.................................................................................................................................. 42
2.4 Tokens.............................................................................................................................................. 43
2.4.1 Unicode character escape sequences........................................................................................... 43
2.4.2 Identifiers................................................................................................................................... 44
2.4.3 Keywords................................................................................................................................... 45
2.4.4 Literals....................................................................................................................................... 46
2.4.4.1 Boolean literals...................................................................................................................... 46
2.4.4.2 Integer literals....................................................................................................................... 46
2.4.4.3 Real literals........................................................................................................................... 47
2.4.4.4 Character literals................................................................................................................... 48
2.4.4.5 String literals......................................................................................................................... 49
2.4.4.6 The null literal....................................................................................................................... 51
2.4.5 Operators and punctuators........................................................................................................... 51
2.5 Pre-processing directives................................................................................................................... 51
2.5.1 Conditional compilation symbols.................................................................................................... 52
2.5.2 Pre-processing expressions.......................................................................................................... 53
2.5.3 Declaration directives.................................................................................................................. 53
2.5.4 Conditional compilation directives................................................................................................. 54
2.5.5 Line directives............................................................................................................................. 56
2.5.6 Diagnostic directives.................................................................................................................... 57
2.5.7 Region directives......................................................................................................................... 57
3. Basic concepts.................................................................................................................................... 59
3.1 Application Startup............................................................................................................................ 59
3.2 Application termination....................................................................................................................... 60
3.3 Declarations...................................................................................................................................... 60
3.4 Members.......................................................................................................................................... 62
3.4.1 Namespace members.................................................................................................................. 62
3.4.2 Struct members........................................................................................................................... 63
3.4.3 Enumeration members................................................................................................................. 63
3.4.4 Class members............................................................................................................................ 63
3.4.5 Interface members...................................................................................................................... 63
3.4.6 Array members........................................................................................................................... 64
3.4.7 Delegate members...................................................................................................................... 64
3.5 Member access................................................................................................................................. 64
3.5.1 Declared accessibility.................................................................................................................. 64
3.5.2 Accessibility domains................................................................................................................... 65
3.5.3 Protected access for instance members........................................................................................ 67
3.5.4 Accessibility constraints............................................................................................................... 68
3.6 Signatures and overloading................................................................................................................. 68
3.7 Scopes.............................................................................................................................................. 69
3.7.1 Name hiding................................................................................................................................ 71
3.7.1.1 Hiding through nesting........................................................................................................... 72
3.7.1.2 Hiding through inheritance...................................................................................................... 72
3.8 Namespace and type names............................................................................................................... 74
3.8.1 Fully qualified names................................................................................................................... 75
3.9 Automatic memory management........................................................................................................ 75
4. Types................................................................................................................................................... 79
4.1 Value types....................................................................................................................................... 79
4.1.1 Default constructors.................................................................................................................... 80
4.1.2 Struct types................................................................................................................................. 81
4.1.3 Simple types................................................................................................................................ 81
4.1.4 Integral types.............................................................................................................................. 82
4.1.5 Floating point types...................................................................................................................... 83
4.1.6 The decimal type......................................................................................................................... 84
4.1.7 The bool type.............................................................................................................................. 85
4.1.8 Enumeration types....................................................................................................................... 85
4.2 Reference types................................................................................................................................ 85
4.2.1 Class types................................................................................................................................. 86
4.2.2 The object type........................................................................................................................... 86
4.2.3 The string type............................................................................................................................ 86
4.2.4 Interface types............................................................................................................................ 86
4.2.5 Array types................................................................................................................................. 87
4.2.6 Delegate types............................................................................................................................ 87
4.3 Boxing and unboxing.......................................................................................................................... 87
4.3.1 Boxing conversions...................................................................................................................... 87
4.3.2 Unboxing conversions.................................................................................................................. 88
5. Variables............................................................................................................................................. 89
5.1 Variable categories............................................................................................................................ 89
5.1.1 Static variables............................................................................................................................ 89
5.1.2 Instance variables........................................................................................................................ 89
5.1.2.1 Instance variables in classes................................................................................................... 90
5.1.2.2 Instance variables in structs................................................................................................... 90
5.1.3 Array elements........................................................................................................................... 90
5.1.4 Value parameters........................................................................................................................ 90
5.1.5 Reference parameters................................................................................................................. 90
5.1.6 Output parameters....................................................................................................................... 90
5.1.7 Local variables............................................................................................................................ 91
5.2 Default values................................................................................................................................... 91
5.3 Definite assignment........................................................................................................................... 92
5.3.1 Initially assigned variables............................................................................................................ 94
5.3.2 Initially unassigned variables......................................................................................................... 94
5.4 Variable references........................................................................................................................... 94
6. Conversions........................................................................................................................................ 95
6.1 Implicit conversions........................................................................................................................... 95
6.1.1 Identity conversion...................................................................................................................... 95
6.1.2 Implicit numeric conversions........................................................................................................ 95
6.1.3 Implicit enumeration conversions.................................................................................................. 96
6.1.4 Implicit reference conversions...................................................................................................... 96
6.1.5 Boxing conversions...................................................................................................................... 96
6.1.6 Implicit constant expression conversions....................................................................................... 97
6.1.7 User-defined implicit conversions................................................................................................. 97
6.2 Explicit conversions........................................................................................................................... 97
6.2.1 Explicit numeric conversions........................................................................................................ 97
6.2.2 Explicit enumeration conversions.................................................................................................. 98
6.2.3 Explicit reference conversions...................................................................................................... 99
6.2.4 Unboxing conversions.................................................................................................................. 99
6.2.5 User-defined explicit conversions................................................................................................ 100
6.3 Standard conversions....................................................................................................................... 100
6.3.1 Standard implicit conversions...................................................................................................... 100
6.3.2 Standard explicit conversions...................................................................................................... 100
6.4 User-defined conversions................................................................................................................. 100
6.4.1 Permitted user-defined conversions............................................................................................. 100
6.4.2 Evaluation of user-defined conversions........................................................................................ 100
6.4.3 User-defined implicit conversions................................................................................................ 101
6.4.4 User-defined explicit conversions................................................................................................ 102
7. Expressions...................................................................................................................................... 105
7.1 Expression classifications................................................................................................................. 105
7.1.1 Values of expressions................................................................................................................ 106
7.2 Operators........................................................................................................................................ 106
7.2.1 Operator precedence and associativity........................................................................................ 106
7.2.2 Operator overloading................................................................................................................. 107
7.2.3 Unary operator overload resolution............................................................................................. 108
7.2.4 Binary operator overload resolution............................................................................................. 109
7.2.5 Candidate user-defined operators................................................................................................ 109
7.2.6 Numeric promotions................................................................................................................... 109
7.2.6.1 Unary numeric promotions................................................................................................... 110
7.2.6.2 Binary numeric promotions................................................................................................... 110
7.3 Member lookup................................................................................................................................ 111
7.3.1 Base types................................................................................................................................ 111
7.4 Function members............................................................................................................................ 112
7.4.1 Argument lists........................................................................................................................... 112
7.4.2 Overload resolution.................................................................................................................... 112
7.4.2.1 Applicable function member................................................................................................. 112
7.4.2.2 Better function member....................................................................................................... 112
7.4.2.3 Better conversion................................................................................................................ 112
7.4.3 Function member invocation....................................................................................................... 112
7.4.3.1 Invocations on boxed instances............................................................................................. 112
7.4.4 Virtual function member lookup.................................................................................................. 112
7.4.5 Interface function member lookup............................................................................................... 112
7.5 Primary expressions......................................................................................................................... 112
7.5.1 Literals..................................................................................................................................... 112
7.5.2 Simple names............................................................................................................................ 112
7.5.2.1 Invariant meaning in blocks.................................................................................................. 112
7.5.3 Parenthesized expressions.......................................................................................................... 112
7.5.4 Member access......................................................................................................................... 112
7.5.4.1 Identical simple names and type names................................................................................. 112
7.5.5 Invocation expressions............................................................................................................... 112
7.5.5.1 Method invocations.............................................................................................................. 112
7.5.5.2 Delegate invocations............................................................................................................ 112
7.5.6 Element access......................................................................................................................... 112
7.5.6.1 Array access....................................................................................................................... 112
7.5.6.2 Indexer access.................................................................................................................... 112
7.5.7 This access............................................................................................................................... 112
7.5.8 Base access.............................................................................................................................. 112
7.5.9 Postfix increment and decrement operators................................................................................. 112
7.5.10 new operator........................................................................................................................... 112
7.5.10.1 Object creation expressions................................................................................................ 112
7.5.10.2 Array creation expressions................................................................................................. 112
7.5.10.3 Delegate creation expressions............................................................................................. 112
7.5.11 The typeof operator................................................................................................................. 112
7.5.12 The checked and unchecked operators...................................................................................... 112
Unary operators.................................................................................................................................... 112
7.6.1 Unary plus operator................................................................................................................... 112
7.6.2 Unary minus operator................................................................................................................ 112
7.6.3 Logical negation operator........................................................................................................... 112
7.6.4 Bitwise complement operator..................................................................................................... 112
7.6.5 Prefix increment and decrement operators.................................................................................. 112
7.6.6 Cast expressions........................................................................................................................ 112
7.7 Arithmetic operators........................................................................................................................ 112
7.7.1 Multiplication operator................................................................................................................ 112
7.7.2 Division operator....................................................................................................................... 112
7.7.3 Remainder operator................................................................................................................... 112
7.7.4 Addition operator....................................................................................................................... 112
7.7.5 Subtraction operator................................................................................................................... 112
7.8 Shift operators................................................................................................................................. 112
7.9 Relational and type testing operators................................................................................................. 112
7.9.1 Integer comparison operators..................................................................................................... 112
7.9.2 Floating-point comparison operators............................................................................................ 112
7.9.3 Decimal comparison operators.................................................................................................... 112
7.9.4 Boolean equality operators......................................................................................................... 112
7.9.5 Enumeration comparison operators............................................................................................. 112
7.9.6 Reference type equality operators............................................................................................... 112
7.9.7 String equality operators............................................................................................................. 112
7.9.8 Delegate equality operators........................................................................................................ 112
7.9.9 The is operator.......................................................................................................................... 112
7.9.10 The as operator....................................................................................................................... 112
7.10 Logical operators........................................................................................................................... 112
7.10.1 Integer logical operators........................................................................................................... 112
7.10.2 Enumeration logical operators................................................................................................... 112
7.10.3 Boolean logical operators.......................................................................................................... 112
7.11 Conditional logical operators............................................................................................................ 112
7.11.1 Boolean conditional logical operators......................................................................................... 112
7.11.2 User-defined conditional logical operators.................................................................................. 112
7.12 Conditional operator....................................................................................................................... 112
7.13 Assignment operators..................................................................................................................... 112
7.13.1 Simple assignment.................................................................................................................... 112
7.13.2 Compound assignment.............................................................................................................. 112
7.13.3 Event assignment..................................................................................................................... 112
7.14 Expression..................................................................................................................................... 112
7.15 Constant expressions...................................................................................................................... 112
7.16 Boolean expressions....................................................................................................................... 112
8. Statements........................................................................................................................................ 112
8.1 End points and reachability............................................................................................................... 112
8.2 Blocks............................................................................................................................................. 112
8.2.1 Statement lists........................................................................................................................... 112
8.3 The empty statement........................................................................................................................ 112
8.4 Labeled statements.......................................................................................................................... 112
8.5 Declaration statements..................................................................................................................... 112
8.5.1 Local variable declarations......................................................................................................... 112
8.5.2 Local constant declarations........................................................................................................ 112
8.6 Expression statements...................................................................................................................... 112
8.7 Selection statements......................................................................................................................... 112
8.7.1 The if statement........................................................................................................................ 112
8.7.2 The switch statement................................................................................................................. 112
8.8 Iteration statements.......................................................................................................................... 112
8.8.1 The while statement................................................................................................................... 112
8.8.2 The do statement....................................................................................................................... 112
8.8.3 The for statement...................................................................................................................... 112
8.8.4 The foreach statement............................................................................................................... 112
8.9 Jump statements.............................................................................................................................. 112
8.9.1 The break statement.................................................................................................................. 112
8.9.2 The continue statement.............................................................................................................. 112
8.9.3 The goto statement.................................................................................................................... 112
8.9.4 The return statement.................................................................................................................. 112
8.9.5 The throw statement.................................................................................................................. 112
8.10 The try statement........................................................................................................................... 112
8.11 The checked and unchecked statements.......................................................................................... 112
8.12 The lock statement......................................................................................................................... 112
8.13 The using statement....................................................................................................................... 112
9. Namespaces...................................................................................................................................... 112
9.1 Compilation units.............................................................................................................................. 112
9.2 Namespace declarations................................................................................................................... 112
9.3 Using directives............................................................................................................................... 112
9.3.1 Using alias directives................................................................................................................. 112
9.3.2 Using namespace directives....................................................................................................... 112
9.4 Namespace members....................................................................................................................... 112
9.5 Type declarations............................................................................................................................. 112
10. Classes............................................................................................................................................ 112
10.1 Class declarations.......................................................................................................................... 112
10.1.1 Class modifiers........................................................................................................................ 112
10.1.1.1 Abstract classes................................................................................................................ 112
10.1.1.2 Sealed classes................................................................................................................... 112
10.1.2 Class base specification............................................................................................................ 112
10.1.2.1 Base classes...................................................................................................................... 112
10.1.2.2 Interface implementations................................................................................................... 112
10.1.3 Class body............................................................................................................................... 112
10.2 Class members.............................................................................................................................. 112
10.2.1 Inheritance.............................................................................................................................. 112
10.2.2 The new modifier..................................................................................................................... 112
10.2.3 Access modifiers..................................................................................................................... 112
10.2.4 Constituent types..................................................................................................................... 112
10.2.5 Static and instance members..................................................................................................... 112
10.2.6 Nested types........................................................................................................................... 112
10.2.6.1 Fully qualified name........................................................................................................... 112
10.2.6.2 Declared accessibility......................................................................................................... 112
10.2.6.3 Hiding............................................................................................................................... 112
10.2.6.4 this access......................................................................................................................... 112
10.2.6.5 Access to private and protected members of the containing type........................................... 112
10.2.7 Reserved member names......................................................................................................... 112
10.2.7.1 Member Names Reserved for Properties............................................................................ 112
10.2.7.2 Member Names Reserved for Events................................................................................. 112
10.2.7.3 Member Names Reserved for Indexers............................................................................... 112
10.2.7.4 Member Names Reserved for Destructors.......................................................................... 112
10.3 Constants...................................................................................................................................... 112
10.4 Fields............................................................................................................................................ 112
10.4.1 Static and instance fields.......................................................................................................... 112
10.4.2 Readonly fields........................................................................................................................ 112
10.4.2.1 Using static readonly fields for constants............................................................................. 112
10.4.2.2 Versioning of constants and static readonly fields................................................................. 112
10.4.3 Volatile fields........................................................................................................................... 112
10.4.4 Field initialization...................................................................................................................... 112
10.4.5 Variable initializers................................................................................................................... 112
10.4.5.1 Static field initialization....................................................................................................... 112
10.4.5.2 Instance field initialization................................................................................................... 112
10.5 Methods........................................................................................................................................ 112
10.5.1 Method parameters.................................................................................................................. 112
10.5.1.1 Value parameters.............................................................................................................. 112
10.5.1.2 Reference parameters........................................................................................................ 112
10.5.1.3 Output parameters............................................................................................................. 112
10.5.1.4 Parameter arrays............................................................................................................... 112
10.5.2 Static and instance methods...................................................................................................... 112
10.5.3 Virtual methods....................................................................................................................... 112
10.5.4 Override methods.................................................................................................................... 112
10.5.5 Sealed methods........................................................................................................................ 112
10.5.6 Abstract methods..................................................................................................................... 112
10.5.7 External methods..................................................................................................................... 112
10.5.8 Method body........................................................................................................................... 112
10.5.9 Method overloading.................................................................................................................. 112
10.6 Properties...................................................................................................................................... 112
10.6.1 Static and instance properties.................................................................................................... 112
10.6.2 Accessors............................................................................................................................... 112
10.6.3 Virtual, sealed, override, and abstract accessors......................................................................... 112
10.7 Events........................................................................................................................................... 112
10.7.1 Event accessors....................................................................................................................... 112
10.7.2 Static and instance events......................................................................................................... 112
10.7.3 Virtual, sealed, override, and abstract accessors......................................................................... 112
10.8 Indexers........................................................................................................................................ 112
10.8.1 Indexer overloading.................................................................................................................. 112
10.9 Operators...................................................................................................................................... 112
10.9.1 Unary operators....................................................................................................................... 112
10.9.2 Binary operators...................................................................................................................... 112
10.9.3 Conversion operators............................................................................................................... 112
10.10 Instance constructors.................................................................................................................... 112
10.10.1 Constructor initializers............................................................................................................ 112
10.10.2 Instance variable initializers..................................................................................................... 112
10.10.3 Constructor execution............................................................................................................. 112
10.10.4 Default constructors............................................................................................................... 112
10.10.5 Private constructors............................................................................................................... 112
10.10.6 Optional instance constructor parameters................................................................................. 112
10.11 Static constructors........................................................................................................................ 112
10.12 Destructors.................................................................................................................................. 112
11. Structs............................................................................................................................................. 112
11.1 Struct declarations.......................................................................................................................... 112
11.1.1 Struct modifiers....................................................................................................................... 112
11.1.2 Struct interfaces...................................................................................................................... 112
11.1.3 Struct body.............................................................................................................................. 112
11.2 Struct members.............................................................................................................................. 112
11.3 Class and struct differences............................................................................................................ 112
11.3.1 Value semantics...................................................................................................................... 112
11.3.2 Inheritance.............................................................................................................................. 112
11.3.3 Assignment............................................................................................................................. 112
11.3.4 Default values......................................................................................................................... 112
11.3.5 Boxing and unboxing................................................................................................................ 112
11.3.6 Meaning of this........................................................................................................................ 112
11.3.7 Field initializers........................................................................................................................ 112
11.3.8 Constructors............................................................................................................................ 112
11.3.9 Destructors............................................................................................................................. 112
11.4 Struct examples............................................................................................................................. 112
11.4.1 Database integer type.............................................................................................................. 112
11.4.2 Database boolean type............................................................................................................. 112
12. Arrays.............................................................................................................................................. 112
12.1 Array types................................................................................................................................... 112
12.1.1 The System.Array type............................................................................................................ 112
12.2 Array creation............................................................................................................................... 112
12.3 Array element access..................................................................................................................... 112
12.4 Array members.............................................................................................................................. 112
12.5 Array covariance........................................................................................................................... 112
12.6 Array initializers............................................................................................................................. 112
13. Interfaces........................................................................................................................................ 112
13.1 Interface declarations..................................................................................................................... 112
13.1.1 Interface modifiers................................................................................................................... 112
13.1.2 Base interfaces........................................................................................................................ 112
13.1.3 Interface body......................................................................................................................... 112
13.2 Interface members......................................................................................................................... 112
13.2.1 Interface methods.................................................................................................................... 112
13.2.2 Interface properties.................................................................................................................. 112
13.2.3 Interface events....................................................................................................................... 112
13.2.4 Interface indexers.................................................................................................................... 112
13.2.5 Interface member access......................................................................................................... 112
13.3 Fully qualified interface member names........................................................................................... 112
13.4 Interface implementations............................................................................................................... 112
13.4.1 Explicit interface member implementations................................................................................ 112
13.4.2 Interface mapping.................................................................................................................... 112
13.4.3 Interface implementation inheritance......................................................................................... 112
13.4.4 Interface re-implementation...................................................................................................... 112
13.4.5 Abstract classes and interfaces................................................................................................. 112
14. Enums............................................................................................................................................. 112
14.1 Enum declarations.......................................................................................................................... 112
14.2 Enum modifiers.............................................................................................................................. 112
14.3 Enum members.............................................................................................................................. 112
14.4 Enum values and operations............................................................................................................ 112
15. Delegates........................................................................................................................................ 112
15.1 Delegate declarations..................................................................................................................... 112
15.2 Delegate instantiation..................................................................................................................... 112
15.3 Delegate invocation........................................................................................................................ 112
16. Exceptions...................................................................................................................................... 112
16.1 Causes of exceptions...................................................................................................................... 112
16.2 The System.Exception class............................................................................................................ 112
16.3 How exceptions are handled........................................................................................................... 112
16.4 Common Exception Classes............................................................................................................ 112
17. Attributes........................................................................................................................................ 112
17.1 Attribute classes............................................................................................................................ 112
17.1.1 Attribute usage........................................................................................................................ 112
17.1.2 Positional and named parameters.............................................................................................. 112
17.1.3 Attribute parameter types......................................................................................................... 112
17.2 Attribute specification..................................................................................................................... 112
17.3 Attribute instances......................................................................................................................... 112
17.3.1 Compilation of an attribute........................................................................................................ 112
17.3.2 Run-time retrieval of an attribute instance.................................................................................. 112
17.4 Reserved attributes........................................................................................................................ 112
17.4.1 The AttributeUsage attribute.................................................................................................... 112
17.4.2 The Conditional attribute........................................................................................................... 112
17.4.3 The Obsolete attribute.............................................................................................................. 112
A. Unsafe code...................................................................................................................................... 112
A.1 Unsafe contexts.............................................................................................................................. 112
A.2 Pointer types.................................................................................................................................. 112
A.3 Fixed and moveable variables.......................................................................................................... 112
A.4 Pointer conversions......................................................................................................................... 112
A.5 Pointers in expressions.................................................................................................................... 112
A.5.1 Pointer indirection..................................................................................................................... 112
A.5.2 Pointer member access............................................................................................................. 112
A.5.3 Pointer element access............................................................................................................. 112
A.5.4 The address-of operator............................................................................................................ 112
A.5.5 Pointer increment and decrement............................................................................................... 112
A.5.6 Pointer arithmetic..................................................................................................................... 112
A.5.7 Pointer comparison................................................................................................................... 112
A.5.8 The sizeof operator................................................................................................................... 112
A.6 The fixed statement........................................................................................................................ 112
A.7 Stack allocation............................................................................................................................... 112
A.8 Dynamic memory allocation............................................................................................................. 112
B. Interoperability................................................................................................................................ 112
B.1 The ComAliasName attribute........................................................................................................... 112
B.2 The ComImport attribute................................................................................................................. 112
B.3 The ComRegisterFunction attribute.................................................................................................. 112
B.4 The ComSourceInterfaces attribute.................................................................................................. 112
B.5 The ComUnregisterFunction attribute............................................................................................... 112
B.6 The ComVisible attribute................................................................................................................. 112
B.7 The DispId attribute........................................................................................................................ 112
B.8 The DllImport attribute.................................................................................................................... 112
B.9 The FieldOffset attribute.................................................................................................................. 112
B.10 The Guid attribute.......................................................................................................................... 112
B.11 The HasDefaultInterface attribute.................................................................................................. 112
B.12 The ImportedFromTypeLib attribute............................................................................................... 112
B.13 The In and Out attributes............................................................................................................... 112
B.14 The IndexerName attribute............................................................................................................ 112
B.15 The InterfaceType attribute........................................................................................................... 112
B.16 The MarshalAs attribute................................................................................................................ 112
B.17 The NoIDispatch attribute.............................................................................................................. 112
B.18 The PreserveSig attribute............................................................................................................... 112
B.19 The StructLayout attribute............................................................................................................. 112
B.20 The TypeLibFunc attribute............................................................................................................. 112
B.21 The TypeLibType attribute............................................................................................................. 112
B.22 The TypeLibVar attribute.............................................................................................................. 112
B.23 Supporting enums.......................................................................................................................... 112
C. Grammar........................................................................................................................................... 112
C.1 Lexical grammar............................................................................................................................. 112
C.1.1 Line terminators........................................................................................................................ 112
C.1.2 White space............................................................................................................................. 112
C.1.3 Comments................................................................................................................................ 112
C.1.4 Tokens..................................................................................................................................... 112
C.1.5 Unicode character escape sequences......................................................................................... 112
C.1.6 Identifiers................................................................................................................................. 112
C.1.7 Keywords................................................................................................................................ 112
C.1.8 Literals..................................................................................................................................... 112
C.1.9 Operators and punctuators......................................................................................................... 112
C.1.10 Pre-processing directives......................................................................................................... 112
C.2 Syntactic grammar.......................................................................................................................... 112
C.2.1 Basic concepts......................................................................................................................... 112
C.2.2 Types....................................................................................................................................... 112
C.2.3 Variables.................................................................................................................................. 112
C.2.4 Expressions.............................................................................................................................. 112
C.2.5 Statements............................................................................................................................... 112
17.4.4 Namespaces............................................................................................................................ 112
C.2.6 Classes.................................................................................................................................... 112
C.2.7 Structs..................................................................................................................................... 112
C.2.8 Arrays..................................................................................................................................... 112
C.2.9 Interfaces................................................................................................................................. 112
C.2.10 Enums.................................................................................................................................... 112
C.2.11 Delegates............................................................................................................................... 112
C.2.12 Attributes............................................................................................................................... 112
C.3 Grammar extensions for unsafe code................................................................................................ 112
C.3.1 Unsafe contexts........................................................................................................................ 112
C.3.1.1 Pointer types...................................................................................................................... 112
C.3.1.2 Pointers in expressions........................................................................................................ 112
C.3.1.3 Pointer indirection............................................................................................................... 112
C.3.1.4 Pointer member access....................................................................................................... 112
C.3.1.5 The address-of operator...................................................................................................... 112
C.3.1.6 The sizeof operator............................................................................................................. 112
C.3.1.7 The fixed statement............................................................................................................ 112
C.3.1.8 Stack allocation................................................................................................................... 112
D. References....................................................................................................................................... 112
1. Introduction
C# is a simple, modern, object oriented, and type-safe programming language derived from C and C++. It will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of Visual Basic and the raw power of C++.
Visual C# .NET is Microsoft’s C# development tool. It includes an interactive development environment, visual designers for building Windows and Web applications, a compiler, and a debugger. Visual C# .NET is part of a suite of products, called Visual Studio .NET, that also includes Visual Basic .NET, Visual C++ .NET, and the JScript scripting language. All of these languages provide access to the Microsoft .NET Framework, which includes a common execution engine and a rich class library. The.NET Framework defines a “Common Language Specification” (CLS), a sort of lingua franca that ensures seamless interoperability between CLS-compliant languages and class libraries. For C# developers, this means that even though C# is a new language, it has complete access to the same rich class libraries that are used by seasoned tools such as Visual Basic .NET and Visual C++ .NET. C# itself does not include a class library.
The rest of this chapter describes the essential features of the language. While later chapters describe rules and exceptions in a detail-oriented and sometimes mathematical manner, this chapter strives for clarity and brevity at the expense of completeness. The intent is to provide the reader with an introduction to the language that will facilitate the writing of early programs and the reading of later chapters.
1.1 Getting started
The canonical “hello, world” program can be written as follows:
using System;
class Hello
{
static void Main() {
Console.WriteLine("hello, world");
}
}
The source code for a C# program is typically stored in one or more text files with a file extension of .cs, as in hello.cs. Using the command-line compiler provided with Visual Studio .NET, such a program can be compiled with the command line directive
csc hello.cs
which produces an application named hello.exe. The output produced by this application when it is run is:
hello, world
Close examination of this program is illuminating:
· The using System; directive references a namespace called System that is provided by the Microsoft .NET Framework class library. This namespace contains the Console class referred to in the Main method. Namespaces provide a hierarchical means of organizing the elements of one or more programs. A “using” directive enables unqualified use of the types that are members of the namespace. The “hello, world” program uses Console.WriteLine as shorthand for System.Console.WriteLine. (For the sake of brevity, most examples in this specification omit the using System; directive.)
· The Main method is a member of the class Hello. It has the static modifier, and so it is a method on the class Hello rather than on instances of this class.
· The entry point for an application—the method that is called to begin execution—is always a static method named Main.
· The “hello, world” output is produced using a class library. The language does not itself provide a class library. Instead, it uses a class library that is also used by Visual Basic .NET and Visual C++ .NET.
For C and C++ developers, it is interesting to note a few things that do not appear in the “hello, world” program.
· The program does not use a global method for Main. Methods and variables are not supported at the global level; such elements are always contained within type declarations (e.g., class and struct declarations).
· The program does not use either “::” or “->” operators. The “::” is not an operator at all, and the “->” operator is used in only a small fraction of programs – those that employ unsafe code (§A). The separator “.” is used in compound names such as Console.WriteLine.
· The program does not contain forward declarations. Forward declarations are never needed, as declaration order is not significant.
· The program does not use #include to import program text. Dependencies among programs are handled symbolically rather than textually. This approach eliminates barriers between applications written using different languages. For example, the Console class need not be written in C#.
1.2 Types
C# supports two kinds of types: value types and reference types. Value types include simple types (e.g., char, int, and float), enum types, and struct types. Reference types include class types, interface types, delegate types, and array types.
Value types differ from reference types in that variables of the value types directly contain their data, whereas variables of the reference types store references to objects. With reference types, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
The example
class Class1
{
public int Value = 0;
}
class Test
{
static void Main() {
int val1 = 0;
int val2 = val1;
val2 = 123;
Class1 ref1 = new Class1();
Class1 ref2 = ref1;
ref2.Value = 123;
Console.WriteLine("Values: {0}, {1}", val1, val2);
Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
}
}
shows this difference. The output produced is
Values: 0, 123
Refs: 123, 123
The assignment to the local variable val1 does not impact the local variable val2 because both local variables are of a value type (the type int) and each local variable of a value type has its own storage. In contrast, the assignment ref2.Value = 123; affects the object that both ref1 and ref2 reference.
The lines
Console.WriteLine("Values: {0}, {1}", val1, val2);
Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
deserve further comment, as they demonstrate some of the string formatting behavior of Console.WriteLine, which takes a variable number of arguments. The first argument is a string, which may contain numbered placeholders like {0} and {1}. Each placeholder refers to a trailing argument with {0} referring to the second argument, {1} referring to the third argument, and so on. Before the output is sent to the console, each placeholder is replaced with the formatted value of its corresponding argument.
Developers can define new value types through enum and struct declarations, and can define new reference types via class, interface, and delegate declarations. The example
public enum Color
{
Red, Blue, Green
}
public struct Point
{
public int x, y;
}
public interface IBase
{
void F();
}
public interface IDerived: IBase
{
void G();
}
public class A
{
protected virtual void H() {
Console.WriteLine("A.H");
}
}
public class B: A, IDerived
{
public void F() {
Console.WriteLine("B.F, implementation of IDerived.F");
}
public void G() {
Console.WriteLine("B.G, implementation of IDerived.G");
}
override protected void H() {
Console.WriteLine("B.H, override of A.H");
}
}
public delegate void EmptyDelegate();
shows an example of each kind of type declaration. Later sections describe type declarations in detail.
No comments:
Post a Comment