c# language specification - ecma web viewiec — the international electrotechnical commission....

846
C # Language Specification Working Draft WD3.11 June 22 2016

Upload: phungtuong

Post on 30-Jan-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

C# Language Specification

C#

Language Specification

Working Draft WD3.11

June 22 2016

Table of Contents

Forewordxix

Introductionxx

1. Scope1

2. Conformance3

3. Normative references5

4. Terms and definitions7

5. Acronyms and abbreviations13

6. General description15

7. Language overview17

7.1 General17

7.2 Hello world17

7.3 Program structure18

7.4 Types and variables20

7.5 Expressions22

7.6 Statements25

7.7 Classes and objects29

7.7.1 General29

7.7.2 Members30

7.7.3 Accessibility30

7.7.4 Type parameters31

7.7.5 Base classes31

7.7.6 Fields32

7.7.7 Methods32

7.7.7.1 General32

7.7.7.2 Parameters33

7.7.7.3 Method body and local variables34

7.7.7.4 Static and instance methods34

7.7.7.5 Virtual, override, and abstract methods35

7.7.7.6 Method overloading37

7.7.8 Other function members38

7.7.8.1 General38

7.7.8.2 Constructors39

7.7.8.3 Properties40

7.7.8.4 Indexers40

7.7.8.5 Events41

7.7.8.6 Operators41

7.7.8.7 Finalizers42

7.8 Structs42

7.9 Arrays43

7.10 Interfaces44

7.11 Enums46

7.12 Delegates47

7.13 Attributes48

8. Lexical structure51

8.1 Programs51

8.2 Grammars51

8.2.1 General51

8.2.2 Grammar notation51

8.2.3 Lexical grammar53

8.2.4 Syntactic grammar54

8.2.5 Grammar ambiguities54

8.3 Lexical analysis55

8.3.1 General55

8.3.2 Line terminators55

8.3.3 Comments56

8.3.4 White space57

8.4 Tokens58

8.4.1 General58

8.4.2 Unicode character escape sequences58

8.4.3 Identifiers59

8.4.4 Keywords60

8.4.5 Literals62

8.4.5.1 General62

8.4.5.2 Boolean literals62

8.4.5.3 Integer literals62

8.4.5.4 Real literals63

8.4.5.5 Character literals64

8.4.5.6 String literals65

8.4.5.7 The null literal67

8.4.6 Operators and punctuators67

8.5 Pre-processing directives67

8.5.1 General67

8.5.2 Conditional compilation symbols69

8.5.3 Pre-processing expressions69

8.5.4 Definition directives70

8.5.5 Conditional compilation directives71

8.5.6 Diagnostic directives73

8.5.7 Region directives74

8.5.8 Line directives74

8.5.9 Pragma directives75

8.5.9.1 General75

8.5.9.2 Pragma warning76

9. Basic concepts77

9.1 Application startup77

9.2 Application termination79

9.3 Declarations79

9.4 Members84

9.4.1 General84

9.4.2 Namespace members84

9.4.3 Struct members84

9.4.4 Enumeration members84

9.4.5 Class members85

9.4.6 Interface members85

9.4.7 Array members85

9.4.8 Delegate members85

9.5 Member access85

9.5.1 General85

9.5.2 Declared accessibility85

9.5.3 Accessibility domains86

9.5.4 Protected access for instance members89

9.5.5 Accessibility constraints90

9.6 Signatures and overloading90

9.7 Scopes92

9.7.1 General92

9.7.2 Name hiding95

9.7.2.1 General95

9.7.2.2 Hiding through nesting95

9.7.2.3 Hiding through inheritance95

9.8 Namespace and type names97

9.8.1 General97

9.8.2 Fully qualified names99

9.9 Automatic memory management100

9.10 Execution order102

10. Types105

10.1 General105

10.2 Value types105

10.2.1 General105

10.2.2 The System.ValueType type106

10.2.3 Default constructors106

10.2.4 Struct types107

10.2.5 Simple types107

10.2.6 Integral types108

10.2.7 Floating-point types109

10.2.8 The decimal type111

10.2.9 The bool type111

10.2.10 Enumeration types112

10.2.11 Nullable value types112

10.3 Reference types112

10.3.1 General112

10.3.2 Class types113

10.3.3 The object type114

10.3.4 The dynamic type114

10.3.5 The string type114

10.3.6 Interface types114

10.3.7 Array types114

10.3.8 Delegate types114

10.4 Boxing and unboxing115

10.4.1 General115

10.4.2 Boxing conversions115

10.4.3 Unboxing conversions117

10.5 Constructed types119

10.5.1 General119

10.5.2 Type arguments119

10.5.3 Open and closed types120

10.5.4 Bound and unbound types120

10.5.5 Satisfying constraints120

10.6 Type parameters121

10.7 Expression tree types122

10.8 The dynamic type123

11. Variables125

11.1 General125

11.2 Variable categories125

11.2.1 General125

11.2.2 Static variables125

11.2.3 Instance variables125

11.2.3.1 General125

11.2.3.2 Instance variables in classes126

11.2.3.3 Instance variables in structs126

11.2.4 Array elements126

11.2.5 Value parameters126

11.2.6 Reference parameters126

11.2.7 Output parameters127

11.2.8 Local variables127

11.3 Default values128

11.4 Definite assignment128

11.4.1 General128

11.4.2 Initially assigned variables129

11.4.3 Initially unassigned variables129

11.4.4 Precise rules for determining definite assignment130

11.4.4.1 General130

11.4.4.2 General rules for statements130

11.4.4.3 Block statements, checked, and unchecked statements130

11.4.4.4 Expression statements131

11.4.4.5 Declaration statements131

11.4.4.6 If statements131

11.4.4.7 Switch statements131

11.4.4.8 While statements131

11.4.4.9 Do statements132

11.4.4.10 For statements132

11.4.4.11 Break, continue, and goto statements132

11.4.4.12 Throw statements132

11.4.4.13 Return statements132

11.4.4.14 Try-catch statements133

11.4.4.15 Try-finally statements133

11.4.4.16 Try-catch-finally statements133

11.4.4.17 Foreach statements134

11.4.4.18 Using statements134

11.4.4.19 Lock statements135

11.4.4.20 Yield statements135

11.4.4.21 General rules for simple expressions135

11.4.4.22 General rules for expressions with embedded expressions135

11.4.4.23 Invocation expressions and object creation expressions136

11.4.4.24 Simple assignment expressions136

11.4.4.25 && expressions137

11.4.4.26 || expressions137

11.4.4.27 ! expressions138

11.4.4.28 ?? expressions139

11.4.4.29 ?: expressions139

11.4.4.30 Anonymous functions139

11.5 Variable references141

11.6 Atomicity of variable references141

12. Conversions143

12.1 General143

12.2 Implicit conversions143

12.2.1 General143

12.2.2 Identity conversion144

12.2.3 Implicit numeric conversions144

12.2.4 Implicit enumeration conversions144

12.2.5 Implicit nullable conversions144

12.2.6 Null literal conversions145

12.2.7 Implicit reference conversions145

12.2.8 Boxing conversions146

12.2.9 Implicit dynamic conversions147

12.2.10 Implicit constant expression conversions147

12.2.11 Implicit conversions involving type parameters147

12.2.12 User-defined implicit conversions148

12.2.13 Anonymous function conversions and method group conversions148

12.3 Explicit conversions148

12.3.1 General148

12.3.2 Explicit numeric conversions149

12.3.3 Explicit enumeration conversions151

12.3.4 Explicit nullable conversions151

12.3.5 Explicit reference conversions151

12.3.6 Unboxing conversions152

12.3.7 Explicit dynamic conversions153

12.3.8 Explicit conversions involving type parameters154

12.3.9 User-defined explicit conversions155

12.4 Standard conversions155

12.4.1 General155

12.4.2 Standard implicit conversions155

12.4.3 Standard explicit conversions155

12.5 User-defined conversions155

12.5.1 General155

12.5.2 Permitted user-defined conversions156

12.5.3 Lifted conversion operators156

12.5.4 Evaluation of user-defined conversions156

12.5.5 User-defined implicit conversions157

12.5.6 User-defined explicit conversions158

12.6 Conversions involving nullable types160

12.6.1 Nullable Conversions160

12.6.2 Lifted conversions160

12.7 Anonymous function conversions160

12.7.1 General160

12.7.2 Evaluation of anonymous function conversions to delegate types162

12.7.3 Evaluation of anonymous function conversions to expression tree types162

12.7.4 Implementation example162

12.8 Method group conversions166

13. Expressions170

13.1 General170

13.2 Expression classifications170

13.2.1 General170

13.2.2 Values of expressions171

13.3 Static and Dynamic Binding171

13.3.1 General171

13.3.2 Binding-time172

13.3.3 Dynamic binding172

13.3.4 Types of subexpressions173

13.4 Operators173

13.4.1 General173

13.4.2 Operator precedence and associativity173

13.4.3 Operator overloading174

13.4.4 Unary operator overload resolution176

13.4.5 Binary operator overload resolution176

13.4.6 Candidate user-defined operators176

13.4.7 Numeric promotions177

13.4.7.1 General177

13.4.7.2 Unary numeric promotions177

13.4.7.3 Binary numeric promotions177