C# in Depth [4th ed.]
Book information
Description
Contents......Page 3 Foreword......Page 12 Preface......Page 14 About the Book......Page 15 --- C# in Context......Page 19 1.1 An evolving language......Page 20 1.1.1 A helpful type system at large and small scales......Page 21 1.1.2 Ever more concise code......Page 23 1.1.3 Simple data access with LINQ......Page 26 1.1.4 Asynchrony......Page 27 1.1.5 Balancing efficiency and complexity......Page 28 1.1.6 Evolution at speed: Using minor versions......Page 29 1.2 An evolving platform......Page 30 1.3 An evolving community......Page 31 1.4 An evolving book......Page 32 1.4.2 Examples using Noda Time......Page 33 1.4.3 Terminology choices......Page 34 Summary......Page 35 --- C# 2-5......Page 36 2 C# 2......Page 38 2.1.1 Introduction by example: Collections before generics......Page 39 2.1.2 Generics save the day......Page 42 2.1.3 What can be generic?......Page 46 2.1.4 Type inference for type arguments to methods......Page 47 2.1.5 Type constraints......Page 49 2.1.6 The default and typeof operators......Page 51 2.1.7 Generic type initialization and state......Page 54 2.2 Nullable value types......Page 55 2.2.1 Aim: Expressing an absence of information......Page 56 2.2.2 CLR and framework support: The Nullable struct......Page 57 2.2.3 Language support......Page 60 2.3 Simplified delegate creation......Page 66 2.3.2 Anonymous methods......Page 67 2.3.3 Delegate compatibility......Page 69 2.4 Iterators......Page 70 2.4.1 Introduction to iterators......Page 71 2.4.2 Lazy execution......Page 72 2.4.3 Evaluation of yield statements......Page 73 2.4.4 The importance of being lazy......Page 74 2.4.5 Evaluation of finally blocks......Page 75 2.4.6 The importance of finally handling......Page 78 2.4.7 Implementation sketch......Page 79 2.5 Minor features......Page 83 2.5.1 Partial types......Page 84 2.5.3 Separate getter/setter access for properties......Page 86 2.5.4 Namespace aliases......Page 87 2.5.5 Pragma directives......Page 89 2.5.7 InternalsVisibleTo......Page 90 Summary......Page 91 3 C# 3 - LINQ & everything with it......Page 92 3.1 Automatically implemented properties......Page 93 3.2.1 Typing terminology......Page 94 3.2.2 Implicitly typed local variables (var)......Page 95 3.2.3 Implicitly typed arrays......Page 96 3.3.1 Introduction to object and collection initializers......Page 98 3.3.2 Object initializers......Page 100 3.3.3 Collection initializers......Page 101 3.4.1 Syntax and basic behavior......Page 103 3.4.2 The compiler-generated type......Page 106 3.4.3 Limitations......Page 107 3.5 Lambda expressions......Page 108 3.5.1 Lambda expression syntax......Page 109 3.5.2 Capturing variables......Page 111 3.5.3 Expression trees......Page 118 3.6.1 Declaring an extension method......Page 120 3.6.2 Invoking an extension method......Page 121 3.6.3 Chaining method calls......Page 123 3.7 Query expressions......Page 124 3.7.2 Range variables and transparent identifiers......Page 125 3.7.3 Deciding when to use which syntax for LINQ......Page 127 Summary......Page 128 4 C# 4 - Improving interoperability......Page 130 4.1.1 Introduction to dynamic typing......Page 131 4.1.2 Dynamic behavior beyond reflection......Page 136 4.1.3 A brief look behind the scenes......Page 141 4.1.4 Limitations and surprises in dynamic typing......Page 144 4.1.5 Usage suggestions......Page 148 4.2 Optional parameters & named arguments......Page 150 4.2.1 Parameters with default values and arguments with names......Page 151 4.2.2 Determining the meaning of a method call......Page 152 4.2.3 Impact on versioning......Page 154 4.3 COM interoperability improvements......Page 155 4.3.1 Linking primary interop assemblies......Page 156 4.3.2 Optional parameters in COM......Page 157 4.3.3 Named indexers......Page 159 4.4.1 Simple examples of variance in action......Page 160 4.4.2 Syntax for variance in interface and delegate declarations......Page 161 4.4.3 Restrictions on using variance......Page 162 4.4.4 Generic variance in practice......Page 164 Summary......Page 166 5 Asynchronous Code......Page 167 5.1.1 First encounters of the asynchronous kind......Page 169 5.1.2 Breaking down the first example......Page 171 5.2.1 Fundamentals of asynchronous execution......Page 172 5.2.2 Synchronization contexts......Page 174 5.2.3 Modeling asynchronous methods......Page 175 5.3 Async method declarations......Page 177 5.3.1 Return types from async methods......Page 178 5.4 Await expressions......Page 179 5.4.1 The awaitable pattern......Page 180 5.4.2 Restrictions on await expressions......Page 182 5.5 Wrapping of return values......Page 183 5.6.1 What is awaited and when?......Page 185 5.6.2 Evaluation of await expressions......Page 186 5.6.3 The use of awaitable pattern members......Page 190 5.6.4 Exception unwrapping......Page 191 5.6.5 Method completion......Page 193 5.7 Asynchronous anonymous functions......Page 197 5.8.1 The 99.9% case: ValueTask......Page 199 5.8.2 The 0.1% case: Building your own custom task type......Page 201 5.9 Async main methods in C# 7.1......Page 203 5.10.1 Avoid context capture by using ConfigureAwait (where appropriate)......Page 204 5.10.2 Enable parallelism by starting multiple independent tasks......Page 206 5.10.4 Allow cancellation wherever possible......Page 207 5.10.5 Testing asynchrony......Page 208 Summary......Page 209 6 Async Implementation......Page 210 6.1 Structure of the generated code......Page 212 6.1.1 The stub method: Preparation and taking the first step......Page 215 6.1.2 Structure of the state machine......Page 216 6.1.3 The MoveNext() method (high level)......Page 219 6.1.4 The SetStateMachine method and the state machine boxing dance......Page 221 6.2.1 A full concrete example......Page 222 6.2.2 MoveNext() method general structure......Page 224 6.2.3 Zooming into an await expression......Page 226 6.3 How control flow affects MoveNext()......Page 227 6.3.1 Control flow between await expressions is simple......Page 228 6.3.2 Awaiting within a loop......Page 229 6.3.3 Awaiting within a try/finally block......Page 230 6.4 Execution contexts and flow......Page 233 6.5 Custom task types revisited......Page 235 Summary......Page 236 7.1 Capturing variables in foreach loops......Page 237 7.2.1 Basic behavior......Page 239 7.2.3 Simplifying INotifyPropertyChanged implementations......Page 241 7.2.4 Corner cases of caller information attributes......Page 243 Summary......Page 249 --- C# 6......Page 250 8 Super-sleek Properties & Expression-bodied Members......Page 251 8.1 A brief history of properties......Page 252 8.2.1 Read-only automatically implemented properties......Page 254 8.2.2 Initializing automatically implemented properties......Page 255 8.2.3 Automatically implemented properties in structs......Page 256 8.3.1 Even simpler read-only computed properties......Page 258 8.3.2 Expression-bodied methods, indexers, and operators......Page 261 8.3.3 Restrictions on expression-bodied members in C# 6......Page 263 8.3.4 Guidelines for using expression-bodied members......Page 265 Summary......Page 267 9 Stringy Features......Page 268 9.1.2 Custom formatting with format strings......Page 269 9.1.3 Localization......Page 271 9.2.1 Simple interpolation......Page 274 9.2.3 Interpolated verbatim string literals......Page 275 9.3 Localization using FormattableString......Page 277 9.3.1 Compiler handling of interpolated string literals (part 2)......Page 278 9.3.2 Formatting a FormattableString in a specific culture......Page 279 9.3.3 Other uses for FormattableString......Page 281 9.3.4 Using FormattableString with older versions of .NET......Page 284 9.4.1 Developers and machines, but maybe not end users......Page 286 9.4.2 Hard limitations of interpolated string literals......Page 288 9.4.3 When you can but really shouldn?t......Page 289 9.5.1 First examples of nameof......Page 291 9.5.2 Common uses of nameof......Page 293 9.5.3 Tricks and traps when using nameof......Page 296 Summary......Page 299 10.1 Using static directives......Page 300 10.1.1 Importing static members......Page 301 10.1.2 Extension methods and using static......Page 304 10.2 Object and collection initializer enhancements......Page 306 10.2.1 Indexers in object initializers......Page 307 10.2.2 Using extension methods in collection initializers......Page 310 10.2.3 Test code vs. production code......Page 314 10.3.1 Simple and safe property dereferencing......Page 315 10.3.2 The null conditional operator in more detail......Page 316 10.3.3 Handling Boolean comparisons......Page 317 10.3.4 Indexers and the null conditional operator......Page 318 10.3.5 Working effectively with the null conditional operator......Page 319 10.4 Exception filters......Page 321 10.4.1 Syntax and semantics of exception filters......Page 322 10.4.2 Retrying operations......Page 327 10.4.3 Logging as a side effect......Page 328 10.4.4 Individual, case-specific exception filters......Page 329 10.4.5 Why not just throw?......Page 330 Summary......Page 331 --- C# 7 & beyond......Page 332 11 Composition using Tuples......Page 334 11.1 Introduction to tuples......Page 335 11.2.1 Syntax......Page 336 11.2.2 Inferred element names for tuple literals (C# 7.1)......Page 338 11.2.3 Tuples as bags of variables......Page 339 11.3.1 Types of tuple literals......Page 344 11.3.2 Conversions from tuple literals to tuple types......Page 345 11.3.3 Conversions between tuple types......Page 349 11.3.5 Element name checking in inheritance......Page 351 11.3.6 Equality and inequality operators (C# 7.3)......Page 352 11.4.1 Introducing System.ValueTuple......Page 353 11.4.2 Element name handling......Page 354 11.4.4 String representations of tuples......Page 356 11.4.5 Regular equality and ordering comparisons......Page 357 11.4.6 Structural equality and ordering comparisons......Page 358 11.4.7 Womples and large tuples......Page 360 11.5 Alternatives to tuples......Page 361 11.5.2 Anonymous types......Page 362 11.6.1 Nonpublic APIs and easily changed code......Page 363 11.6.2 Local variables......Page 364 11.6.3 Fields......Page 365 11.6.4 Tuples and dynamic don?t play together nicely......Page 366 Summary......Page 367 12 Deconstruction & Pattern Matching......Page 368 12.1 Deconstruction of tuples......Page 369 12.1.1 Deconstruction to new variables......Page 370 12.1.2 Deconstruction assignments to existing variables and properties......Page 372 12.2 Deconstruction of nontuple types......Page 376 12.2.1 Instance deconstruction methods......Page 377 12.2.2 Extension deconstruction methods and overloading......Page 378 12.2.3 Compiler handling of Deconstruct calls......Page 379 12.3 Introduction to pattern matching......Page 380 12.4.1 Constant patterns......Page 382 12.4.2 Type patterns......Page 383 12.4.3 The var pattern......Page 386 12.5 Using patterns with the is operator......Page 387 12.6 Using patterns with switch statements......Page 389 12.6.1 Guard clauses......Page 390 12.6.2 Pattern variable scope for case labels......Page 391 12.6.3 Evaluation order of pattern-based switch statements......Page 392 12.7.1 Spotting deconstruction opportunities......Page 394 Summary......Page 395 13 Efficiency with Pass by Reference......Page 396 13.1 Recap: What do you know about ref?......Page 397 13.2.1 Ref locals......Page 400 13.2.2 Ref returns......Page 405 13.2.3 The conditional ?: operator and ref values (C# 7.2)......Page 407 13.2.4 Ref readonly (C# 7.2)......Page 408 13.3 in parameters (C# 7.2)......Page 410 13.3.1 Compatibility considerations......Page 411 13.3.2 The surprising mutability of in parameters: External changes......Page 412 13.3.3 Overloading with in parameters......Page 413 13.3.4 Guidance for in parameters......Page 414 13.4.1 Background: Implicit copying with read-only variables......Page 416 13.4.2 The readonly modifier for structs......Page 418 13.4.3 XML serialization is implicitly read-write......Page 419 13.5.1 Using ref/in parameters in extension methods to avoid copying......Page 420 13.5.2 Restrictions on ref and in extension methods......Page 422 13.6 Ref-like structs (C# 7.2)......Page 423 13.6.1 Rules for ref-like structs......Page 424 13.6.2 Span and stackalloc......Page 425 Summary......Page 429 14.1 Local methods......Page 430 14.1.1 Variable access within local methods......Page 432 14.1.2 Local method implementations......Page 435 14.1.3 Usage guidelines......Page 440 14.2.1 Inline variable declarations for out parameters......Page 442 14.2.2 Restrictions lifted in C# 7.3 for out variables and pattern variables......Page 443 14.3.1 Binary integer literals......Page 444 14.3.2 Underscore separators......Page 445 14.4 Throw expressions......Page 446 14.5 Default literals (C# 7.1)......Page 447 14.6 Nontrailing named arguments (C# 7.2)......Page 448 14.8.1 Generic type constraints......Page 450 14.8.2 Overload resolution improvements......Page 451 Summary......Page 452 15 C# 8 & beyond......Page 454 15.1.1 What problem do nullable reference types solve?......Page 455 15.1.2 Changing the meaning when using reference types......Page 456 15.1.3 Enter nullable reference types......Page 457 15.1.4 Nullable reference types at compile time and execution time......Page 458 15.1.5 The damn it or bang operator......Page 460 15.1.6 Experiences of nullable reference type migration......Page 462 15.1.7 Future improvements......Page 464 15.2 Switch expressions......Page 468 15.3.1 Matching properties in patterns......Page 470 15.3.2 Deconstruction patterns......Page 471 15.3.3 Omitting types from patterns......Page 472 15.4.1 Index and Range types and literals......Page 473 15.4.2 Applying indexes and ranges......Page 474 15.5.1 Asynchronous resource disposal with using await......Page 476 15.5.2 Asynchronous iteration with foreach await......Page 477 15.5.3 Asynchronous iterators......Page 480 15.6.1 Default interface methods......Page 481 15.6.2 Record types......Page 483 15.6.3 Even more features in brief......Page 484 15.7 Getting involved......Page 485 Conclusion......Page 486 Language Features by Version......Page 487 Index......Page 493
Similar books
C# in Depth
2008 · PDF
C# od podszewki
2012 · PDF
Software Mistakes and Tradoffs
2022 · MOBI
Software Mistakes and Tradeoffs: How to make good programming decisions
2022 · EPUB
C# in Depth
Software Mistakes and Tradeoffs: How to make good programming decisions
2021 · PDF
Groovy in Action
2015 · PDF
C# in Depth
2019 · PDF