Upload
letram
View
213
Download
0
Embed Size (px)
Citation preview
C# Language Specification
C#
Language Specification
Working Draft WD3.15
January 25 2017
Table of Contents
Foreword1
Introduction2
1. Scope3
2. Conformance5
3. Normative references7
4. Terms and definitions9
5. Acronyms and abbreviations13
6. General description15
7. Language overview17
8. Lexical structure18
8.1 Programs18
8.2 Grammars18
8.2.1 General18
8.2.2 Grammar notation18
8.2.3 Lexical grammar19
8.2.4 Syntactic grammar20
8.2.5 Grammar ambiguities20
8.3 Lexical analysis21
8.3.1 General21
8.3.2 Line terminators21
8.3.3 Comments22
8.3.4 White space23
8.4 Tokens23
8.4.1 General23
8.4.2 Unicode character escape sequences24
8.4.3 Identifiers24
8.4.4 Keywords26
8.4.5 Literals27
8.4.5.1 General27
8.4.5.2 Boolean literals27
8.4.5.3 Integer literals27
8.4.5.4 Real literals28
8.4.5.5 Character literals29
8.4.5.6 String literals30
8.4.5.7 The null literal32
8.4.6 Operators and punctuators32
8.5 Pre-processing directives33
8.5.1 General33
8.5.2 Conditional compilation symbols34
8.5.3 Pre-processing expressions34
8.5.4 Definition directives35
8.5.5 Conditional compilation directives36
8.5.6 Diagnostic directives38
8.5.7 Region directives39
8.5.8 Line directives39
8.5.9 Pragma directives40
8.5.9.1 General40
8.5.9.2 Pragma warning40
9. Basic concepts43
9.1 Application startup43
9.2 Application termination44
9.3 Declarations44
9.4 Members47
9.4.1 General47
9.4.2 Namespace members47
9.4.3 Struct members47
9.4.4 Enumeration members47
9.4.5 Class members48
9.4.6 Interface members48
9.4.7 Array members48
9.4.8 Delegate members48
9.5 Member access48
9.5.1 General48
9.5.2 Declared accessibility48
9.5.3 Accessibility domains49
9.5.4 Protected access51
9.5.5 Accessibility constraints53
9.6 Signatures and overloading53
9.7 Scopes55
9.7.1 General55
9.7.2 Name hiding57
9.7.2.1 General57
9.7.2.2 Hiding through nesting58
9.7.2.3 Hiding through inheritance58
9.8 Namespace and type names59
9.8.1 General59
9.8.2 Fully qualified names61
9.9 Automatic memory management62
9.10 Execution order64
10. Types67
10.1 General67
10.2 Reference types67
10.2.1 General67
10.2.2 Class types68
10.2.3 The object type68
10.2.4 The dynamic type69
10.2.5 The string type69
10.2.6 Interface types69
10.2.7 Array types69
10.2.8 Delegate types69
10.3 Value types69
10.3.1 General69
10.3.2 The System.ValueType type70
10.3.3 Default constructors70
10.3.4 Struct types71
10.3.5 Simple types71
10.3.6 Integral types72
10.3.7 Floating-point types73
10.3.8 The decimal type74
10.3.9 The bool type75
10.3.10 Enumeration types75
10.3.11 Nullable value types75
10.3.12 Boxing and unboxing76
10.4 Constructed types76
10.4.1 General76
10.4.2 Type arguments77
10.4.3 Open and closed types77
10.4.4 Bound and unbound types77
10.4.5 Satisfying constraints77
10.5 Type parameters78
10.6 Expression tree types79
10.7 The dynamic type80
11. Variables81
11.1 General81
11.2 Variable categories81
11.2.1 General81
11.2.2 Static variables81
11.2.3 Instance variables81
11.2.3.1 General81
11.2.3.2 Instance variables in classes82
11.2.3.3 Instance variables in structs82
11.2.4 Array elements82
11.2.5 Value parameters82
11.2.6 Reference parameters82
11.2.7 Output parameters83
11.2.8 Local variables83
11.3 Default values84
11.4 Definite assignment84
11.4.1 General84
11.4.2 Initially assigned variables85
11.4.3 Initially unassigned variables85
11.4.4 Precise rules for determining definite assignment85
11.4.4.1 General85
11.4.4.2 General rules for statements86
11.4.4.3 Block statements, checked, and unchecked statements86
11.4.4.4 Expression statements86
11.4.4.5 Declaration statements87
11.4.4.6 If statements87
11.4.4.7 Switch statements87
11.4.4.8 While statements87
11.4.4.9 Do statements87
11.4.4.10 For statements88
11.4.4.11 Break, continue, and goto statements88
11.4.4.12 Throw statements88
11.4.4.13 Return statements88
11.4.4.14 Try-catch statements89
11.4.4.15 Try-finally statements89
11.4.4.16 Try-catch-finally statements89
11.4.4.17 Foreach statements90
11.4.4.18 Using statements90
11.4.4.19 Lock statements90
11.4.4.20 Yield statements91
11.4.4.21 General rules for constant expressions91
11.4.4.22 General rules for simple expressions91
11.4.4.23 General rules for expressions with embedded expressions91
11.4.4.24 Invocation expressions and object creation expressions92
11.4.4.25 Simple assignment expressions92
11.4.4.26 && expressions93
11.4.4.27 || expressions94
11.4.4.28 ! expressions94
11.4.4.29 ?? expressions95
11.4.4.30 ?: expressions95
11.4.4.31 Anonymous functions95
11.5 Variable references96
11.6 Atomicity of variable references96
12. Conversions97
12.1 General97
12.2 Implicit conversions97
12.2.1 General97
12.2.2 Identity conversion98
12.2.3 Implicit numeric conversions98
12.2.4 Implicit enumeration conversions98
12.2.5 Implicit nullable conversions98
12.2.6 Null literal conversions98
12.2.7 Implicit reference conversions99
12.2.8 Boxing conversions99
12.2.9 Implicit dynamic conversions101
12.2.10 Implicit constant expression conversions101
12.2.11 Implicit conversions involving type parameters101
12.2.12 User-defined implicit conversions102
12.2.13 Anonymous function conversions and method group conversions102
12.3 Explicit conversions102
12.3.1 General102
12.3.2 Explicit numeric conversions103
12.3.3 Explicit enumeration conversions104
12.3.4 Explicit nullable conversions105
12.3.5 Explicit reference conversions105
12.3.6 Unboxing conversions106
12.3.7 Explicit dynamic conversions106
12.3.8 Explicit conversions involving type parameters107
12.3.9 User-defined explicit conversions108
12.4 Standard conversions108
12.4.1 General108
12.4.2 Standard implicit conversions108
12.4.3 Standard explicit conversions109
12.5 User-defined conversions109
12.5.1 General109
12.5.2 Permitted user-defined conversions109
12.5.3 Lifted conversion operators109
12.5.4 Evaluation of user-defined conversions109
12.5.5 User-defined implicit conversions110
12.5.6 User-defined explicit conversions111
12.6 Conversions involving nullable types112
12.6.1 Nullable Conversions112
12.6.2 Lifted conversions113
12.7 Anonymous function conversions113
12.7.1 General113
12.7.2 Evaluation of anonymous function conversions to delegate types114
12.7.3 Evaluation of anonymous function conversions to expression tree types115
12.7.4 Implementation example115
12.8 Method group conversions118
13. Expressions120
13.1 General120
13.2 Expression classifications120
13.2.1 General120
13.2.2 Values of expressions121
13.3 Static and Dynamic Binding121
13.3.1 General121
13.3.2 Binding-time122
13.3.3 Dynamic binding122
13.3.4 Types of subexpressions123
13.4 Operators123
13.4.1 General123
13.4.2 Operator precedence and associativity123
13.4.3 Operator overloading124
13.4.4 Unary operator overload resolution126
13.4.5 Binary operator overload resolution126
13.4.6 Candidate user-defined operators126
13.4.7 Numeric promotions127
13.4.7.1 General127
13.4.7.2 Unary numeric promotions127
13.4.7.3 Binary numeric promotions127
13.4.8 Lifted operators128
13.5 Member lookup129
13.5.1 General129
13.5.2 Base types130
13.6 Function members130
13.6.1 General130
13.6.2 Argument lists133
13.6.2.1 General133
13.6.2.2 Corresponding parameters134
13.6.2.3 Run-time evaluation of argument lists135
13.6.3 Type inference136
13.6.3.1 General136
13.6.3.2 The first phase137
13.6.3.3 The second phase137
13.6.3.4 Input types138
13.6.3.5 Output types138
13.6.3.6 Dependence138
13.6.3.7 Output type inferences138
13.6.3.8 Explicit parameter type inferences138
13.6.3.9 Exact inferences138
13.6.3.10 Lower-bound inferences139
13.6.3.11 Upper-bound inferences139
13.6.3.12 Fixing140
13.6.3.13 Inferred return type140
13.6.3.14 Type inference for conversion of method groups141
13.6.3.15 Finding the best common type of a set of expressions142
13.6.4 Overload resolution142
13.6.4.1 General142
13.6.4.2 Applicable function member143
13.6.4.3 Better function member143