C# in depth

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

C# in Depth, Third Edition updates the best-selling second edition to cover the new features of C# 5, including the challenges of writing maintainable asynchronous code. It preserves the uniquely insightful look into the tricky areas and dusty corners of C# that only expert Jon Skeet can provide.
About this Book
If youre a .NET developer, youll use C# whether youre building an advanced enterprise application or just slamming out a quick app. In C# 5, you can do amazing things with generics, lambda expressions, dynamic typing, LINQ, iterator blocks, and other features. But first you have to learn it in depth.
C# in Depth, Third Edition has been thoroughly revised to cover the new features of C# 5, including the subtleties of writing maintainable asynchronous code. Youll see the power of C# in action, learning how to work with high-value features that youll be glad to have in your toolkit. And youll learn to avoid hidden pitfalls of C# programming with the help of crystal clear explanations of «behind the scenes» issues.
This book assumes youve digested your first C# book and are hungry for more!

Author(s): Jon Skeet
Edition: 3
Publisher: Manning Publications
Year: 2013

Language: English
Pages: 616
Tags: Библиотека;Компьютерная литература;C#;

C# in Depth......Page 1
brief contents......Page 8
contents......Page 10
foreword......Page 20
preface......Page 22
acknowledgments......Page 23
Who should read this book?......Page 25
Roadmap......Page 26
Terminology, typography, and downloads......Page 28
Author Online and the C# in Depth website......Page 29
about the author......Page 30
about the cover illustration......Page 31
Part 1 Preparing for the journey......Page 32
1 The changing face of C# development......Page 34
1.1 Starting with a simple data type......Page 35
1.1.1 The Product type in C# 1......Page 36
1.1.2 Strongly typed collections in C# 2......Page 37
1.1.3 Automatically implemented properties in C# 3......Page 38
1.1.4 Named arguments in C# 4......Page 39
1.2.1 Sorting products by name......Page 40
1.2.2 Querying collections......Page 43
1.3.1 Representing an unknown price......Page 45
1.4 Introducing LINQ......Page 47
1.4.1 Query expressions and in-process queries......Page 48
1.4.2 Querying XML......Page 49
1.4.3 LINQ to SQL......Page 50
1.5.1 Simplifying COM interoperability......Page 51
1.5.2 Interoperating with a dynamic language......Page 52
1.6 Writing asynchronous code without the heartache......Page 53
1.7 Dissecting the .NET platform......Page 54
1.7.3 Framework libraries......Page 55
1.8.1 Presenting full programs as snippets......Page 56
1.8.2 Didactic code isn’t production code......Page 57
1.8.3 Your new best friend: the language specification......Page 58
1.9 Summary......Page 59
2 Core foundations: building on C# 1......Page 60
2.1.1 A recipe for simple delegates......Page 61
2.1.2 Combining and removing delegates......Page 66
2.1.3 A brief diversion into events......Page 67
2.1.4 Summary of delegates......Page 68
2.2.1 C#’s place in the world of type systems......Page 69
2.2.2 When is C# 1’s type system not rich enough?......Page 72
2.3 Value types and reference types......Page 75
2.3.1 Values and references in the real world......Page 76
2.3.2 Value and reference type fundamentals......Page 77
2.3.3 Dispelling myths......Page 78
2.3.4 Boxing and unboxing......Page 80
2.3.5 Summary of value types and reference types......Page 81
2.4.1 Features related to delegates......Page 82
2.4.2 Features related to the type system......Page 84
2.4.3 Features related to value types......Page 86
2.5 Summary......Page 87
Part 2 C# 2: Solving the issues of C# 1......Page 88
3 Parameterized typing with generics......Page 90
3.1 Why generics are necessary......Page 91
3.2.1 Learning by example: a generic dictionary......Page 93
3.2.2 Generic types and type parameters......Page 95
3.2.3 Generic methods and reading generic declarations......Page 98
3.3 Beyond the basics......Page 101
3.3.1 Type constraints......Page 102
3.3.2 Type inference for type arguments of generic methods......Page 107
3.3.3 Implementing generics......Page 108
3.4 Advanced generics......Page 114
3.4.1 Static fields and static constructors......Page 115
3.4.2 How the JIT compiler handles generics......Page 116
3.4.3 Generic iteration......Page 118
3.4.4 Reflection and generics......Page 121
3.5.1 Lack of generic variance......Page 125
3.5.2 Lack of operator constraints or a “numeric” constraint......Page 130
3.5.4 Comparison with C++ templates......Page 132
3.5.5 Comparison with Java generics......Page 134
3.6 Summary......Page 135
4 Saying nothing with nullable types......Page 136
4.1.1 Why value type variables can’t be null......Page 137
4.1.2 Patterns for representing null values in C# 1......Page 138
4.2.1 Introducing Nullable......Page 140
4.2.2 Boxing Nullable and unboxing......Page 143
4.2.3 Equality of Nullable instances......Page 144
4.3 C# 2’s syntactic sugar for nullable types......Page 145
4.3.1 The ? modifier......Page 146
4.3.2 Assigning and comparing with null......Page 147
4.3.3 Nullable conversions and operators......Page 149
4.3.4 Nullable logic......Page 152
4.3.6 The null coalescing operator......Page 154
4.4 Novel uses of nullable types......Page 157
4.4.1 Trying an operation without using output parameters......Page 158
4.4.2 Painless comparisons with the null coalescing operator......Page 160
4.5 Summary......Page 162
5 Fast-tracked delegates......Page 164
5.1 Saying goodbye to awkward delegate syntax......Page 165
5.2 Method group conversions......Page 167
5.3 Covariance and contravariance......Page 168
5.3.1 Contravariance for delegate parameters......Page 169
5.3.2 Covariance of delegate return types......Page 170
5.3.3 A small risk of incompatibility......Page 172
5.4.1 Starting simply: acting on a parameter......Page 173
5.4.2 Returning values from anonymous methods......Page 176
5.4.3 Ignoring delegate parameters......Page 177
5.5.1 Defining closures and different types of variables......Page 179
5.5.2 Examining the behavior of captured variables......Page 180
5.5.3 What’s the point of captured variables?......Page 182
5.5.4 The extended lifetime of captured variables......Page 183
5.5.5 Local variable instantiations......Page 184
5.5.6 Mixtures of shared and distinct variables......Page 186
5.5.7 Captured variable guidelines and summary......Page 187
5.6 Summary......Page 189
6 Implementing iterators the easy way......Page 190
6.1 C# 1: The pain of handwritten iterators......Page 191
6.2.1 Introducing iterator blocks and yield return......Page 194
6.2.2 Visualizing an iterator’s workflow......Page 196
6.2.3 Advanced iterator execution flow......Page 198
6.2.4 Quirks in the implementation......Page 201
6.3.1 Iterating over the dates in a timetable......Page 203
6.3.2 Iterating over lines in a file......Page 204
6.3.3 Filtering items lazily using an iterator block and a predicate......Page 207
6.4 Pseudo-synchronous code with the Concurrency and Coordination Runtime......Page 209
6.5 Summary......Page 211
7 Concluding C# 2: the final features......Page 213
7.1 Partial types......Page 214
7.1.1 Creating a type with multiple files......Page 215
7.1.2 Uses of partial types......Page 217
7.1.3 Partial methods—C# 3 only!......Page 219
7.2 Static classes......Page 221
7.3 Separate getter/setter property access......Page 223
7.4 Namespace aliases......Page 224
7.4.1 Qualifying namespace aliases......Page 225
7.4.2 The global namespace alias......Page 226
7.4.3 Extern aliases......Page 227
7.5.1 Warning pragmas......Page 228
7.5.2 Checksum pragmas......Page 229
7.6 Fixed-size buffers in unsafe code......Page 230
7.7.1 Friend assemblies in the simple case......Page 232
7.7.2 Why use InternalsVisibleTo?......Page 233
7.7.3 InternalsVisibleTo and signed assemblies......Page 234
7.8 Summary......Page 235
Part 3 C# 3: Revolutionizing data access......Page 236
8 Cutting fluff with a smart compiler......Page 238
8.1 Automatically implemented properties......Page 239
8.2.1 Using var to declare a local variable......Page 242
8.2.2 Restrictions on implicit typing......Page 244
8.2.3 Pros and cons of implicit typing......Page 245
8.2.4 Recommendations......Page 246
8.3.1 Defining some sample types......Page 247
8.3.2 Setting simple properties......Page 248
8.3.3 Setting properties on embedded objects......Page 250
8.3.4 Collection initializers......Page 251
8.3.5 Uses of initialization features......Page 254
8.4 Implicitly typed arrays......Page 255
8.5.1 First encounters of the anonymous kind......Page 256
8.5.2 Members of anonymous types......Page 258
8.5.3 Projection initializers......Page 259
8.5.4 What’s the point?......Page 260
8.6 Summary......Page 262
9 Lambda expressions and expression trees......Page 263
9.1.1 Preliminaries: Introducing the Func<…> delegate types......Page 265
9.1.2 First transformation to a lambda expression......Page 266
9.1.4 Implicitly typed parameter lists......Page 267
9.1.5 Shortcut for a single parameter......Page 268
9.2.1 Filtering, sorting, and actions on lists......Page 269
9.2.2 Logging in an event handler......Page 271
9.3 Expression trees......Page 272
9.3.1 Building expression trees programmatically......Page 273
9.3.2 Compiling expression trees into delegates......Page 274
9.3.3 Converting C# lambda expressions to expression trees......Page 275
9.3.4 Expression trees at the heart of LINQ......Page 279
9.3.5 Expression trees beyond LINQ......Page 280
9.4 Changes to type inference and overload resolution......Page 282
9.4.1 Reasons for change: streamlining generic method calls......Page 283
9.4.2 Inferred return types of anonymous functions......Page 284
9.4.3 Two-phase type inference......Page 285
9.4.4 Picking the right overloaded method......Page 289
9.5 Summary......Page 291
10 Extension methods......Page 293
10.1 Life before extension methods......Page 294
10.2.1 Declaring extension methods......Page 296
10.2.2 Calling extension methods......Page 298
10.2.3 Extension method discovery......Page 299
10.2.4 Calling a method on a null reference......Page 300
10.3.1 First steps with Enumerable......Page 302
10.3.2 Filtering with Where and chaining method calls together......Page 304
10.3.3 Interlude: haven’t we seen the Where method before?......Page 306
10.3.4 Projections using the Select method and anonymous types......Page 307
10.3.5 Sorting using the OrderBy method......Page 308
10.3.6 Business examples involving chaining......Page 309
10.4.2 Fluent interfaces......Page 311
10.4.3 Using extension methods sensibly......Page 313
10.5 Summary......Page 315
11 Query expressions and LINQ to Objects......Page 316
11.1.1 Fundamental concepts in LINQ......Page 317
11.1.2 Defining the sample data model......Page 322
11.2 Simple beginnings: selecting elements......Page 323
11.2.2 Compiler translations as the basis of query expressions......Page 324
11.2.3 Range variables and nontrivial projections......Page 327
11.2.4 Cast, OfType, and explicitly typed range variables......Page 329
11.3.1 Filtering using a where clause......Page 331
11.3.2 Degenerate query expressions......Page 332
11.3.3 Ordering using an orderby clause......Page 333
11.4 Let clauses and transparent identifiers......Page 335
11.4.1 Introducing an intermediate computation with let......Page 336
11.4.2 Transparent identifiers......Page 337
11.5.1 Inner joins using join clauses......Page 338
11.5.2 Group joins with join...into clauses......Page 342
11.5.3 Cross joins and flattening sequences using multiple from clauses......Page 345
11.6.1 Grouping with the group...by clause......Page 349
11.6.2 Query continuations......Page 352
11.7.1 Operations that require dot notation......Page 355
11.7.3 Where query expressions shine......Page 356
11.8 Summary......Page 357
12 LINQ beyond collections......Page 359
12.1 Querying a database with LINQ to SQL......Page 360
12.1.1 Getting started: the database and model......Page 361
12.1.2 Initial queries......Page 363
12.1.3 Queries involving joins......Page 365
12.2 Translations using IQueryable and IQueryProvider......Page 367
12.2.1 Introducing IQueryable and related interfaces......Page 368
12.2.2 Faking it: interface implementations to log calls......Page 369
12.2.3 Gluing expressions together: the Queryable extension methods......Page 372
12.2.4 The fake query provider in action......Page 373
12.3 LINQ-friendly APIs and LINQ to XML......Page 375
12.3.1 Core types in LINQ to XML......Page 376
12.3.2 Declarative construction......Page 378
12.3.3 Queries on single nodes......Page 380
12.3.4 Flattened query operators......Page 382
12.3.5 Working in harmony with LINQ......Page 383
12.4.1 Plotting the Mandelbrot set with a single thread......Page 384
12.4.2 Introducing ParallelEnumerable, ParallelQuery, and AsParallel......Page 385
12.4.3 Tweaking parallel queries......Page 387
12.5 Inverting the query model with LINQ to Rx......Page 388
12.5.1 IObservable and IObserver......Page 389
12.5.3 Querying observables......Page 391
12.5.4 What’s the point?......Page 394
12.6.1 Design and implementation guidelines......Page 395
12.6.2 Sample extension: selecting a random element......Page 396
12.7 Summary......Page 398
Part 4 C# 4: Playing nicely with others......Page 400
13 Minor changes to simplify code......Page 402
13.1.1 Optional parameters......Page 403
13.1.2 Named arguments......Page 409
13.1.3 Putting the two together......Page 413
13.2.1 The horrors of automating Word before C# 4......Page 418
13.2.2 The revenge of optional parameters and named arguments......Page 419
13.2.3 When is a ref parameter not a ref parameter?......Page 420
13.2.4 Calling named indexers......Page 421
13.2.5 Linking primary interop assemblies......Page 422
13.3.1 Types of variance: covariance and contravariance......Page 425
13.3.2 Using variance in interfaces......Page 427
13.3.4 Complex situations......Page 430
13.3.5 Restrictions and notes......Page 432
13.4.1 Robust locking......Page 436
13.4.2 Changes to field-like events......Page 437
13.5 Summary......Page 438
14 Dynamic binding in a static language......Page 440
14.1.1 What is dynamic typing?......Page 442
14.1.2 When is dynamic typing useful, and why?......Page 443
14.1.3 How does C# 4 provide dynamic typing?......Page 444
14.2 The five-minute guide to dynamic......Page 445
14.3 Examples of dynamic typing......Page 447
14.3.1 COM in general, and Microsoft Office in particular......Page 448
14.3.2 Dynamic languages such as IronPython......Page 450
14.3.3 Dynamic typing in purely managed code......Page 454
14.4.1 Introducing the Dynamic Language Runtime......Page 460
14.4.2 DLR core concepts......Page 462
14.4.3 How the C# compiler handles dynamic......Page 465
14.4.4 The C# compiler gets even smarter......Page 469
14.4.5 Restrictions on dynamic code......Page 472
14.5.1 Using ExpandoObject......Page 475
14.5.2 Using DynamicObject......Page 479
14.5.3 Implementing IDynamicMetaObjectProvider......Page 486
14.6 Summary......Page 490
Part 5 C# 5: Asynchrony made simple......Page 492
15 Asynchrony with async/await......Page 494
15.1.1 First encounters of the asynchronous kind......Page 496
15.1.2 Breaking down the first example......Page 498
15.2.1 Fundamentals of asynchronous execution......Page 499
15.2.2 Modeling asynchronous methods......Page 502
15.3.1 Declaring an async method......Page 503
15.3.2 Return types from async methods......Page 504
15.3.3 The awaitable pattern......Page 505
15.3.4 The flow of await expressions......Page 508
15.3.5 Returning from an async method......Page 512
15.3.6 Exceptions......Page 513
15.4 Asynchronous anonymous functions......Page 521
15.5 Implementation details: compiler transformation......Page 523
15.5.1 Overview of the generated code......Page 524
15.5.2 Structure of the skeleton method......Page 526
15.5.3 Structure of the state machine......Page 528
15.5.4 One entry point to rule them all......Page 529
15.5.5 Control around await expressions......Page 531
15.5.6 Keeping track of a stack......Page 532
15.6 Using async/await effectively......Page 534
15.6.1 The task-based asynchronous pattern......Page 535
15.6.2 Composing async operations......Page 538
15.6.3 Unit testing asynchronous code......Page 542
15.6.4 The awaitable pattern redux......Page 546
15.6.5 Asynchronous operations in WinRT......Page 547
15.7 Summary......Page 548
16 C# 5 bonus features and closing thoughts......Page 550
16.2 Caller information attributes......Page 551
16.2.1 Basic behavior......Page 552
16.2.2 Logging......Page 553
16.2.3 Implementing INotifyPropertyChanged......Page 554
16.2.4 Using caller information attributes without .NET 4.5......Page 555
16.3 Closing thoughts......Page 556
A.1 Aggregation......Page 558
A.2 Concatenation......Page 559
A.3 Conversion......Page 560
A.4 Element operators......Page 562
A.6 Generation......Page 563
A.7 Grouping......Page 564
A.8 Joins......Page 565
A.9 Partitioning......Page 566
A.10 Projection......Page 567
A.11 Quantifiers......Page 568
A.13 Set-based operators......Page 569
A.14 Sorting......Page 570
B.1 Interfaces......Page 571
B.2.1 List......Page 573
B.2.2 Arrays......Page 574
B.2.3 LinkedList......Page 575
B.2.4 Collection, BindingList, ObservableCollection, and KeyedCollection......Page 576
B.3.1 Dictionary......Page 577
B.3.2 SortedList and SortedDictionary......Page 578
B.4.1 HashSet......Page 579
B.5 Queue and Stack......Page 580
B.6.1 IProducerConsumerCollection and BlockingCollection......Page 581
B.6.3 ConcurrentDictionary......Page 582
B.7 Read-only interfaces (.NET 4.5)......Page 583
B.8 Summary......Page 584
C.1 Desktop framework major releases......Page 585
C.2.2 C# 3.0......Page 586
C.3.1 .NET 2.0......Page 587
C.3.3 .NET 3.5......Page 588
C.4 Runtime (CLR) features......Page 589
C.4.2 CLR 4.0......Page 590
C.5.2 Silverlight......Page 591
C.5.4 Windows Runtime (WinRT)......Page 592
C.6 Summary......Page 593
A......Page 594
C......Page 595
D......Page 598
E......Page 600
G......Page 601
H......Page 602
I......Page 603
L......Page 604
N......Page 606
O......Page 607
Q......Page 608
S......Page 609
T......Page 610
U......Page 611
W......Page 612
Z......Page 613