C#
Language Specification
Version 5.0
Notice
© 1999-2012 Microsoft Corporation. All rights reserved.
Microsoft, Windows, Visual Basic, Visual C#, and Visual C++ are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries/regions.
Other product and company names mentioned herein may be the trademarks of their respective owners.
Copyright Ó Microsoft Corporation 1999-2012. All Rights Reserved.Please send corrections, comments, and other feedback to csharp@microsoft.com
Table of Contents
1. Introduction 1
1.1 Hello world 1
1.2 Program structure 2
1.3 Types and variables 4
1.4 Expressions 6
1.5 Statements 8
1.6 Classes and objects 12
1.6.1 Members 12
1.6.2 Accessibility 13
1.6.3 Type parameters 13
1.6.4 Base classes 14
1.6.5 Fields 14
1.6.6 Methods 15
1.6.6.1 Parameters 15
1.6.6.2 Method body and local variables 16
1.6.6.3 Static and instance methods 17
1.6.6.4 Virtual, override, and abstract methods 18
1.6.6.5 Method overloading 20
1.6.7 Other function members 21
1.6.7.1 Constructors 22
1.6.7.2 Properties 23
1.6.7.3 Indexers 23
1.6.7.4 Events 24
1.6.7.5 Operators 24
1.6.7.6 Destructors 25
1.7 Structs 25
1.8 Arrays 26
1.9 Interfaces 27
1.10 Enums 29
1.11 Delegates 30
1.12 Attributes 31
2. Lexical structure 33
2.1 Programs 33
2.2 Grammars 33
2.2.1 Grammar notation 33
2.2.2 Lexical grammar 34
2.2.3 Syntactic grammar 34
2.3 Lexical analysis 34
2.3.1 Line terminators 35
2.3.2 Comments 35
2.3.3 White space 37
2.4 Tokens 37
2.4.1 Unicode character escape sequences 37
2.4.2 Identifiers 38
2.4.3 Keywords 39
2.4.4 Literals 40
2.4.4.1 Boolean literals 40
2.4.4.2 Integer literals 40
2.4.4.3 Real literals 41
2.4.4.4 Character literals 42
2.4.4.5 String literals 43
2.4.4.6 The null literal 45
2.4.5 Operators and punctuators 45
2.5 Pre-processing directives 45
2.5.1 Conditional compilation symbols 46
2.5.2 Pre-processing expressions 47
2.5.3 Declaration directives 47
2.5.4 Conditional compilation directives 48
2.5.5 Diagnostic directives 51
2.5.6 Region directives 51
2.5.7 Line directives 52
2.5.8 Pragma directives 52
2.5.8.1 Pragma warning 53
3. Basic concepts 55
3.1 Application Startup 55
3.2 Application termination 56
3.3 Declarations 56
3.4 Members 58
3.4.1 Namespace members 58
3.4.2 Struct members 59
3.4.3 Enumeration members 59
3.4.4 Class members 59
3.4.5 Interface members 60
3.4.6 Array members 60
3.4.7 Delegate members 60
3.5 Member access 60
3.5.1 Declared accessibility 60
3.5.2 Accessibility domains 61
3.5.3 Protected access for instance members 63
3.5.4 Accessibility constraints 64
3.6 Signatures and overloading 65
3.7 Scopes 66
3.7.1 Name hiding 69
3.7.1.1 Hiding through nesting 69
3.7.1.2 Hiding through inheritance 70
3.8 Namespace and type names 71
3.8.1 Fully qualified names 73
3.9 Automatic memory management 73
3.10 Execution order 76
4. Types 77
4.1 Value types 77
4.1.1 The System.ValueType type 78
4.1.2 Default constructors 78
4.1.3 Struct types 79
4.1.4 Simple types 79
4.1.5 Integral types 80
4.1.6 Floating point types 81
4.1.7 The decimal type 82
4.1.8 The bool type 83
4.1.9 Enumeration types 83
4.1.10 Nullable types 83
4.2 Reference types 83
4.2.1 Class types 84
4.2.2 The object type 85
4.2.3 The dynamic type 85
4.2.4 The string type 85
4.2.5 Interface types 85
4.2.6 Array types 85
4.2.7 Delegate types 85
4.3 Boxing and unboxing 86
4.3.1 Boxing conversions 86
4.3.2 Unboxing conversions 87
4.4 Constructed types 88
4.4.1 Type arguments 89
4.4.2 Open and closed types 89
4.4.3 Bound and unbound types 89
4.4.4 Satisfying constraints 89
4.5 Type parameters 90
4.6 Expression tree types 91
4.7 The dynamic type 92
5. Variables 93
5.1 Variable categories 93
5.1.1 Static variables 93
5.1.2 Instance variables 93
5.1.2.1 Instance variables in classes 93
5.1.2.2 Instance variables in structs 94
5.1.3 Array elements 94
5.1.4 Value parameters 94
5.1.5 Reference parameters 94
5.1.6 Output parameters 94
5.1.7 Local variables 95
5.2 Default values 96
5.3 Definite assignment 96
5.3.1 Initially assigned variables 97
5.3.2 Initially unassigned variables 97
5.3.3 Precise rules for determining definite assignment 97
5.3.3.1 General rules for statements 98
5.3.3.2 Block statements, checked, and unchecked statements 98
5.3.3.3 Expression statements 98
5.3.3.4 Declaration statements 98
5.3.3.5 If statements 98
5.3.3.6 Switch statements 99
5.3.3.7 While statements 99
5.3.3.8 Do statements 99
5.3.3.9 For statements 100
5.3.3.10 Break, continue, and goto statements 100
5.3.3.11 Throw statements 100
5.3.3.12 Return statements 100
5.3.3.13 Try-catch statements 100
5.3.3.14 Try-finally statements 101
5.3.3.15 Try-catch-finally statements 101
5.3.3.16 Foreach statements 102
5.3.3.17 Using statements 102
5.3.3.18 Lock statements 102
5.3.3.19 Yield statements 103
5.3.3.20 General rules for simple expressions ...
cojunka