c# language specification - ecma international web viewthe type of a character-literal is char....

885
C # Language Specification Working Draft WD3.16 March 28 2017

Upload: nguyennhi

Post on 31-Jan-2018

243 views

Category:

Documents


1 download

TRANSCRIPT

C# Language Specification

C#

Language Specification

Working Draft WD3.16

March 28 2017

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

8. Lexical structure65

8.1 Programs65

8.2 Grammars65

8.2.1 General65

8.2.2 Grammar notation65

8.2.3 Lexical grammar67

8.2.4 Syntactic grammar68

8.2.5 Grammar ambiguities68

8.3 Lexical analysis69

8.3.1 General69

8.3.2 Line terminators69

8.3.3 Comments70

8.3.4 White space71

8.4 Tokens72

8.4.1 General72

8.4.2 Unicode character escape sequences72

8.4.3 Identifiers73

8.4.4 Keywords74

8.4.5 Literals76

8.4.5.1 General76

8.4.5.2 Boolean literals76

8.4.5.3 Integer literals76

8.4.5.4 Real literals77

8.4.5.5 Character literals78

8.4.5.6 String literals79

8.4.5.7 The null literal81

8.4.6 Operators and punctuators81

8.5 Pre-processing directives81

8.5.1 General81

8.5.2 Conditional compilation symbols83

8.5.3 Pre-processing expressions83

8.5.4 Definition directives84

8.5.5 Conditional compilation directives85

8.5.6 Diagnostic directives87

8.5.7 Region directives88

8.5.8 Line directives88

8.5.9 Pragma directives89

8.5.9.1 General89

8.5.9.2 Pragma warning90

9. Basic concepts91

9.1 Application startup91

9.2 Application termination93

9.3 Declarations93

9.4 Members98

9.4.1 General98

9.4.2 Namespace members98

9.4.3 Struct members98

9.4.4 Enumeration members99

9.4.5 Class members99

9.4.6 Interface members99

9.4.7 Array members99

9.4.8 Delegate members99

9.5 Member access99

9.5.1 General99

9.5.2 Declared accessibility100

9.5.3 Accessibility domains100

9.5.4 Protected access103

9.5.5 Accessibility constraints104

9.6 Signatures and overloading105

9.7 Scopes107

9.7.1 General107

9.7.2 Name hiding109

9.7.2.1 General109

9.7.2.2 Hiding through nesting109

9.7.2.3 Hiding through inheritance110

9.8 Namespace and type names111

9.8.1 General111

9.8.2 Unqualified names114

9.8.3 Fully qualified names114

9.9 Automatic memory management115

9.10 Execution order117

10. Types119

10.1 General119

10.2 Reference types119

10.2.1 General119

10.2.2 Class types120

10.2.3 The object type120

10.2.4 The dynamic type121

10.2.5 The string type121

10.2.6 Interface types121

10.2.7 Array types121

10.2.8 Delegate types121

10.3 Value types121

10.3.1 General121

10.3.2 The System.ValueType type122

10.3.3 Default constructors123

10.3.4 Struct types123

10.3.5 Simple types123

10.3.6 Integral types124

10.3.7 Floating-point types125

10.3.8 The decimal type127

10.3.9 The bool type127

10.3.10 Enumeration types128

10.3.11 Nullable value types128

10.3.12 Boxing and unboxing128

10.4 Constructed types141

10.4.1 General141

10.4.2 Type arguments142

10.4.3 Open and closed types142

10.4.4 Bound and unbound types142

10.4.5 Satisfying constraints143

10.5 Type parameters144

10.6 Expression tree types144

10.7 The dynamic type145

11. Variables147

11.1 General147

11.2 Variable categories147

11.2.1 General147

11.2.2 Static variables147

11.2.3 Instance variables147

11.2.3.1 General147

11.2.3.2 Instance variables in classes148

11.2.3.3 Instance variables in structs148

11.2.4 Array elements148

11.2.5 Value parameters148

11.2.6 Reference parameters148

11.2.7 Output parameters149

11.2.8 Local variables149

11.3 Default values150

11.4 Definite assignment151

11.4.1 General151

11.4.2 Initially assigned variables151

11.4.3 Initially unassigned variables152

11.4.4 Precise rules for determining definite assignment152

11.4.4.1 General152

11.4.4.2 General rules for statements152

11.4.4.3 Block statements, checked, and unchecked statements153

11.4.4.4 Expression statements153

11.4.4.5 Declaration statements153

11.4.4.6 If statements153

11.4.4.7 Switch statements153

11.4.4.8 While statements154

11.4.4.9 Do statements154

11.4.4.10 For statements154

11.4.4.11 Break, continue, and goto statements155

11.4.4.12 Throw statements155

11.4.4.13 Return statements155

11.4.4.14 Try-catch statements155

11.4.4.15 Try-finally statements155

11.4.4.16 Try-catch-finally statements156

11.4.4.17 Foreach statements157

11.4.4.18 Using statements157

11.4.4.19 Lock statements157

11.4.4.20 Yield statements157

11.4.4.21 General rules for constant expressions157

11.4.4.22 General rules for simple expressions158

11.4.4.23 General rules for expressions with embedded expressions158

11.4.4.24 Invocation expressions and object creation expressions158

11.4.4.25 Simple assignment expressions159

11.4.4.26 && expressions160

11.4.4.27 || expressions161

11.4.4.28 ! expressions161

11.4.4.29 ?? expressions162

11.4.4.30 ?: expressions162

11.4.4.31 Anonymous functions163

11.5 Variable references164

11.6 Atomicity of variable references164

12. Conversions165

12.1 General165

12.2 Implicit conversions165

12.2.1 General165

12.2.2 Identity conversion166

12.2.3 Implicit numeric conversions166

12.2.4 Implicit enumeration conversions166

12.2.5 Implicit nullable conversions167

12.2.6 Null literal conversions167

12.2.7 Implicit reference conversions167

12.2.8 Boxing conversions168

12.2.9 Implicit dynamic conversions170

12.2.10 Implicit constant expression conversions171

12.2.11 Implicit conversions involving type parameters171

12.2.12 User-defined implicit conversions172

12.2.13 Anonymous function conversions and method group conversions172

12.3 Explicit conversions172

12.3.1 General172

12.3.2 Explicit numeric conversions172

12.3.3 Explicit enumeration conversions174

12.3.4 Explicit nullable conversions175

12.3.5 Explicit reference conversions175

12.3.6 Unboxing conversions176

12.3.7 Explicit dynamic conversions178

12.3.8 Explicit conversions involving type parameters178

12.3.9 User-defined explicit conversions179

12.4 Standard conversions180

12.4.1 General180

12.4.2 Standard implicit conversions180

12.4.3 Standard explicit conversions180

12.5 User-defined conversions180

12.5.1 General180

12.5.2 Permitted user-defined conversions180

12.5.3 Lifted conversion operators180

12.5.4 Evaluation of user-defined conversions181

12.5.5 User-defined implicit conversions182

12.5.6 User-defined explicit conversions183

12.6 Conversions involving nullable types184

12.6.1 Nullable Conversions184

12.6.2 Lifted conversions185

12.7 Anonymous function conversions185

12.7.1 General185

12.7.2 Evaluation of anonymous function conversions to delegate types187

12.7.3 Evaluation of anonymous function conversions to expression tree types187

12.7.4 Implementation example188

12.8 Method group conversions191

13. Expressions195

13.1 General195

13.2 Expression classifications195

13.2.1 General195

13.2.2 Values of expressions196

13.3 Static and Dynamic Binding196

13.3.1 General196

13.3.2 Binding-time197

13.3.3 Dynamic binding197

13.3.4 Types of subexpressions198

13.4 Operators198

13.4.1 General198

13.4.2 Operator precedence and associativity198

13.4.3 Operator overloading199

13.4.4 Unary operator overload resolution201

13.4.5 Binary operator overload resolution201

13.4.6 Candidate user-defined operators202

13.4.7 Numeric promotions202

13.4.7.1 General202

13.4.7.2 Unary numeric promotions202

13.4.7.3 Binary numeric promotions203

13.4.8 Lifted operators203

13.5 Member lookup204

13.5.1 General204

13.5.2 Base types205

13.6 Function members206

13.6.1 General206

13.6.2 Argument lists208

13.6.2.1 General208

13.6.2.2 Corresponding parameters210

13.6.2.3 Run-time evaluation of argument lists210

13.6.3 Type inference212

13.6.3.1 General212

13.6.3.2 The first phase213

13.6.3.3 The second phase213

13.6.3.4 Input types214

13.6.3.5 Output types214

13.6.3.6 Dependence214

13.6.3.7 Output type inferences214

13.6.3.8 Explicit parameter type inferences214

13.6.3.9 Exact inferences214

13.6.3.10 Lower-bound inferences214

13.6.3.11 Upper-bound inferences215

13.6.3.12 Fixing216

13.6.3.13 Inferred return type216

13.6.3.14 Type inference for conversion of method groups217

13.6.3.15 Finding the best common type of a set of expressions218

13.6