| Preface | xvii |
| Who Is This Book For? | xvii |
| What’s in This Book? | xvii |
| A Warning | xviii |
| About MSDN | xviii |
| MSDN Online | xviii |
| MSDN Publications | ix |
| MSDN Subscriptions | ix |
| 1. Introduction | 1 |
| 1.1 Getting started | 1 |
| 1.2 Types | 2 |
| 1.2.1 Predefined types | 5 |
| 1.2.2 Conversions | 7 |
| 1.2.3 Array types | 8 |
| 1.2.4 Type system unification | 11 |
| 1.3 Variables and parameters | 12 |
| 1.4 Automatic memory management | 17 |
| 1.5 Expressions | 20 |
| 1.6 Statements | 21 |
| 1.7 Classes | 24 |
| 1.7.1 Constants | 27 |
| 1.7.2 Fields | 27 |
| 1.7.3 Methods | 29 |
| 1.7.4 Properties | 30 |
| 1.7.5 Events | 31 |
| 1.7.6 Operators | 33 |
| 1.7.7 Indexers | 35 |
| 1.7.8 Instance constructors | 36 |
| 1.7.9 Destructors | 37 |
| 1.7.10 Static constructors | 38 |
| 1.7.11 Inheritance | 38 |
| 1.8 Structs | 40 |
| 1.9 Interfaces | 41 |
| 1.10 Delegates | 43 |
| 1.11 Enums | 44 |
| 1.12 Namespaces and assemblies | 45 |
| 1.13 Versioning | 48 |
| 1.14 Attributes | 51 |
| 2. Lexical structure | 53 |
| 2.1 Phases of translation | 53 |
| 2.2 Grammar notation | 53 |
| 2.3 Lexical analysis | 54 |
| 2.3.1 Input | 55 |
| 2.3.2 Input characters | 55 |
| 2.3.3 Line terminators | 55 |
| 2.3.4 Comments | 55 |
| 2.3.5 White space | 57 |
| 2.4 Tokens | 57 |
| 2.4.1 Unicode character escape sequences | 57 |
| 2.4.2 Identifiers | 58 |
| 2.4.3 Keywords | 60 |
| 2.4.4 Literals | 61 |
| 2.4.4.1 Boolean literals | 61 |
| 2.4.4.2 Integer literals | 61 |
| 2.4.4.3 Real literals | 62 |
| 2.4.4.4 Character literals | 63 |
| 2.4.4.5 String literals | 64 |
| 2.4.4.6 The null literal | 66 |
| 2.4.5 Operators and punctuators | 66 |
| 2.5 Pre-processing directives | 66 |
| 2.5.1 Pre-processing identifiers | 67 |
| 2.5.2 Pre-processing expressions | 67 |
| 2.5.3 Pre-processing declarations | 68 |
| 2.5.4 #if, #elif, #else, #endif | 69 |
| 2.5.5 #error and #warning | 72 |
| 2.5.6 #region and #endregion | 73 |
| 2.5.7 #line | 73 |
| 3. Basic concepts | 75 |
| 3.1 Program Startup | 75 |
| 3.2 Program Termination | 76 |
| 3.3 Declarations | 76 |
| 3.4 Members | 79 |
| 3.4.1 Namespace members | 80 |
| 3.4.2 Struct members | 80 |
| 3.4.3 Enumeration members | 80 |
| 3.4.4 Class members | 81 |
| 3.4.5 Interface members | 81 |
| 3.4.6 Array members | 81 |
| 3.4.7 Delegate members | 81 |
| 3.5 Member access | 81 |
| 3.5.1 Declared accessibility | 82 |
| 3.5.2 Accessibility domains | 83 |
| 3.5.3 Protected access | 86 |
| 3.5.4 Accessibility constraints | 87 |
| 3.6 Signatures and overloading | 88 |
| 3.7 Scopes | 89 |
| 3.7.1 Name hiding | 92 |
| 3.7.1.1 Hiding through nesting | 92 |
| 3.7.1.2 Hiding through inheritance | 93 |
| 3.8 Namespace and type names | 95 |
| 3.8.1 Fully qualified names | 96 |
| 4. Types | 99 |
| 4.1 Value types | 99 |
| 4.1.1 Default constructors | 100 |
| 4.1.2 Struct types | 101 |
| 4.1.3 Simple types | 102 |
| 4.1.4 Integral types | 103 |
| 4.1.5 Floating point types | 104 |
| 4.1.6 The decimal type | 106 |
| 4.1.7 The bool type | 106 |
| 4.1.8 Enumeration types | 107 |
| 4.2 Reference types | 107 |
| 4.2.1 Class types | 108 |
| 4.2.2 The object type | 108 |
| 4.2.3 The string type | 108 |
| 4.2.4 Interface types | 108 |
| 4.2.5 Array types | 108 |
| 4.2.6 Delegate types | 109 |
| 4.3 Boxing and unboxing | 109 |
| 4.3.1 Boxing conversions | 109 |
| 4.3.2 Unboxing conversions | 111 |
| 5. Variables | 113 |
| 5.1 Variable categories | 113 |
| 5.1.1 Static variables | 114 |
| 5.1.2 Instance variables | 114 |
| 5.1.2.1 Instance variables in classes | 114 |
| 5.1.2.2 Instance variables in structs | 114 |
| 5.1.3 Array elements | 114 |
| 5.1.4 Value parameters | 115 |
| 5.1.5 Reference parameters | 115 |
| 5.1.6 Output parameters | 115 |
| 5.1.7 Local variables | 116 |
| 5.2 Default values | 116 |
| 5.3 Definite assignment | 117 |
| 5.3.1 Initially assigned variables | 120 |
| 5.3.2 Initially unassigned variables | 120 |
| 5.4 Variable references | 120 |
| 6. Conversions | 121 |
| 6.1 Implicit conversions | 121 |
| 6.1.1 Identity conversion | 121 |
| 6.1.2 Implicit numeric conversions | 121 |
| 6.1.3 Implicit enumeration conversions | 122 |
| 6.1.4 Implicit reference conversions | 122 |
| 6.1.5 Boxing conversions | 123 |
| 6.1.6 Implicit constant expression conversions | 123 |
| 6.1.7 User-defined implicit conversions | 123 |
| 6.2 Explicit conversions | 123 |
| 6.2.1 Explicit numeric conversions | 124 |
| 6.2.2 Explicit enumeration conversions | 125 |
| 6.2.3 Explicit reference conversions | 125 |
| 6.2.4 Unboxing conversions | 126 |
| 6.2.5 User-defined explicit conversions | 126 |
| 6.3 Standard conversions | 127 |
| 6.3.1 Standard implicit conversions | 127 |
| 6.3.2 Standard explicit conversions | 127 |
| 6.4 User-defined conversions | 127 |
| 6.4.1 Permitted user-defined conversions | 127 |
| 6.4.2 Evaluation of user-defined conversions | 128 |
| 6.4.3 User-defined implicit conversions | 129 |
| 6.4.4 User-defined explicit conversions | 130 |
| 7. Expressions | 131 |
| 7.1 Expression classifications | 131 |
| 7.1.1 Values of expressions | 132 |
| 7.2 Operators | 133 |
| 7.2.1 Operator precedence and associativity | 133 |
| 7.2.2 Operator overloading | 134 |
| 7.2.3 Unary operator overload resolution | 136 |
| 7.2.4 Binary operator overload resolution | 136 |
| 7.2.5 Candidate user-defined operators | 137 |
| 7.2.6 Numeric promotions | 137 |
| 7.2.6.1 Unary numeric promotions | 137 |
| 7.2.6.2 Binary numeric promotions | 138 |
| 7.3 Member lookup | 139 |
| 7.3.1 Base types | 139 |
| 7.4 Function members | 140 |
| 7.4.1 Argument lists | 142 |
| 7.4.2 Overload resolution | 145 |
| 7.4.2.1 Applicable function member | 146 |
| 7.4.2.2 Better function member | 147 |
| 7.4.2.3 Better conversion | 147 |
| 7.4.3 Function member invocation | 148 |
| 7.4.3.1 Invocations on boxed instances | 149 |
| 7.4.4 Virtual function member lookup | 150 |
| 7.4.5 Interface function member lookup | 150 |
| 7.5 Primary expressions | 150 |
| 7.5.1 Literals | 150 |
| 7.5.2 Simple names | 150 |
| 7.5.2.1 Invariant meaning in blocks | 151 |
| 7.5.3 Parenthesized expressions | 153 |
| 7.5.4 Member access | 153 |
| 7.5.4.1 Identical simple names and type names | 155 |
| 7.5.5 Invocation expressions | 155 |
| 7.5.5.1 Method invocations | 156 |
| 7.5.5.2 Delegate invocations | 157 |
| 7.5.6 Element access | 158 |
| 7.5.6.1 Array access | 158 |
| 7.5.6.2 Indexer access | 159 |
| 7.5.6.3 String indexing | 160 |
| 7.5.7 This access | 160 |
| 7.5.8 Base access | 161 |
| 7.5.9 Postfix increment and decrement operators | 161 |
| 7.5.10 new operator | 162 |
| 7.5.10.1 Object creation expressions | 163 |
| 7.5.10.2 Array creation expressions | 164 |
| 7.5.10.3 Delegate creation expressions | 166 |
| 7.5.11 typeof operator | 168 |
| 7.5.12 checked and unchecked operators | 169 |
| 7.6 Unary expressions | 172 |
| 7.6.1 Unary plus operator | 172 |
| 7.6.2 Unary minus operator | 173 |
| 7.6.3 Logical negation operator | 173 |
| 7.6.4 Bitwise complement operator | 174 |
| 7.6.5 Indirection operator | 174 |
| 7.6.6 Address operator | 174 |
| 7.6.7 Prefix increment and decrement operators | 174 |
| 7.6.8 Cast expressions | 175 |
| 7.7 Arithmetic operators | 176 |
| 7.7.1 Multiplication operator | 177 |
| 7.7.2 Division operator | 178 |
| 7.7.3 Remainder operator | 179 |
| 7.7.4 Addition operator | 180 |
| 7.7.5 Subtraction operator | 182 |
| 7.8 Shift operators | 183 |
| 7.9 Relational operators | 185 |
| 7.9.1 Integer comparison operators | 186 |
| 7.9.2 Floating-point comparison operators | 186 |
| 7.9.3 Decimal comparison operators | 187 |
| 7.9.4 Boolean equality operators | 187 |
| 7.9.5 Enumeration comparison operators | 188 |
| 7.9.6 Reference type equality operators | 188 |
| 7.9.7 String equality operators | 190 |
| 7.9.8 Delegate equality operators | 190 |
| 7.9.9 The is operator | 191 |
| 7.9.10 The as operator | 192 |
| 7.10 Logical operators | 193 |
| 7.10.1 Integer logical operators | 193 |
| 7.10.2 Enumeration logical operators | 194 |
| 7.10.3 Boolean logical operators | 194 |
| 7.11 Conditional logical operators | 194 |
| 7.11.1 Boolean conditional logical operators | 195 |
| 7.11.2 User-defined conditional logical operators | 195 |
| 7.12 Conditional operator | 196 |
| 7.13 Assignment operators | 197 |
| 7.13.1 Simple assignment | 198 |
| 7.13.2 Compound assignment | 200 |
| 7.13.3 Event assignment | 201 |
| 7.14 Expression | 201 |
| 7.15 Constant expressions | 202 |
| 7.16 Boolean expressions | 203 |
| 8. Statements | 205 |
| 8.1 End points and reachability | 205 |
| 8.2 Blocks | 207 |
| 8.2.1 Statement lists | 208 |
| 8.3 The empty statement | 208 |
| 8.4 Labeled statements | 209 |
| 8.5 Declaration statements | 210 |
| 8.5.1 Local variable declarations | 210 |
| 8.5.2 Local constant declarations | 211 |
| 8.6 Expression statements | 212 |
| 8.7 Selection statements | 212 |
| 8.7.1 The if statement | 212 |
| 8.7.2 The switch statement | 214 |
| 8.8 Iteration statements | 218 |
| 8.8.1 The while statement | 218 |
| 8.8.2 The do statement | 219 |
| 8.8.3 The for statement | 220 |
| 8.8.4 The foreach statement | 221 |
| 8.9 Jump statements | 223 |
| 8.9.1 The break statement | 224 |
| 8.9.2 The continue statement | 225 |
| 8.9.3 The goto statement | 225 |
| 8.9.4 The return statement | 227 |
| 8.9.5 The throw statement | 227 |
| 8.10 The try statement | 229 |
| 8.11 The checked and unchecked statements | 232 |
| 8.12 The lock statement | 232 |
| 8.13 The using statement | 233 |
| 9. Namespaces | 237 |
| 9.1 Compilation units | 237 |
| 9.2 Namespace declarations | 238 |
| 9.3 Using directives | 239 |
| 9.3.1 Using alias directives | 240 |
| 9.3.2 Using namespace directives | 243 |
| 9.4 Namespace members | 245 |
| 9.5 Type declarations | 245 |
| 10. Classes | 247 |
| 10.1 Class declarations | 247 |
| 10.1.1 Class modifiers | 247 |
| 10.1.1.1 Abstract classes | 248 |
| 10.1.1.2 Sealed classes | 248 |
| 10.1.2 Class base specification | 249 |
| 10.1.2.1 Base classes | 249 |
| 10.1.2.2 Interface implementations | 250 |
| 10.1.3 Class body | 251 |
| 10.2 Class members | 251 |
| 10.2.1 Inheritance | 252 |
| 10.2.2 The new modifier | 253 |
| 10.2.3 Access modifiers | 253 |
| 10.2.4 Constituent types | 254 |
| 10.2.5 Static and instance members | 254 |
| 10.2.6 Nested types | 255 |
| 10.3 Constants | 255 |
| 10.4 Fields | 257 |
| 10.4.1 Static and instance fields | 259 |
| 10.4.2 Readonly fields | 259 |
| 10.4.2.1 Using static readonly fields for constants | 260 |
| 10.4.2.2 Versioning of constants and static readonly fields | 260 |
| 10.4.3 Field initialization | 261 |
| 10.4.4 Variable initializers | 261 |
| 10.4.4.1 Static field initialization | 263 |
| 10.4.4.2 Instance field initialization | 263 |
| 10.5 Methods | 263 |
| 10.5.1 Method parameters | 265 |
| 10.5.1.1 Value parameters | 266 |
| 10.5.1.2 Reference parameters | 266 |
| 10.5.1.3 Output parameters | 267 |
| 10.5.1.4 Parameter arrays | 269 |
| 10.5.2 Static and instance methods | 272 |
| 10.5.3 Virtual methods | 272 |
| 10.5.4 Override methods | 275 |
| 10.5.5 Sealed methods | 277 |
| 10.5.6 Abstract methods | 278 |
| 10.5.7 External methods | 280 |
| 10.5.8 Method body | 281 |
| 10.5.9 Method overloading | 281 |
| 10.6 Properties | 282 |
| 10.6.1 Static properties | 283 |
| 10.6.2 Accessors | 283 |
| 10.6.3 Virtual, sealed, override, and abstract accessors | 290 |
| 10.7 Events | 292 |
| 10.7.1 Event accessors | 294 |
| 10.7.2 Static events | 296 |
| 10.7.3 Virtual, sealed, override, and abstract accessors | 296 |
| 10.8 Indexers | 297 |
| 10.8.1 Indexer overloading | 300 |
| 10.9 Operators | 301 |
| 10.9.1 Unary operators | 302 |
| 10.9.2 Binary operators | 302 |
| 10.9.3 Conversion operators | 303 |
| 10.10 Instance constructors | 304 |
| 10.10.1 Constructor initializers | 306 |
| 10.10.2 Instance variable initializers | 307 |
| 10.10.3 Constructor execution | 307 |
| 10.10.4 Default constructors | 309 |
| 10.10.5 Private constructors | 310 |
| 10.10.6 Optional constructor parameters | 310 |
| 10.11 Static constructors | 311 |
| 10.12 Destructors | 314 |
| 11. Structs | 315 |
| 11.1 Struct declarations | 315 |
| 11.1.1 Struct modifiers | 316 |
| 11.1.2 Struct interfaces | 316 |
| 11.1.3 Struct body | 316 |
| 11.2 Struct members | 317 |
| 11.3 Class and struct differences | 317 |
| 11.3.1 Value semantics | 317 |
| 11.3.2 Inheritance | 318 |
| 11.3.3 Assignment | 318 |
| 11.3.4 Default values | 319 |
| 11.3.5 Boxing and unboxing | 320 |
| 11.3.6 Meaning of this | 320 |
| 11.3.7 Field initializers | 320 |
| 11.3.8 Constructors | 321 |
| 11.3.9 Destructors | 321 |
| 11.4 Struct examples | 321 |
| 11.4.1 Database integer type | 321 |
| 11.4.2 Database boolean type | 323 |
| 12. Arrays | 325 |
| 12.1 Array types | 325 |
| 12.1.1 The System.Array type | 326 |
| 12.2 Array creation | 326 |
| 12.3 Array element access | 327 |
| 12.4 Array members | 327 |
| 12.5 Array covariance | 327 |
| 12.6 Array initializers | 328 |
| 13. Interfaces | 331 |
| 13.1 Interface declarations | 331 |
| 13.1.1 Interface modifiers | 331 |
| 13.1.2 Base interfaces | 332 |
| 13.1.3 Interface body | 333 |
| 13.2 Interface members | 333 |
| 13.2.1 Interface methods | 334 |
| 13.2.2 Interface properties | 335 |
| 13.2.3 Interface events | 335 |
| 13.2.4 Interface indexers | 335 |
| 13.2.5 Interface member access | 336 |
| 13.3 Fully qualified interface member names | 338 |
| 13.4 Interface implementations | 339 |
| 13.4.1 Explicit interface member implementations | 340 |
| 13.4.2 Interface mapping | 342 |
| 13.4.3 Interface implementation inheritance | 346 |
| 13.4.4 Interface re-implementation | 348 |
| 13.4.5 Abstract classes and interfaces | 350 |
| 14. Enums | 351 |
| 14.1 Enum declarations | 351 |
| 14.2 Enum modifiers | 352 |
| 14.3 Enum members | 352 |
| 14.4 Enum values and operations | 355 |
| 15. Delegates | 357 |
| 15.1 Delegate declarations | 357 |
| 15.1.1 Combinable delegate types | 358 |
| 15.2 Delegate instantiation | 358 |
| 15.3 Multi-cast delegates | 359 |
| 15.4 Delegate invocation | 359 |
| 16. Exceptions | 361 |
| 16.1 Causes of exceptions | 361 |
| 16.2 The System.Exception class | 361 |
| 16.3 How exceptions are handled | 362 |
| 16.4 Common Exception Classes | 363 |
| 17. Attributes | 365 |
| 17.1 Attribute classes | 365 |
| 17.1.1 The AttributeUsage attribute | 365 |
| 17.1.2 Positional and named parameters | 366 |
| 17.1.3 Attribute parameter types | 367 |
| 17.2 Attribute specification | 368 |
| 17.3 Attribute instances | 371 |
| 17.3.1 Compilation of an attribute | 372 |
| 17.3.2 Run-time retrieval of an attribute instance | 372 |
| 17.4 Reserved attributes | 373 |
| 17.4.1 The AttributeUsage attribute | 373 |
| 17.4.2 The Conditional attribute | 374 |
| 17.4.3 The Obsolete attribute | 377 |
| A. Unsafe code | 379 |
| A.1 Unsafe contexts | 379 |
| A.2 Pointer types | 381 |
| A.3 Fixed and moveable variables | 383 |
| A.4 Pointer conversions | 384 |
| A.5 Pointers in expressions | 384 |
| A.5.1 Pointer indirection | 385 |
| A.5.2 Pointer member access | 385 |
| A.5.3 Pointer element access | 386 |
| A.5.4 The address-of operator | 387 |
| A.5.5 Pointer increment and decrement | 388 |
| A.5.6 Pointer arithmetic | 389 |
| A.5.7 Pointer comparison | 389 |
| A.5.8 The sizeof operator | 390 |
| A.6 The fixed statement | 390 |
| A.7 Stack allocation | 393 |
| A.8 Dynamic memory allocation | 394 |
| B. Interoperability | 397 |
| B.1 The ComAliasName attribute | 397 |
| B.2 The ComImport attribute | 397 |
| B.3 The ComRegisterFunction attribute | 398 |
| B.4 The ComSourceInterfaces attribute | 398 |
| B.5 The ComUnregisterFunction attribute | 399 |
| B.6 The ComVisible attribute | 399 |
| B.7 The DispId attribute | 400 |
| B.8 The DllImport attribute | 400 |
| B.9 The FieldOffset attribute | 401 |
| B.10 The Guid attribute | 402 |
| B.11 The HasDefaultInterface attribute | 402 |
| B.12 The ImportedFromTypeLib attribute | 403 |
| B.13 The In and Out attributes | 403 |
| B.14 The IndexerName attribute | 404 |
| B.15 The InterfaceType attribute | 404 |
| B.16 The MarshalAs attribute | 405 |
| B.17 The NoIDispatch attribute | 405 |
| B.18 The PreserveSig attribute | 406 |
| B.19 The StructLayout attribute | 406 |
| B.20 The TypeLibFunc attribute | 407 |
| B.21 The TypeLibType attribute | 407 |
| B.22 The TypeLibVar attribute | 407 |
| B.23 Supporting enums | 408 |
| C. References | 411 |