C # Language Specification Version 4.0 Copyright Microsoft Corporation 1999-2010. All Rights Reserved. Please send corrections, comments, and other feedback to csharp@microsoft.com Notice © 1999-2010 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-2010. All Rights Reserved. Table of Contents 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 Lexical structure 33 1.13 Programs 33 1.14 Grammars 33 1.14.1 Grammar notation 33 1.14.2 Lexical grammar 34 1.14.3 Syntactic grammar 34 1.15 Lexical analysis 34 1.15.1 Line terminators 35 1.15.2 Comments 35 1.15.3 White space 37 1.16 Tokens 37 1.16.1 Unicode character escape sequences 37 1.16.2 Identifiers 38 1.16.3 Keywords 39 1.16.4 Literals 40 1.16.4.1 Boolean literals 40 1.16.4.2 Integer literals 40 Table of Contents 1.16.4.3 Real literals 41 1.16.4.4 Character literals 42 1.16.4.5 String literals 43 1.16.4.6 The null literal 45 1.16.5 Operators and punctuators 45 1.17 Pre-processing directives 45 1.17.1 Conditional compilation symbols 46 1.17.2 Pre-processing expressions 47 1.17.3 Declaration directives 47 1.17.4 Conditional compilation directives 48 1.17.5 Diagnostic directives 51 1.17.6 Region directives 51 1.17.7 Line directives 52 1.17.8 Pragma directives 52 1.17.8.1 Pragma warning 53 Basic concepts 55 1.18 Application Startup 55 1.19 Application termination 56 1.20 Declarations 56 1.21 Members 58 1.21.1 Namespace members 58 1.21.2 Struct members 59 1.21.3 Enumeration members 59 1.21.4 Class members 59 1.21.5 Interface members 60 1.21.6 Array members 60 1.21.7 Delegate members 60 1.22 Member access 60 1.22.1 Declared accessibility 60 1.22.2 Accessibility domains 61 1.22.3 Protected access for instance members 63 1.22.4 Accessibility constraints 64 1.23 Signatures and overloading 65 1.24 Scopes 66 1.24.1 Name hiding 69 1.24.1.1 Hiding through nesting 69 1.24.1.2 Hiding through inheritance 70 1.25 Namespace and type names 71 1.25.1 Fully qualified names 73 1.26 Automatic memory management 73 1.27 Execution order 76 Types 77 1.28 Value types 77 1.28.1 The System.ValueType type 78 1.28.2 Default constructors 78 1.28.3 Struct types 79 1.28.4 Simple types 79 1.28.5 Integral types 80 1.28.6 Floating point types 81 Copyright Microsoft Corporation 1999-2010. All Rights Reserved. iv Table of Contents 1.28.7 The decimal type 82 1.28.8 The bool type 83 1.28.9 Enumeration types 83 1.28.10 Nullable types 83 1.29 Reference types 83 1.29.1 Class types 84 1.29.2 The object type 85 1.29.3 The dynamic type 85 1.29.4 The string type 85 1.29.5 Interface types 85 1.29.6 Array types 85 1.29.7 Delegate types 85 1.30 Boxing and unboxing 86 1.30.1 Boxing conversions 86 1.30.2 Unboxing conversions 87 1.31 Constructed types 88 1.31.1 Type arguments 88 1.31.2 Open and closed types 89 1.31.3 Bound and unbound types 89 1.31.4 Satisfying constraints 89 1.32 Type parameters 90 1.33 Expression tree types 91 1.34 The dynamic type 92 Variables 93 1.35 Variable categories 93 1.35.1 Static variables 93 1.35.2 Instance variables 93 1.35.2.1 Instance variables in classes 93 1.35.2.2 Instance variables in structs 94 1.35.3 Array elements 94 1.35.4 Value parameters 94 1.35.5 Reference parameters 94 1.35.6 Output parameters 94 1.35.7 Local variables 95 1.36 Default values 96 1.37 Definite assignment 96 1.37.1 Initially assigned variables 97 1.37.2 Initially unassigned variables 97 1.37.3 Precise rules for determining definite assignment 97 1.37.3.1 General rules for statements 98 1.37.3.2 Block statements, checked, and unchecked statements 98 1.37.3.3 Expression statements 98 1.37.3.4 Declaration statements 98 1.37.3.5 If statements 98 1.37.3.6 Switch statements 99 1.37.3.7 While statements 99 1.37.3.8 Do statements 99 1.37.3.9 For statements 99 1.37.3.10 Break, continue, and goto statements 100 1.37.3.11 Throw statements 100 Copyright Microsoft Corporation 1999-2010. All Rights Reserved. v C# Language Specification 1.37.3.12 Return statements 100 1.37.3.13 Try-catch statements 100 1.37.3.14 Try-finally statements 101 1.37.3.15 Try-catch-finally statements 101 1.37.3.16 Foreach statements 102 1.37.3.17 Using statements 102 1.37.3.18 Lock statements 102 1.37.3.19 Yield statements 102 1.37.3.20 General rules for simple expressions 103 1.37.3.21 General rules for expressions with embedded expressions 103 1.37.3.22 Invocation expressions and object creation expressions 103 1.37.3.23 Simple assignment expressions 104 1.37.3.24 && expressions 104 1.37.3.25 || expressions 104 1.37.3.26 ! expressions 105 1.37.3.27 ?? expressions 106 1.37.3.28 ?: expressions 106 1.37.3.29 Anonymous functions 106 1.38 Variable references 107 1.39 Atomicity of variable references 107 Conversions 109 1.40 Implicit conversions 109 1.40.1 Identity conversion 109 1.40.2 Implicit numeric conversions 110 1.40.3 Implicit enumeration conversions 110 1.40.4 Implicit nullable conversions 110 1.40.5 Null literal conversions 111 1.40.6 Implicit reference conversions 111 1.40.7 Boxing conversions 111 1.40.8 Implicit dynamic conversions 112 1.40.9 Implicit constant expression conversions 112 1.40.10 Implicit conversions involving type parameters 112 1.40.11 User-defined implicit conversions 113 1.40.12 Anonymous function conversions and method group conversions 113 1.41 Explicit conversions 113 1.41.1 Explicit numeric conversions 114 1.41.2 Explicit enumeration conversions 115 1.41.3 Explicit nullable conversions 115 1.41.4 Explicit reference conversions 116 1.41.5 Unboxing conversions 117 1.41.6 Explicit dynamic conversions 117 1.41.7 Explicit conversions involving type parameters 118 1.41.8 User-defined explicit conversions 119 1.42 Standard conversions 119 1.42.1 Standard implicit conversions 119 1.42.2 Standard explicit conversions 119 1.43 User-defined conversions 119 1.43.1 Permitted user-defined conversions 119 1.43.2 Lifted conversion operators 119 1.43.3 Evaluation of user-defined conversions 120 vi Copyright Microsoft Corporation 1999-2010. All Rights Reserved. Table of Contents 1.43.4 User-defined implicit conversions 121 1.43.5 User-defined explicit conversions 121 1.44 Anonymous function conversions 122 1.44.1 Evaluation of anonymous function conversions to delegate types 123 1.44.2 Evaluation of anonymous function conversions to expression tree types 124 1.44.3 Implementation example 124 1.45 Method group conversions 127 Expressions 129 1.46 Expression classifications 129 1.46.1 Values of expressions 130 1.47 Static and Dynamic Binding 130 1.47.1 Binding-time 131 1.47.2 Dynamic binding 131 1.47.3 Types of constituent expressions 131 1.48 Operators 132 1.48.1 Operator precedence and associativity 132 1.48.2 Operator overloading 133 1.48.3 Unary operator overload resolution 134 1.48.4 Binary operator overload resolution 135 1.48.5 Candidate user-defined operators 135 1.48.6 Numeric promotions 135 1.48.6.1 Unary numeric promotions 136 1.48.6.2 Binary numeric promotions 136 1.48.7 Lifted operators 137 1.49 Member lookup 137 1.49.1 Base types 139 1.50 Function members 139 1.50.1 Argument lists 141 1.50.1.1 Corresponding parameters 142 1.50.1.2 Run-time evaluation of argument lists 143 1.50.2 Type inference 144 1.50.2.1 The first phase 145 1.50.2.2 The second phase 146 1.50.2.3 Input types 146 1.50.2.4 Output types 146 1.50.2.5 Dependence 146 1.50.2.6 Output type inferences 146 1.50.2.7 Explicit parameter type inferences 146 1.50.2.8 Exact inferences 146 1.50.2.9 Lower-bound inferences 147 1.50.2.10 Upper-bound inferences 147 1.50.2.11 Fixing 148 1.50.2.12 Inferred return type 148 1.50.2.13 Type inference for conversion of method groups 149 1.50.2.14 Finding the best common type of a set of expressions 150 1.50.3 Overload resolution 150 1.50.3.1 Applicable function member 150 1.50.3.2 Better function member 151 1.50.3.3 Better conversion from expression 152 1.50.3.4 Better conversion from type 152 Copyright Microsoft Corporation 1999-2010. All Rights Reserved. vii C# Language Specification 1.50.3.5 Better conversion target 152 1.50.3.6 Overloading in generic classes 153 1.50.4 Compile-time checking of dynamic overload resolution 153 1.50.5 Function member invocation 154 1.50.5.1 Invocations on boxed instances 155 1.51 Primary expressions 155 1.51.1 Literals 156 1.51.2 Simple names 156 1.51.2.1 Invariant meaning in blocks 157 1.51.3 Parenthesized expressions 158 1.51.4 Member access 158 1.51.4.1 Identical simple names and type names 160 1.51.4.2 Grammar ambiguities 160 1.51.5 Invocation expressions 161 1.51.5.1 Method invocations 161 1.51.5.2 Extension method invocations 163 1.51.5.3 Delegate invocations 165 1.51.6 Element access 165 1.51.6.1 Array access 166 1.51.6.2 Indexer access 166 1.51.7 This access 167 1.51.8 Base access 167 1.51.9 Postfix increment and decrement operators 168 1.51.10 The new operator 169 1.51.10.1 Object creation expressions 169 1.51.10.2 Object initializers 171 1.51.10.3 Collection initializers 172 1.51.10.4 Array creation expressions 174 1.51.10.5 Delegate creation expressions 176 1.51.10.6 Anonymous object creation expressions 177 1.51.11 The typeof operator 178 1.51.12 The checked and unchecked operators 180 1.51.13 Default value expressions 182 1.51.14 Anonymous method expressions 183 1.52 Unary operators 183 1.52.1 Unary plus operator 183 1.52.2 Unary minus operator 183 1.52.3 Logical negation operator 184 1.52.4 Bitwise complement operator 184 1.52.5 Prefix increment and decrement operators 184 1.52.6 Cast expressions 185 1.53 Arithmetic operators 186 1.53.1 Multiplication operator 186 1.53.2 Division operator 187 1.53.3 Remainder operator 188 1.53.4 Addition operator 189 1.53.5 Subtraction operator 191 1.54 Shift operators 192 1.55 Relational and type-testing operators 194 1.55.1 Integer comparison operators 194 1.55.2 Floating-point comparison operators 195 viii Copyright Microsoft Corporation 1999-2010. All Rights Reserved. Table of Contents 1.55.3 Decimal comparison operators 196 1.55.4 Boolean equality operators 196 1.55.5 Enumeration comparison operators 196 1.55.6 Reference type equality operators 196 1.55.7 String equality operators 198 1.55.8 Delegate equality operators 198 1.55.9 Equality operators and null 199 1.55.10 The is operator 199 1.55.11 The as operator 199 1.56 Logical operators 200 1.56.1 Integer logical operators 201 1.56.2 Enumeration logical operators 201 1.56.3 Boolean logical operators 201 1.56.4 Nullable boolean logical operators 201 1.57 Conditional logical operators 202 1.57.1 Boolean conditional logical operators 203 1.57.2 User-defined conditional logical operators 203 1.58 The null coalescing operator 203 1.59 Conditional operator 204 1.60 Anonymous function expressions 205 1.60.1 Anonymous function signatures 207 1.60.2 Anonymous function bodies 207 1.60.3 Overload resolution 207 1.60.4 Anonymous functions and dynamic binding 208 1.60.5 Outer variables 208 1.60.5.1 Captured outer variables 208 1.60.5.2 Instantiation of local variables 209 1.60.6 Evaluation of anonymous function expressions 211 1.61 Query expressions 211 1.61.1 Ambiguities in query expressions 213 1.61.2 Query expression translation 213 1.61.2.1 Select and groupby clauses with continuations 213 1.61.2.2 Explicit range variable types 214 1.61.2.3 Degenerate query expressions 214 1.61.2.4 From, let, where, join and orderby clauses 215 1.61.2.5 Select clauses 218 1.61.2.6 Groupby clauses 218 1.61.2.7 Transparent identifiers 219 1.61.3 The query expression pattern 220 1.62 Assignment operators 221 1.62.1 Simple assignment 222 1.62.2 Compound assignment 224 1.62.3 Event assignment 225 1.63 Expression 225 1.64 Constant expressions 225 1.65 Boolean expressions 227 Statements 229 1.66 End points and reachability 229 1.67 Blocks 231 1.67.1 Statement lists 231 Copyright Microsoft Corporation 1999-2010. All Rights Reserved. ix C# Language Specification 1.68 The empty statement 232 1.69 Labeled statements 232 1.70 Declaration statements 233 1.70.1 Local variable declarations 233 1.70.2 Local constant declarations 234 1.71 Expression statements 235 1.72 Selection statements 235 1.72.1 The if statement 235 1.72.2 The switch statement 236 1.73 Iteration statements 239 1.73.1 The while statement 239 1.73.2 The do statement 240 1.73.3 The for statement 240 1.73.4 The foreach statement 241 1.74 Jump statements 244 1.74.1 The break statement 245 1.74.2 The continue statement 246 1.74.3 The goto statement 246 1.74.4 The return statement 247 1.74.5 The throw statement 248 1.75 The try statement 249 1.76 The checked and unchecked statements 251 1.77 The lock statement 252 1.78 The using statement 253 1.79 The yield statement 255 Namespaces 257 1.80 Compilation units 257 1.81 Namespace declarations 257 1.82 Extern aliases 258 1.83 Using directives 259 1.83.1 Using alias directives 260 1.83.2 Using namespace directives 262 1.84 Namespace members 264 1.85 Type declarations 264 1.86 Namespace alias qualifiers 265 1.86.1 Uniqueness of aliases 266 Classes 267 1.87 Class declarations 267 1.87.1 Class modifiers 267 1.87.1.1 Abstract classes 268 1.87.1.2 Sealed classes 268 1.87.1.3 Static classes 268 1.87.2 Partial modifier 269 1.87.3 Type parameters 269 1.87.4 Class base specification 270 1.87.4.1 Base classes 270 1.87.4.2 Interface implementations 272 1.87.5 Type parameter constraints 272 1.87.6 Class body 276 x Copyright Microsoft Corporation 1999-2010. All Rights Reserved. [...]... interface I { } Array types Single- and multi-dimensional, for example, int[] and int[,] Delegate types User-defined types of the form e.g delegate int D( ) The eight integral types provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or unsigned form The two floating point types, float and double, are represented using the 32-bit single-precision and 64-bit double-precision IEEE 754 formats... Corporation 199 9-2 010 All Rights Reserved Introduction C# (pronounced “See Sharp”) is a simple, modern, object-oriented, and type-safe programming language C# has its roots in the C family of languages and will be immediately familiar to C, C++, and Java programmers C# is standardized by ECMA International as the ECMA-334 standard and by ISO/IEC as the ISO/IEC 23270 standard Microsoft’s C# compiler for... automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of NET Common Language Runtime Because an assembly is a self-describing unit of functionality containing both code and metadata, there is no need for #include directives and header files in C# The public types and members contained in a particular assembly are made available in a C# program simply by referencing that... Microsoft’s C# compiler for the NET Framework is a conforming implementation of both of these standards C# is an object-oriented language, but C# further includes support for component-oriented programming Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality Key to such components is that they present a programming... The decimal type is a 128-bit data type suitable for financial and monetary calculations C# s bool type is used to represent boolean values—values that are either true or false Character and string processing in C# uses Unicode encoding The char type represents a UTF-16 code unit, and the string type represents a sequence of UTF-16 code units The following table summarizes C# s numeric types 4 Copyright... double High-precision decimal: decimal Boolean: bool Enum types Struct types User-defined types of the form struct S { } Nullable types Reference types User-defined types of the form enum E { } Extensions of all other value types with a null value Class types Ultimate base class of all other types: object Unicode strings: string User-defined types of the form class C { } Interface types User-defined... class libraries, which, by default, are automatically referenced by the Microsoft C# compiler Note that C# itself does not have a separate runtime library Instead, the NET Framework is the runtime library of C# 1.2 Program structure The key organizational concepts in C# are programs, namespaces, types, members, and assemblies C# programs consist of one or more source files Programs declare types, which... transported, and operated upon in a consistent manner Furthermore, C# supports both user-defined reference types and value types, allowing dynamic allocation of objects as well as in-line storage of lightweight structures To ensure that C# programs and libraries can evolve over time in a compatible manner, much emphasis has been placed on versioning in C# s design Many programming languages pay little attention... × 1038, 7-digit precision 64 double 5.0 × 10−324 to 1.7 × 10308, 15-digit precision Decimal 128 decimal 1.0 × 10−28 to 7.9 × 1028, 28-digit precision Unsigned integral C# programs use type declarations to create new types A type declaration specifies the name and the members of the new type Five of C# s categories of types are user-definable: class types, struct types, interface types, enum types,... object-oriented and type-safe Class, struct, interface and delegate types all support generics, whereby they can be parameterized with other types An enum type is a distinct type with named constants Every enum type has an underlying type, which must be one of the eight integral types The set of values of an enum type is the same as the set of values of the underlying type C# supports single- and multi-dimensional . Corporation 199 9-2 010. All Rights Reserved. v C# Language Specification 1.37.3.12 Return statements 100 1.37.3.13 Try-catch statements 100 1.37.3.14 Try-finally statements 101 1.37.3.15 Try-catch-finally. represented using the 32-bit single-precision and 64-bit double-precision IEEE 754 formats. The decimal type is a 128-bit data type suitable for financial and monetary calculations. C# s bool type is. int[] and int[,] Delegate types User-defined types of the form e.g. delegate int D( ) The eight integral types provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or unsigned