1、 C# Language Specification Version 3.0 Notice © 1999-2008 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. Oth
2、er product and company names mentioned herein may be the trademarks of their respective owners. Copyright Microsoft Corporation 1999-2008. All Rights Reserved. Please send corrections, comments, and other feedback to csharp@ Table of Contents Table of Contents 1. Introduction 1 1.1 Hello
3、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 l
4、ocal 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 Destruc
5、tors 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
6、 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 40 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 42 2.4.4.4 Character literals 42 2.4.4.5 String lite
7、rals 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 47 2.5.2 Pre-processing expressions 47 2.5.3 Declaration directives 48 2.5.4 Conditional compilation directives 49 2.5.5 Diagnostic directives 51 2.5
8、6 Region directives 52 2.5.7 Line directives 52 2.5.8 Pragma directives 53 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 memb
9、ers 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
10、 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 Sys
11、tem.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 84 4.2.1 Class typ
12、es 84 4.2.2 The object type 85 4.2.3 The string type 85 4.2.4 Interface types 85 4.2.5 Array types 85 4.2.6 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 8
13、9 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 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 94 5.1.2.2 Instance variables in str
14、ucts 94 5.1.3 Array elements 94 5.1.4 Value parameters 94 5.1.5 Reference parameters 94 5.1.6 Output parameters 95 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
15、 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 99 5.3.3.5 If statements 99 5.3.3.6 Switch statements 99 5.3.3.7 While statements 99 5.3.
16、3.8 Do statements 100 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 101 5.3.3.14 Try-finally statements 101 5.3.3.15 Try-catch-finally statements 101 5.3.3.16 Foreach sta
17、tements 102 5.3.3.17 Using statements 102 5.3.3.18 Lock statements 103 5.3.3.19 Yield statements 103 5.3.3.20 General rules for simple expressions 103 5.3.3.21 General rules for expressions with embedded expressions 103 5.3.3.22 Invocation expressions and object creation expressions 104 5.3.3
18、23 Simple assignment expressions 104 5.3.3.24 && expressions 104 5.3.3.25 || expressions 105 5.3.3.26 ! expressions 106 5.3.3.27 ?? expressions 106 5.3.3.28 ?: expressions 107 5.3.3.29 Anonymous functions 107 5.4 Variable references 108 5.5 Atomicity of variable references 108 6. Conversio
19、ns 109 6.1 Implicit conversions 109 6.1.1 Identity conversion 109 6.1.2 Implicit numeric conversions 109 6.1.3 Implicit enumeration conversions 110 6.1.4 Implicit nullable conversions 110 6.1.5 Null literal conversions 110 6.1.6 Implicit reference conversions 111 6.1.7 Boxing conversions 111
20、 6.1.8 Implicit constant expression conversions 112 6.1.9 Implicit conversions involving type parameters 112 6.1.10 User-defined implicit conversions 112 6.1.11 Anonymous function conversions and method group conversions 112 6.2 Explicit conversions 112 6.2.1 Explicit numeric conversions 113
21、6.2.2 Explicit enumeration conversions 114 6.2.3 Explicit nullable conversions 115 6.2.4 Explicit reference conversions 115 6.2.5 Unboxing conversions 116 6.2.6 Explicit conversions involving type parameters 116 6.2.7 User-defined explicit conversions 117 6.3 Standard conversions 117 6.3.1 St
22、andard implicit conversions 117 6.3.2 Standard explicit conversions 118 6.4 User-defined conversions 118 6.4.1 Permitted user-defined conversions 118 6.4.2 Lifted conversion operators 118 6.4.3 Evaluation of user-defined conversions 118 6.4.4 User-defined implicit conversions 119 6.4.5 User-d
23、efined explicit conversions 120 6.5 Anonymous function conversions 121 6.5.1 Evaluation of anonymous function conversions to delegate types 122 6.5.2 Evaluation of anonymous function conversions to expression tree types 123 6.5.3 Implementation example 123 6.6 Method group conversions 125 7. E
24、xpressions 129 7.1 Expression classifications 129 7.1.1 Values of expressions 130 7.2 Operators 130 7.2.1 Operator precedence and associativity 130 7.2.2 Operator overloading 131 7.2.3 Unary operator overload resolution 133 7.2.4 Binary operator overload resolution 133 7.2.5 Candidate user-d
25、efined operators 133 7.2.6 Numeric promotions 134 7.2.6.1 Unary numeric promotions 134 7.2.6.2 Binary numeric promotions 134 7.2.7 Lifted operators 135 7.3 Member lookup 136 7.3.1 Base types 137 7.4 Function members 137 7.4.1 Argument lists 140 7.4.2 Type inference 142 7.4.2.1 The first ph
26、ase 143 7.4.2.2 The second phase 143 7.4.2.3 Input types 143 7.4.2.4 Output types 143 7.4.2.5 Dependence 143 7.4.2.6 Output type inferences 143 7.4.2.7 Explicit parameter type inferences 144 7.4.2.8 Exact inferences 144 7.4.2.9 Lower-bound inferences 144 7.4.2.10 Fixing 144 7.4.2.11 Inferr
27、ed return type 144 7.4.2.12 Type inference for conversion of method groups 146 7.4.2.13 Finding the best common type of a set of expressions 146 7.4.3 Overload resolution 146 7.4.3.1 Applicable function member 147 7.4.3.2 Better function member 147 7.4.3.3 Better conversion from expression 148
28、 7.4.3.4 Better conversion from type 148 7.4.3.5 Overloading in generic classes 149 7.4.4 Function member invocation 149 7.4.4.1 Invocations on boxed instances 150 7.5 Primary expressions 151 7.5.1 Literals 151 7.5.2 Simple names 151 7.5.2.1 Invariant meaning in blocks 153 7.5.3 Parenthesiz
29、ed expressions 154 7.5.4 Member access 154 7.5.4.1 Identical simple names and type names 155 7.5.4.2 Grammar ambiguities 156 7.5.5 Invocation expressions 157 7.5.5.1 Method invocations 157 7.5.5.2 Extension method invocations 158 7.5.5.3 Delegate invocations 160 7.5.6 Element access 161 7.5
30、6.1 Array access 161 7.5.6.2 Indexer access 162 7.5.7 This access 162 7.5.8 Base access 163 7.5.9 Postfix increment and decrement operators 163 7.5.10 The new operator 164 7.5.10.1 Object creation expressions 165 7.5.10.2 Object initializers 166 7.5.10.3 Collection initializers 168 7.5.10.
31、4 Array creation expressions 169 7.5.10.5 Delegate creation expressions 171 7.5.10.6 Anonymous object creation expressions 172 7.5.11 The typeof operator 174 7.5.12 The checked and unchecked operators 175 7.5.13 Default value expressions 178 7.5.14 Anonymous method expressions 178 7.6 Unary o
32、perators 178 7.6.1 Unary plus operator 178 7.6.2 Unary minus operator 179 7.6.3 Logical negation operator 179 7.6.4 Bitwise complement operator 179 7.6.5 Prefix increment and decrement operators 180 7.6.6 Cast expressions 181 7.7 Arithmetic operators 181 7.7.1 Multiplication operator 181 7.
33、7.2 Division operator 182 7.7.3 Remainder operator 183 7.7.4 Addition operator 184 7.7.5 Subtraction operator 186 7.8 Shift operators 188 7.9 Relational and type-testing operators 189 7.9.1 Integer comparison operators 190 7.9.2 Floating-point comparison operators 190 7.9.3 Decimal compariso
34、n operators 191 7.9.4 Boolean equality operators 191 7.9.5 Enumeration comparison operators 191 7.9.6 Reference type equality operators 192 7.9.7 String equality operators 193 7.9.8 Delegate equality operators 194 7.9.9 Equality operators and null 194 7.9.10 The is operator 194 7.9.11 The as
35、 operator 195 7.10 Logical operators 196 7.10.1 Integer logical operators 196 7.10.2 Enumeration logical operators 196 7.10.3 Boolean logical operators 196 7.10.4 Nullable boolean logical operators 197 7.11 Conditional logical operators 197 7.11.1 Boolean conditional logical operators 198 7.
36、11.2 User-defined conditional logical operators 198 7.12 The null coalescing operator 199 7.13 Conditional operator 199 7.14 Anonymous function expressions 200 7.14.1 Anonymous function signatures 202 7.14.2 Anonymous function bodies 202 7.14.3 Overload resolution 203 7.14.4 Outer variables 2
37、04 7.14.4.1 Captured outer variables 204 7.14.4.2 Instantiation of local variables 204 7.14.5 Evaluation of anonymous function expressions 207 7.15 Query expressions 207 7.15.1 Ambiguities in query expressions 208 7.15.2 Query expression translation 208 7.15.2.1 Select and groupby clauses wit
38、h continuations 209 7.15.2.2 Explicit range variable types 209 7.15.2.3 Degenerate query expressions 210 7.15.2.4 From, let, where, join and orderby clauses 210 7.15.2.5 Select clauses 214 7.15.2.6 Groupby clauses 214 7.15.2.7 Transparent identifiers 214 7.15.3 The query expression pattern 21
39、6 7.16 Assignment operators 217 7.16.1 Simple assignment 218 7.16.2 Compound assignment 219 7.16.3 Event assignment 220 7.17 Expression 221 7.18 Constant expressions 221 7.19 Boolean expressions 222 8. Statements 223 8.1 End points and reachability 223 8.2 Blocks 225 8.2.1 Statement lists
40、 225 8.3 The empty statement 226 8.4 Labeled statements 226 8.5 Declaration statements 227 8.5.1 Local variable declarations 227 8.5.2 Local constant declarations 228 8.6 Expression statements 229 8.7 Selection statements 229 8.7.1 The if statement 229 8.7.2 The switch statement 230 8.8 It
41、eration statements 233 8.8.1 The while statement 234 8.8.2 The do statement 234 8.8.3 The for statement 235 8.8.4 The foreach statement 236 8.9 Jump statements 238 8.9.1 The break statement 239 8.9.2 The continue statement 240 8.9.3 The goto statement 240 8.9.4 The return statement 242 8.9
42、5 The throw statement 242 8.10 The try statement 243 8.11 The checked and unchecked statements 246 8.12 The lock statement 246 8.13 The using statement 247 8.14 The yield statement 249 9. Namespaces 251 9.1 Compilation units 251 9.2 Namespace declarations 251 9.3 Extern aliases 253 9.4 Us
43、ing directives 253 9.4.1 Using alias directives 254 9.4.2 Using namespace directives 256 9.5 Namespace members 258 9.6 Type declarations 258 9.7 Namespace alias qualifiers 259 9.7.1 Uniqueness of aliases 260 10. Classes 261 10.1 Class declarations 261 10.1.1 Class modifiers 261 10.1.1.1 Ab
44、stract classes 262 10.1.1.2 Sealed classes 262 10.1.1.3 Static classes 262 10.1.2 Partial modifier 263 10.1.3 Type parameters 263 10.1.4 Class base specification 264 10.1.4.1 Base classes 264 10.1.4.2 Interface implementations 266 10.1.5 Type parameter constraints 266 10.1.6 Class body 270
45、 10.2 Partial types 270 10.2.1 Attributes 270 10.2.2 Modifiers 271 10.2.3 Type parameters and constraints 271 10.2.4 Base class 272 10.2.5 Base interfaces 272 10.2.6 Members 272 10.2.7 Partial methods 273 10.2.8 Name binding 275 10.3 Class members 275 10.3.1 The instance type 277 10.3.2 M
46、embers of constructed types 277 10.3.3 Inheritance 278 10.3.4 The new modifier 279 10.3.5 Access modifiers 279 10.3.6 Constituent types 279 10.3.7 Static and instance members 279 10.3.8 Nested types 280 10.3.8.1 Fully qualified name 281 10.3.8.2 Declared accessibility 281 10.3.8.3 Hiding 28
47、2 10.3.8.4 this access 282 10.3.8.5 Access to private and protected members of the containing type 283 10.3.8.6 Nested types in generic classes 284 10.3.9 Reserved member names 284 10.3.9.1 Member names reserved for properties 285 10.3.9.2 Member names reserved for events 286 10.3.9.3 Member
48、names reserved for indexers 286 10.3.9.4 Member names reserved for destructors 286 10.4 Constants 286 10.5 Fields 288 10.5.1 Static and instance fields 289 10.5.2 Readonly fields 289 10.5.2.1 Using static readonly fields for constants 290 10.5.2.2 Versioning of constants and static readonly f
49、ields 290 10.5.3 Volatile fields 291 10.5.4 Field initialization 292 10.5.5 Variable initializers 292 10.5.5.1 Static field initialization 293 10.5.5.2 Instance field initialization 294 10.6 Methods 295 10.6.1 Method parameters 297 10.6.1.1 Value parameters 298 10.6.1.2 Reference parameters 298 10.6.1.3 Output parameters 299 10.6.1.4 Parameter arrays 300 10.6.2 Static and instance methods 302 10.6.3 Virtual methods 302 10.6.4 Override methods 304 10.6.5 Sealed methods 306 10.6.6 Abstract methods 307 10.6.7 External methods 308 10.6.8 Partial methods 309 10.6.9 E






