Annotated C# Standard (eBook)
864 Seiten
Elsevier Science (Verlag)
978-0-08-055053-4 (ISBN)
* Written by members of the standards committee
* Annotates the standard with practical implementation advice
* The definitive reference to the C# International Standard
Standards, while being definitive, do not usually serve as the best reference to the use of a programming language. Books on languages usually are able to explain usage better, but lack the definitive precision of a standard. Annotated C# Standard combines the two; it is the standard with added explanatory material. Written by members of the standards committee Annotates the standard with practical implementation advice The definitive reference to the C# International Standard
Front Cover 1
C# Annotated Standard 4
Copyright Page 5
Dedications 6
Contents 8
Foreword to the Annotated Standard 25
Preface to the Annotated Standard 26
Acknowledgments 26
About The Authors 29
Errata To The International Standard 30
The C# International Standard and Foreword 33
Introduction 34
CLI not required 34
Chapter 1: Scope 35
Chapter 2: Conformance 36
Interpreters 37
Chapter 3: Normative references 38
Chapter 4: Definitions 39
Application vs. program 39
Assembly vs. class files 39
Accessing class libraries 40
Programs, assemblies, applications and class libraries 40
Chapter 5: Notational conventions 42
Chapter 6: Acronyms and abbreviations 44
ASCII Rules! 44
The C# name 44
Chapter 7: General description 45
Where to look for requirements on unsafe constructs 45
Chapter 8: Language overview 46
Annotation free zone 46
8.1 Getting started 46
8.2 Types 47
8.2.1 Predefined types 49
8.2.2 Conversions 51
8.2.3 Array types 52
8.2.4 Type system unification 54
8.3 Variables and parameters 55
8.4 Automatic memory management 59
8.5 Expressions 61
8.6 Statements 62
8.7 Classes 65
8.7.1 Constants 68
8.7.2 Fields 68
8.7.3 Methods 69
8.7.4 Properties 72
8.7.5 Events 72
8.7.6 Operators 74
8.7.7 Indexers 75
8.7.8 Instance constructors 77
8.7.9 Finalizers 78
8.7.10 Static constructors 78
8.7.11 Inheritance 79
8.7.12 Static classes 80
8.7.13 Partial type declarations 81
8.8 Structs 82
8.9 Interfaces 82
8.10 Delegates 84
8.11 Enums 85
8.12 Namespaces and assemblies 86
8.13 Versioning 88
8.14 Extern aliases 90
8.15 Attributes 92
8.16 Generics 94
8.16.1 Why generics? 94
8.16.2 Creating and consuming generics 95
8.16.3 Multiple type parameters 96
8.16.4 Constraints 96
8.16.5 Generic methods 98
8.17 Anonymous methods 99
8.18 Iterators 102
8.19 Nullable types 106
Chapter 9: Lexical structure 109
9.1 Programs 109
Much ado about nothing 109
9.2 Grammars 110
9.2.1 Lexical grammar 110
9.2.2 Syntactic grammar 110
9.2.3 Grammar ambiguities 111
Rationale: the “following token” set 111
Similar cast expression ambiguity 112
F(G< A, B>
9.3 Lexical analysis 112
9.3.1 Line terminators 113
9.3.2 Comments 114
9.3.3 White space 115
9.4 Tokens 115
9.4.1 Unicode escape sequences 116
No escapes in verbatim strings 116
No escapes in comments 116
9.4.2 Identifiers 117
Identifier normalization 118
The humble underscore 118
Keyword escape mechanism 119
Code generation 120
9.4.3 Keywords 120
Language evolution 120
9.4.4 Literals 121
9.4.4.1 Boolean literals 121
Boolean arguments considered harmful? 121
9.4.4.2 Integer literals 122
Historical note 123
Boundary differences 123
9.4.4.3 Real literals 123
What is 1.D? 124
Money or deciMal? 124
9.4.4.4 Character literals 125
No octal character escapes 126
9.4.4.5 String literals 126
Platform independent newlines 127
Historical note 127
Happy birthday, Joel 128
Overspecification… 128
Hexadecimal escape character pitfalls 128
9.4.4.6 The null literal 129
9.4.5 Operators and punctuators 129
> >
Tokenization anecdote 130
9.5 Pre-processing directives 130
To pre-process, or not pre-process? 131
Why no delimited comments in #directives? 131
Why no macros? 132
9.5.1 Conditional compilation symbols 132
Conditional symbol oddity 133
No program-wide pre-processing symbols 133
9.5.2 Pre-processing expressions 133
Inequality is xor equality is not-xor
9.5.3 Declaration directives 134
Rationale: pp-declaration placement 135
Conditional symbol style 136
9.5.4 Conditional compilation directives 136
Another issue with comments and #if 139
Carbuncle 139
9.5.5 Diagnostic directives 140
Diagnostic directive example 140
9.5.6 Region control 141
Historical note 141
9.5.7 Line directives 141
Historical note 142
#line example 142
9.5.8 Pragma directives 143
Chapter 10: Basic concepts 144
The emperor has no threads! 144
Example: Synchronization and Threads 147
10.1 Application startup 150
Main is callable 151
10.2 Application termination 151
Suppression of finalizers 152
No infanticide 152
10.3 Declarations 152
Declaration spaces and anonymous methods 154
10.4 Members 156
10.4.1 Namespace members 156
10.4.2 Struct members 156
10.4.3 Enumeration members 157
10.4.4 Class members 157
10.4.5 Interface members 157
10.4.7 Delegate members 157
10.5 Member access 157
10.5.1 Declared accessibility 157
Clarification of internal 158
Clarification of protected internal 158
Accessibility vs. visibility 159
Default considered harmful? 159
Default class accessibility pitfall 159
10.5.2 Accessibility domains 160
10.5.3 Protected access for instance members 162
Rationale: access to protected members 163
Access to other instantiations 164
Twin or just sibling? 164
10.5.4 Accessibility constraints 165
Declaration accessibility consistency 166
Constrained object creation 167
10.6 Signatures and overloading 167
Explicit interface implementation 167
Pitfall of overloading on ref or out 168
Why no ref and out overloading? 168
10.7 Scopes 169
Declaration scope within switch statements 170
Anonymous methods impact scope and lifetime 171
In scope but not referable 172
10.7.1 Name hiding 173
Context-sensitive name resolution 173
10.7.1.1 Hiding through nesting 173
Name hiding in a constructor 174
10.7.1.2 Hiding through inheritance 175
Abstract cannot be hidden 176
10.8 Namespace and type names 177
An extended example 179
10.8.1 Unqualified name 183
10.8.2 Fully qualified names 183
10.9 Automatic memory management 184
Constructors, exceptions, and finalizers: a volatile combination 185
One implementation’s garbage is another’s treasure 186
10.10 Execution order 189
Synchronous order, asynchronous chaos 190
Chapter 11: Types 192
Shallow copying of value types 192
Uniformity and hidden costs 192
11.1 Value types 193
The immediate base class of value types 194
11.1.1 The System.ValueType type 194
11.1.2 Default constructors 195
11.1.3 Struct types 195
11.1.4 Simple types 195
Use the keywords! 196
Keywords not allowed! 197
Do not use the keywords 197
11.1.5 Integral types 197
byte/sbyte naming oddity 198
11.1.6 Floating point types 199
Two’s company, three’s a crowd 199
The other floating point type 199
Rarity is relative 200
11.1.7 The decimal type 201
IEEE decimal 201
Using IEEE decimal in C#: supporting legacy data 201
11.1.8 The bool type 202
Strong Booleans 203
11.1.9 Enumeration types 203
Weak enums: distinct but similar 203
11.2 Reference types 203
Objects vs. values 204
11.2.1 Class types 204
11.2.2 The object type 204
11.2.3 The string type 204
Strings are immutable 204
Insecure strings 205
11.2.4 Interface types 205
Interface inheritance is not class inheritance 205
11.2.5 Array types 205
11.2.6 Delegate types 205
11.2.7 The null type 206
Pseudo-type 206
11.3 Boxing and unboxing 206
Can you tell whether a struct instance is boxed? 206
11.3.1 Boxing conversions 208
White lies and diaphanous types 209
11.3.2 Unboxing conversions 211
Sometimes your host bites… 212
11.4 Nullable types 212
11.4.1 Members 213
Default value 213
To be, or not to be…a value type 213
11.4.2 Implemented interfaces 213
Chapter 12: Variables 214
Aliasing 214
12.1 Variable categories 214
12.1.1 Static variables 214
12.1.2 Instance variables 214
12.1.2.1 Instance variables in classes 215
12.1.2.2 Instance variables in structs 215
12.1.3 Array elements 215
12.1.4 Value parameters 215
12.1.5 Reference parameters 215
Examples 216
Example: linked data structures 216
Example: avoiding repeated index calculations 217
Example: avoiding simple reassignment errors 218
12.1.6 Output parameters 219
Output parameters vs. reference parameters 219
Using output parameters to return multiple results 220
12.1.7 Local variables 220
Extended life or early death 221
12.2 Default values 221
Recursive initialization 222
Pointless instance field initialization 222
12.3 Definite assignment 222
Definitely assigned, maybe… 223
12.3.1 Initially assigned variables 224
12.3.2 Initially unassigned variables 224
12.3.3 Precise rules for determining definite assignment 224
Current compilers, constant expressions, and definite assignment 225
12.3.3.1 General rules for statements 226
Arriving from the unreachable 226
12.3.3.2 Block statements, checked, and unchecked statements 227
12.3.3.3 Expression statements 227
12.3.3.4 Declaration statements 227
12.3.3.5 If statements 227
Definitely confused? 227
12.3.3.6 Switch statements 228
12.3.3.7 While statements 228
12.3.3.8 Do statements 228
12.3.3.9 For statements 229
12.3.3.10 Break, continue, and goto statements 229
12.3.3.11 Throw statements 229
12.3.3.12 Return statements 229
12.3.3.13 Try-catch statements 230
12.3.3.14 Try-finally statements 230
12.3.3.15 Try-catch-finally statements 230
12.3.3.16 Foreach statements 231
12.3.3.17 Using statements 232
12.3.3.18 Lock statements 232
12.3.3.19 General rules for simple expressions 232
12.3.3.20 General rules for expressions with embedded expressions 232
12.3.3.21 Invocation expressions and object creation expressions 233
Trying to peek at an uninitialized variable 233
12.3.3.22 Simple assignment expressions 233
12.3.3.23 & &
12.3.3.24 || expressions 235
12.3.3.25 ! expressions 236
12.3.3.26 ?: expressions 236
12.3.3.27 Anonymous method expressions 236
12.3.3.28 Yield statements 237
12.3.3.29 ?? expressions 237
Alternative definite assignment rules for the ?? operator 238
12.4 Variable references 239
“classified as a variable” 239
12.5 Atomicity of variable references 240
Atomicity vs. volatility 240
Chapter 13: Conversions 241
13.1 Implicit conversions 241
13.1.1 Identity conversion 241
13.1.2 Implicit numeric conversions 241
Loss of precision 242
13.1.3 Implicit enumeration conversions 242
Equal rights for hexadecimals 242
13.1.4 Implicit reference conversions 243
Conversions to implemented interfaces 243
13.1.5 Boxing conversions 244
Leave my box alone… 244
13.1.6 Implicit type parameter conversions 245
13.1.7 Implicit constant expression conversions 245
Mind your constants 245
13.1.8 User-defined implicit conversions 245
13.2 Explicit conversions 245
Being explicitly implicit 246
13.2.1 Explicit numeric conversions 246
Why no double to decimal implicit conversions? 248
Decimal conversion exceptions dependent on format 248
13.2.2 Explicit enumeration conversions 249
Dubious explicit enum conversions 249
13.2.3 Explicit reference conversions 249
Never changes, maybe… 250
Conversion from System.Collections.Generic.IList< T>
Mauled by the host, again… 252
13.2.4 Unboxing conversions 252
13.2.5 Explicit type parameter conversions 253
13.2.6 User-defined explicit conversions 253
13.3 Standard conversions 253
13.3.1 Standard implicit conversions 254
13.3.2 Standard explicit conversions 254
13.4 User-defined conversions 254
13.4.1 Permitted user-defined conversions 254
13.4.2 Evaluation of user-defined conversions 254
13.4.3 User-defined implicit conversions 255
13.4.4 User-defined explicit conversions 256
13.5 Anonymous method conversions 257
Anecdote: Mono widget design improves C# 257
13.6 Method group conversions 259
Non-orthogonality 259
Through a glass darkly… 260
13.7 Conversions involving nullable types 262
Wrapping conversions 262
13.7.1 Null type conversions 262
13.7.2 Nullable conversions 262
Nullable “identity” conversions 263
13.7.3 Lifted conversions 263
The same, but different 263
Chapter 14: Expressions 264
14.1 Expression classifications 264
14.1.1 Values of expressions 265
14.2 Operators 265
Operand order of evaluation 266
14.2.1 Operator precedence and associativity 266
Erratum and clarification 267
14.2.2 Operator overloading 267
14.2.3 Unary operator overload resolution 268
14.2.4 Binary operator overload resolution 269
14.2.5 Candidate user-defined operators 269
14.2.6 Numeric promotions 270
Land of surprises 270
Into the void 272
Loosely literal 272
Reasons for numeric promotion 272
14.2.6.1 Unary numeric promotions 273
14.2.6.2 Binary numeric promotions 273
14.2.7 Lifted operators 274
Null values in C# and in SQL 275
14.3 Member lookup 276
14.3.1 Base types 277
14.4 Function members 278
14.4.1 Argument lists 280
Unrealistic optimism 281
Hidden (small) costs of parameter arrays 282
String formatting not part of the Standard 282
14.4.2 Overload resolution 283
Overload resolution: C# vs. CIL 284
14.4.2.1 Applicable function member 284
14.4.2.2 Better function member 285
14.4.2.3 Better conversion 286
Lost digits 286
Better conversion pitfall 286
14.4.3 Function member invocation 287
No boxing 289
14.4.3.1 Invocations on boxed instances 289
From diaphanous to concrete 290
But wait, there is more… 290
14.5 Primary expressions 290
14.5.1 Literals 291
14.5.2 Simple names 291
14.5.2.1 Invariant meaning in blocks 293
Simpler confusion 294
14.5.3 Parenthesized expressions 294
14.5.4 Member access 295
14.5.4.1 Identical simple names and type names 296
14.5.5 Invocation expressions 297
14.5.5.1 Method invocations 297
What is not considered during overload resolution? 298
Sometimes there is no right answer… 299
14.5.5.2 Delegate invocations 301
Raising events and thread safety 301
14.5.6 Element access 302
14.5.6.1 Array access 303
Array indexing corner case 303
Red Katipo 304
14.5.6.2 Indexer access 304
14.5.7 This access 305
this = that? 306
14.5.8 Base access 306
Base access is nonvirtual 306
No override bypass 307
14.5.9 Postfix increment and decrement operators 308
Overflow eventually… 308
14.5.10 The new operator 309
New for old? 310
14.5.10.1 Object creation expressions 310
14.5.10.2 Array creation expressions 312
Java difference 313
14.5.10.3 Delegate creation expressions 314
Name equivalence and conversions 315
Creating a delegate from a delegate 318
Breaking change 319
14.5.11 The typeof operator 320
typeof(Generic< ,>
14.5.12 The sizeof operator 323
Marshal.SizeOf != sizeof 324
14.5.13 The checked and unchecked operators 324
Checked expression pitfall 325
(Un)checked constants 325
The meaning of (un)checked 328
14.5.14 Default value expression 328
Historical aside 328
14.5.15 Anonymous methods 329
Breaking the ties that bind… 329
Anonymous sharing 329
14.5.15.1 Anonymous method signatures 330
14.5.15.2 Anonymous method blocks 330
14.5.15.3 Outer variables 331
14.5.15.3.1 Captured outer variables 331
Captured local variables: modification and lifetime 332
Captured variable pitfall 332
Caution: space leaks 333
Visitor-style enumeration 333
14.5.15.3.2 Instantiation of local variables 335
Caution: local variables shared between threads 338
Caution: implementation-dependent space leaks 338
14.5.15.4 Anonymous method evaluation 340
Interlude: call-by-name 341
14.5.15.5 Implementation example 344
Anonymous methods, generic parameters, and a bug 347
14.6 Unary expressions 348
14.6.1 Unary plus operator 348
14.6.2 Unary minus operator 348
14.6.3 Logical negation operator 349
14.6.4 Bitwise complement operator 350
14.6.5 Prefix increment and decrement operators 350
Obscure C++ difference 350
14.6.6 Cast expressions 351
14.7 Arithmetic operators 352
Void arithmetic 352
Operator to instruction/method mapping not defined 352
14.7.1 Multiplication operator 353
14.7.2 Division operator 354
Divided we stand 354
A touch of Pentium 356
14.7.3 Remainder operator 357
Remainder latitude 357
IEC remainder 358
Further latitude 358
14.7.4 Addition operator 359
Hidden boxing 360
Implicitly avoiding the boxing 361
String concatenation efficiency 362
14.7.5 Subtraction operator 363
Negation, not negative 364
Legacy coding style 366
14.8 Shift operators 366
Negative shifts 367
Out of bounds shift count behavior? 368
Zero-extension signed shift 369
14.9 Relational and type-testing operators 369
Incomparable 369
All references are equal, but some are more equal than others 370
The invisible contract 373
Equality for all 374
Exploiting the built-in equality on value types 375
14.9.1 Integer comparison operators 375
14.9.2 Floating-point comparison operators 376
NaNs may be equal 377
14.9.3 Decimal comparison operators 377
14.9.4 Boolean equality operators 378
14.9.5 Enumeration comparison operators 378
14.9.6 Reference type equality operators 378
Specification mistake 379
Boxing not required 379
14.9.7 String equality operators 381
14.9.8 Delegate equality operators 381
Creating unique invocation list items 382
14.9.9 Equality operators and nullable types 383
14.9.10 is operator 383
14.9.11 as operator 384
is and as ignore user-defined conversions 385
14.10 Logical operators 386
14.10.1 Integer logical operators 386
Extra bits 387
Bit buckets 387
14.10.2 Enumeration logical operators 387
14.10.3 Boolean logical operators 388
Forgotten “long-circuiting” operators 388
14.10.4 The bool? logical operators 388
14.11 Conditional logical operators 389
14.11.1 Boolean conditional logical operators 389
14.11.2 User-defined conditional logical operators 390
true/false overloading 390
14.12 The null coalescing operator 391
Subtle interaction with implicit conversions 391
14.13 Conditional operator 393
14.14 Assignment operators 393
14.14.1 Simple assignment 394
14.14.2 Compound assignment 396
14.14.3 Event assignment 397
14.15 Expression 397
14.16 Constant expressions 397
Expressions can be constant 398
Compile time constant evaluation 399
Divide by zero always throws 399
14.17 Boolean expressions 400
Chapter 15: Statements 401
15.1 End points and reachability 401
Flow-analysis vs. unsigned types 402
15.2 Blocks 403
15.2.1 Statement lists 404
15.3 The empty statement 404
15.4 Labeled statements 405
15.5 Declaration statements 405
15.5.1 Local variable declarations 406
15.5.2 Local constant declarations 406
15.6 Expression statements 407
15.7 Selection statements 408
15.7.1 The if statement 408
15.7.2 The switch statement 409
Switching on a string 409
Switching on nullables… 410
Et tu, Mono? 411
Independent, within limits 413
Switch section reordering 415
Bad switch in a bad switch 415
15.8 Iteration statements 415
15.8.1 The while statement 415
15.8.2 The do statement 416
15.8.3 The for statement 416
Iteration count not fixed 417
15.8.4 The foreach statement 418
GetEnumerator() selection 419
Why is the iteration variable read-only? 420
Subtle change due to anonymous methods 420
Implications of MoveNext() 422
Iteration variable: read-only != readonly 422
15.9 Jump statements 423
15.9.1 The break statement 424
15.9.2 The continue statement 425
15.9.3 The goto statement 425
15.9.4 The return statement 427
15.9.5 The throw statement 427
null ! NullReferenceException 428
Plug-in style exception handling 428
Spot the difference 429
15.10 The try statement 430
Possible security vulnerability 431
Variety is the spice of life 432
Why is the exception variable name optional? 432
Rationale: Why is jumping out of a finally block forbidden? 433
Unstoppable exceptions? 434
Sometimes other code runs before finally 435
15.11 The checked and unchecked statements 436
Nested un/checking? 437
15.12 The lock statement 437
Lock thread safety? 438
Lock safety 439
15.13 The using statement 439
Rationale: designing for nonmemory resources 440
Redux: One implementation’s treasure is another’s garbage 443
15.14 The yield statement 444
yield syntax? 446
Chapter 16: Namespaces 447
16.1 Compilation units 447
Compilation-unit misnomer 447
Class libraries 448
16.2 Namespace declarations 448
16.3 Extern alias directives 449
Extern alias implied flexibility 450
16.4 Using directives 450
16.4.1 Using alias directives 451
Using alias constraints 455
Using alias reordering 456
16.4.2 Using namespace directives 456
Ouch, it bit me! 458
Intercepting calls 460
16.5 Namespace members 461
16.6 Type declarations 461
16.7 Qualified alias member 462
Chapter 17: Classes 465
17.1 Class declarations 465
Trailing semicolons 465
17.1.1 Class modifiers 465
17.1.1.1 Abstract classes 466
abstract + override == redundant? 467
abstract + override != redundant 467
17.1.1.2 Sealed classes 468
17.1.1.3 Static classes 468
Some bugs lead to features 469
17.1.2 Class base specification 470
Positional inheritance notation 470
17.1.2.1 Base classes 471
17.1.2.2 Interface implementations 472
17.1.3 Class body 473
17.1.4 Partial declarations 473
Divide and confuse… 473
But sometimes useful 474
17.2 Class members 476
Minor Java difference 477
17.2.1 Inheritance 479
17.2.2 The new modifier 480
17.2.3 Access modifiers 480
Absent modifier != default modifier 480
17.2.4 Constituent types 481
17.2.5 Static and instance members 481
No access to static members through instance expressions 481
Rationale: instance method call semantics 482
17.2.6 Nested types 484
Delegates and enums are types too 484
17.2.6.1 Fully qualified name 485
17.2.6.2 Declared accessibility 485
17.2.6.3 Hiding 485
17.2.6.4 this access 486
Simulating Java-style inner classes 487
17.2.6.5 Access to private and protected members of the containing type 488
Access clarification 489
Older C++ difference 489
17.2.7 Reserved member names 490
17.2.7.1 Member names reserved for properties 490
Neutral, but not agnostic 490
The meaning of “reserved” 491
17.2.7.2 Member names reserved for events 492
17.2.7.3 Member names reserved for indexers 492
Indexer names on CLI 492
17.2.7.4 Member names reserved for finalizers 493
17.3 Constants 493
Constant references do not trigger static initialization 493
Having nothing might be useful 494
17.4 Fields 495
17.4.1 Static and instance fields 497
17.4.2 Readonly fields 497
Readonly yet modifiable 497
17.4.2.1 Using static readonly fields for constants 497
17.4.2.2 Versioning of constants and static readonly fields 498
17.4.3 Volatile fields 498
Evolution of volatile 499
Unrealistic optimism redux 500
17.4.4 Field initialization 501
17.4.5 Variable initializers 501
17.4.5.1 Static field initialization 502
Static fields may never be initialized 504
17.4.5.2 Instance field initialization 504
Rationale: why field initializers cannot reference instance fields 505
Unable to create inner objects 505
17.5 Methods 505
Why no throw specifications? 507
17.5.1 Method parameters 508
17.5.1.1 Value parameters 509
17.5.1.2 Reference parameters 510
A round peg in a square hole… 510
17.5.1.3 Output parameters 512
17.5.1.4 Parameter arrays 513
Params arrays can be null 514
Is the params keyword needed? 516
17.5.2 Static and instance methods 517
17.5.3 Virtual methods 517
Instance methods are nonvirtual by default 518
Non-virtual calling of virtual methods 519
Invocation by compile-time and run-time type 521
17.5.4 Override methods 521
17.5.5 Sealed methods 523
17.5.6 Abstract methods 524
17.5.7 External methods 525
17.5.8 Method body 526
Foreign bodies 526
17.5.9 Method overloading 527
17.6 Properties 527
A property cannot be a ref or out parameter 528
The accessors of a property cannot be named 528
17.6.1 Static and instance properties 529
17.6.2 Accessors 529
More foreign bodies 530
Value hiding in an accessor 530
The most frequently requested new feature 538
17.6.3 Virtual, sealed, override, and abstract accessors 538
17.7 Events 540
Externally, events are write-only 541
17.7.1 Field-like events 542
Do-nothing delegate 543
Event –= vs. += 544
Non-thread-safe struct events 547
17.7.2 Event accessors 548
Custom event handler semantics 550
17.7.3 Static and instance events 550
17.7.4 Virtual, sealed, override, and abstract accessors 550
17.8 Indexers 551
No static indexers 552
Named indexers 555
17.8.1 Indexer overloading 556
17.9 Operators 556
The best laid plans of mice and men… 557
Bedfellows: operators and structs 558
17.9.1 Unary operators 558
17.9.2 Binary operators 559
17.9.3 Conversion operators 560
Indirect implicit conversion to an interface 561
17.10 Instance constructors 562
17.10.1 Constructor initializers 563
Infinite recursion not erroneous 564
17.10.2 Instance variable initializers 564
17.10.3 Constructor execution 565
Rationale: field initialization before base constructor invocation 567
17.10.4 Default constructors 568
17.10.5 Private constructors 568
17.10.6 Optional instance constructor parameters 569
17.11 Static constructors 569
No access specifier no parameter
Trigger one, trigger all 570
Constants are not “referenced” 571
Which static constructor is triggered? 571
Indeterminately deterministic 574
17.12 Finalizers 575
C++ destructors versus C# finalizers 575
Thwarting a finalizer 576
Thwarting a finalizer on the CLI 577
Finalizer issues 578
Final deadlock 578
Erroneous details 579
Chapter 18: Structs 580
Freedom of allocation 580
“Heap” allocation required 580
Union City blues 581
Premature optimization 582
Danger, UXB! 583
18.1 Struct declarations 584
18.1.1 Struct modifiers 585
18.1.2 Struct interfaces 585
18.1.3 Struct body 585
18.2 Struct members 585
18.3 Class and struct differences 586
readonly value-type fields 586
18.3.1 Value semantics 587
Struct layout cycles 588
Struct equality contracts 589
18.3.2 Inheritance 590
18.3.3 Assignment 590
18.3.4 Default values 590
Why do structs always have a default constructor? 591
18.3.5 Boxing and unboxing 592
18.3.6 Meaning of this 592
18.3.7 Field initializers 592
18.3.8 Constructors 593
Definite assignment inside a struct constructor 594
Non–dynamic allocation using new 594
18.3.9 Finalizers 594
Why no ~S() in struct type S? 594
18.3.10 Static constructors 594
Static constructor roulette 595
Chapter 19: Arrays 597
19.1 Array types 597
19.1.1 The System.Array type 598
19.2 Array creation 598
19.3 Array element access 598
foreach: simpler and faster 598
19.4 Array members 599
Arrays and vectors on the CLI 599
19.5 Array covariance 600
19.6 Arrays and the generic IList interface 601
19.7 Array initializers 601
Brace depth must equal array dimension 603
Chapter 20: Interfaces 604
20.1 Interface declarations 604
Partial interfaces? 604
20.1.1 Interface modifiers 604
20.1.2 Base interfaces 605
Different inheritance 606
20.1.3 Interface body 606
20.2 Interface members 606
20.2.1 Interface methods 608
20.2.2 Interface properties 608
20.2.3 Interface events 608
20.2.4 Interface indexers 609
20.2.5 Interface member access 609
20.3 Fully qualified interface member names 611
20.4 Interface implementations 612
Interfaces implemented through the base class 612
20.4.1 Explicit interface member implementations 613
Excess accessors not allowed in explicit interface member implementation 614
Simulating return type covariance 615
params oddity 616
20.4.2 Interface mapping 616
Morphing non-virtual methods 618
Inaccurate assertion 619
20.4.3 Interface implementation inheritance 621
Erratum? 621
Meaning of ‘‘implements’’ 621
20.4.4 Interface re-implementation 623
Original implementation inaccessible 624
20.4.5 Abstract classes and interfaces 626
Abstract class interface alternatives 626
Chapter 21: Enums 629
Enums are integral 629
21.1 Enum declarations 630
Why no char underlying type? 631
21.2 Enum modifiers 631
21.3 Enum members 631
Second-class citizens 632
Type of an enum member 632
enum alternatives 634
21.4 The System.Enum type 635
21.5 Enum values and operations 636
Operations on enums 636
Chapter 22: Delegates 637
22.1 Delegate declarations 637
22.2 Delegate instantiation 639
Creation does not trigger static initialization 640
Delegates for value type instance methods 641
The C# pimpernel… 642
22.3 Delegate invocation 642
Chapter 23: Exceptions 645
Exceptions not advised as control flow 645
23.1 Causes of exceptions 645
Asynchronous exceptions 646
Unexpected exception 646
23.2 The System.Exception class 646
Optional InnerException 646
A good idea in theory, but not in practice 647
23.3 How exceptions are handled 647
Here be dragons 648
23.4 Common exception classes 648
Genteel exception classes? 649
Chapter 24: Attributes 650
24.1 Attribute classes 650
Suffix omission clarification 650
24.1.1 Attribute usage 651
24.1.2 Positional and named parameters 652
24.1.3 Attribute parameter types 653
24.2 Attribute specification 653
return vs. @return 655
Specification omission 658
Multiple declaration unclarity 659
24.3 Attribute instances 660
24.3.1 Compilation of an attribute 660
Constructor not called at compile-time 661
24.3.2 Run-time retrieval of an attribute instance 661
Reflection not part of C# standard 662
24.4 Reserved attributes 662
24.4.1 The AttributeUsage attribute 662
AttributeUsage and partial compile-time checks 662
24.4.2 The Conditional attribute 663
24.4.2.1 Conditional methods 663
Derived surprises 663
24.4.2.2 Conditional attribute classes 667
24.4.3 The Obsolete attribute 668
What error parameter? 668
Really obsolete 668
Chapter 25: Generics 670
Not separate, just pragmatic 670
C# generics compared to Ada, C++, Haskell, Java 1.5… 670
25.1 Generic class declarations 672
No type parameters in constructor declarations, but type arguments in constructor invocations 672
Overloading on number of type parameters 673
25.1.1 Type parameters 673
Null comparison despite no conversion 674
25.1.2 The instance type 675
25.1.3 Members of generic classes 675
25.1.4 Static fields in generic classes 676
Non-identical twins 676
25.1.5 Static constructors in generic classes 677
25.1.6 Accessing protected members 678
25.1.7 Overloading in generic classes 678
Clarifications 679
25.1.8 Parameter array methods and type parameters 679
25.1.9 Overriding and generic classes 680
25.1.10 Operators in generic classes 680
No operators on constructed type 681
Ignored ambiguity 682
25.1.11 Nested types in generic classes 683
Thinking generically: containers 684
25.2 Generic struct declarations 686
Infinite chains of constructed types 686
25.3 Generic interface declarations 687
25.3.1 Uniqueness of implemented interfaces 687
25.3.2 Explicit interface member implementations 688
25.4 Generic delegate declarations 689
Generic function types 690
25.5 Constructed types 690
25.5.1 Type arguments 691
25.5.2 Open and closed types 692
25.5.3 Base classes and interfaces of a constructed type 692
25.5.4 Members of a constructed type 693
25.5.5 Accessibility of a constructed type 694
25.5.6 Conversions 694
25.5.7 Using alias directives 695
Aliases do not take type parameters 695
25.6 Generic methods 695
25.6.1 Generic method signatures 696
25.6.2 Virtual generic methods 696
25.6.3 Calling generic methods 698
Polymorphic recursion 699
25.6.4 Inference of type arguments 699
Specification bug 701
Parameter arrays 701
Limited inference 702
25.6.5 Using a generic method with a delegate 702
From generic method to delegate 702
25.6.6 No generic properties, events, indexers, operators, constructors, or finalizers 703
Rationale: no generic constructors, operators, properties, or indexers 703
25.7 Constraints 705
No nullable constraint 706
Using constraints to simulate Java wildcard types 707
Ineffective base class? 709
No “numeric” constraints 711
25.7.1 Satisfying constraints 713
25.7.2 Member lookup on type parameters 714
25.7.3 Type parameters and boxing 714
No cast, no box 716
Inherited constraints 717
25.7.4 Conversions involving type parameters 717
Thinking generically: algorithms 718
Chapter 26: Iterators 721
Growing pains 721
Coroutines allowed? 722
26.1 Iterator blocks 722
26.1.1 Enumerator interfaces 723
26.1.2 Enumerable interfaces 723
26.1.3 Yield type 723
26.1.4 This access 723
How many iterators does it take to enumerate? 723
Enumerating a data structure: iterators, handwritten enumerator, or visitor style? 725
26.2 Enumerator objects 728
Iterators, finally, lock and using 728
Obscure iterator and finally/lock/using interaction 729
26.2.1 The MoveNext method 730
Implicit yield break at end of iterator block 731
26.2.2 The Current property 731
26.2.3 The Dispose method 731
26.3 Enumerable objects 732
26.3.1 The GetEnumerator method 732
26.4 Implementation example 733
Anecdote: iterator blocks, generic parameters, and a bug 741
Chapter 27: Unsafe code 742
27.1 Unsafe contexts 742
27.2 Pointer types 745
Missing grammar rule 746
Volatile pointers 747
A confusing C# vs. C/C++ syntax difference 747
27.3 Fixed and moveable variables 748
27.4 Pointer conversions 749
27.5 Pointers in expressions 750
27.5.1 Pointer indirection 751
27.5.2 Pointer member access 751
27.5.3 Pointer element access 752
27.5.4 The address-of operator 753
No pointer-to-volatile 754
27.5.5 Pointer increment and decrement 754
27.5.6 Pointer arithmetic 755
27.5.7 Pointer comparison 756
27.5.8 The sizeof operator 756
27.6 The fixed statement 757
27.7 Stack allocation 760
The programmer does not always come first 762
27.8 Dynamic memory allocation 762
A: Grammar 765
A.1 Lexical grammar 765
A.1.1 Line terminators 765
A.1.2 White space 765
A.1.3 Comments 766
A.1.4 Tokens 766
A.1.5 Unicode escape sequences 767
A.1.6 Identifiers 767
A.1.7 Keywords 768
A.1.8 Literals 768
A.1.9 Operators and punctuators 770
A.1.10 Pre-processing directives 771
A.2 Syntactic grammar 773
A.2.1 Basic concepts 773
A.2.2 Types 774
A.2.3 Variables 775
A.2.4 Expressions 775
A.2.5 Statements 779
A.2.6 Classes 784
A.2.7 Structs 791
A.2.8 Arrays 791
A.2.9 Interfaces 792
A.2.10 Enums 793
A.2.11 Delegates 794
A.2.12 Attributes 794
A.2.13 Generics 795
A.3 Grammar extensions for unsafe code 797
B: Portability issues 800
B.1 Undefined behavior 800
B.2 Implementation-defined behavior 800
B.3 Unspecified behavior 801
B.4 Other Issues 801
C: Naming Guidelines 802
Naming guideline summary 802
D: Standard Library 803
E: Documentation Comments 814
E.1 Introduction 814
E.2 Recommended tags 815
E.2.1 < c>
E.2.2 < code>
E.2.3 < example>
E.2.4 < exception>
E.2.5 < list>
E.2.6 < para>
E.2.7 < param>
E.2.8 < paramref>
E.2.9 < permission>
E.2.10 < remarks>
E.2.11 < returns>
E.2.12 < see>
E.2.13 < seealso>
E.2.14 < summary>
E.2.15 < typeparam>
E.2.16 < typeparamref>
E.2.17 < value>
E.3 Processing the documentation file 824
E.3.1 ID string format 825
E.3.2 ID string examples 826
E.4 An example 830
E.4.1 C# source code 830
E.4.2 Resulting XML 833
F: Bibliography 837
Index 838
Erscheint lt. Verlag | 27.7.2010 |
---|---|
Sprache | englisch |
Themenwelt | Sachbuch/Ratgeber |
Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge | |
ISBN-10 | 0-08-055053-3 / 0080550533 |
ISBN-13 | 978-0-08-055053-4 / 9780080550534 |
Haben Sie eine Frage zum Produkt? |
![PDF](/img/icon_pdf_big.jpg)
Größe: 3,9 MB
Kopierschutz: Adobe-DRM
Adobe-DRM ist ein Kopierschutz, der das eBook vor Mißbrauch schützen soll. Dabei wird das eBook bereits beim Download auf Ihre persönliche Adobe-ID autorisiert. Lesen können Sie das eBook dann nur auf den Geräten, welche ebenfalls auf Ihre Adobe-ID registriert sind.
Details zum Adobe-DRM
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen eine
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen eine
Geräteliste und zusätzliche Hinweise
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich