HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This PDF will be accessible from your Account page after purchase and requires PDF reading software, such as Acrobat® Reader®.
The eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
“Welcome to one of the greatest collaborations you could dream of in the world of C# books—and probably far beyond!”
—From the Foreword by Mads Torgersen, C# Program Manager, Microsoft
Essential C# 6.0 is a well-organized, no-fluff guide to the latest versions of C# for programmers at all levels of experience. Fully updated to reflect new C# 6.0 and .NET 4.6 features and patterns, it will help you write C# code that’s simple, powerful, robust, secure, and maintainable.
This book’s authors are world-class C# experts: long-time Microsoft MVP and Regional Director Mark Michaelis and Eric Lippert, formerly principal developer on Microsoft’s C# compiler team. Together, they cover the entire language, illustrating key constructs with succinct examples and offering a complete foundation for successful C# development.
Essential C# 6.0 makes it easy to program with any version of C#, whether you’re creating new code or maintaining existing systems. Separate indexes for C# versions 4, 5, and 6 help you quickly find version-specific answers with accompanying visual indicators that help you identify which language innovations will work when. This edition also includes a set of best-practice C# Coding Guidelines updated to leverage C# 6.0 constructs.
Coverage includes
For Qualified Instructors
An instructor’s guide, exercises, and a slide deck are available to support your courses.
Figures xv
Tables xvii
Foreword xix
Preface xxiii
Acknowledgments xxxv
About the Authors xxxvii
Chapter 1: Introducing C# 1
Hello, World 2
C# Syntax Fundamentals 4
Console Input and Output 18
Chapter 2: Data Types 35
Fundamental Numeric Types 36
More Fundamental Types 45
null and void 58
Categories of Types 61
Nullable Modifier 64
Conversions between Data Types 65
Arrays 71
Chapter 3: Operators and Control Flow 89
Operators 90
Introducing Flow Control 107
Code Blocks ({}) 114
Code Blocks, Scopes, and Declaration Spaces 116
Boolean Expressions 118
Bitwise Operators (<<, >>, |, &, ^, ~) 128
Control Flow Statements, Continued 134
Jump Statements 146
C# Preprocessor Directives 152
Chapter 4: Methods and Parameters 161
Calling a Method 162
Declaring a Method 169
The using Directive 175
Returns and Parameters on Main() 180
Advanced Method Parameters 183
Recursion 192
Method Overloading 194
Optional Parameters 197
Basic Error Handling with Exceptions 202
Chapter 5: Classes 217
Declaring and Instantiating a Class 221
Instance Fields 225
Instance Methods 227
Using the this Keyword 228
Access Modifiers 235
Properties 237
Constructors 254
Static Members 265
Extension Methods 275
Encapsulating the Data 277
Nested Classes 281
Partial Classes 284
Chapter 6: Inheritance 289
Derivation 290
Overriding the Base Class 302
Abstract Classes 314
All Classes Derive from System.Object 320
Verifying the Underlying Type with the is Operator 321
Conversion Using the as Operator 322
Chapter 7: Interfaces 325
Introducing Interfaces 326
Polymorphism through Interfaces 327
Interface Implementation 332
Converting between the Implementing Class and Its Interfaces 338
Interface Inheritance 338
Multiple Interface Inheritance 341
Extension Methods on Interfaces 341
Implementing Multiple Inheritance via Interfaces 343
Versioning 346
Interfaces Compared with Classes 347
Interfaces Compared with Attributes 349
Chapter 8: Value Types 351
Structs 355
Boxing 362
Enums 371
Chapter 9: Well-Formed Types 383
Overriding object Members 383
Operator Overloading 395
Referencing Other Assemblies 403
Defining Namespaces 409
XML Comments 413
Garbage Collection 418
Resource Cleanup 421
Lazy Initialization 429
Chapter 10: Exception Handling 433
Multiple Exception Types 433
Catching Exceptions 436
General Catch Block 440
Guidelines for Exception Handling 443
Defining Custom Exceptions 446
Rethrowing a Wrapped Exception 449
Chapter 11: Generics 455
C# without Generics 456
Introducing Generic Types 461
Constraints 473
Generic Methods 486
Covariance and Contravariance 491
Generic Internals 498
Chapter 12: Delegates and Lambda Expressions 505
Introducing Delegates 506
Lambda Expressions 516
Anonymous Methods 522
General-Purpose Delegates: System.Func and System.Action 524
Chapter 13: Events 543
Coding the Observer Pattern with Multicast Delegates 544
Events 558
Chapter 14: Collection Interfaces with Standard Query Operators 571
Anonymous Types and Implicitly Typed Local Variables 572
Collection Initializers 578
What Makes a Class a Collection: IEnumerable<T> 582
Standard Query Operators 588
Chapter 15: LINQ with Query Expressions 621
Introducing Query Expressions 622
Query Expressions Are Just Method Invocations 640
Chapter 16: Building Custom Collections 643
More Collection Interfaces 644
Primary Collection Classes 646
Providing an Indexer 663
Returning Null or an Empty Collection 666
Iterators 667
Chapter 17: Reflection, Attributes, and Dynamic Programming 683
Reflection 684
nameof Operator 694
Attributes 696
Programming with Dynamic Objects 719
Chapter 18: Multithreading 731
Multithreading Basics 734
Working with System.Threading 741
Asynchronous Tasks 749
Canceling a Task 768
The Task-Based Asynchronous Pattern 775
Executing Loop Iterations in Parallel 798
Running LINQ Queries in Parallel 809
Chapter 19: Thread Synchronization 815
Why Synchronization? 817
Timers 845
Chapter 20: Platform Interoperability and Unsafe Code 849
Platform Invoke 850
Pointers and Addresses 862
Executing Unsafe Code via a Delegate 872
Using the Windows Runtime Libraries from C# 873
Chapter 21: The Common Language Infrastructure 877
Defining the Common Language Infrastructure 878
CLI Implementations 879
C# Compilation to Machine Code 879
Runtime 883
Application Domains 888
Assemblies, Manifests, and Modules 888
Common Intermediate Language 891
Common Type System 892
Common Language Specification 893
Base Class Library 893
Metadata 894
Appendix A: Downloading and Installing the C# Compiler and CLI Platform 897
Microsoft’s .NET 897
Appendix B: Tic-Tac-Toe Source Code Listing 903
Index 909
Index of 6.0 Topics 953
Index of 5.0 Topics 955
Index of 4.0 Topics 959
Index of 3.0 Topics 965