SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
The Comprehensive, Expert Guide to C# 12.0 for Programmers at All Levels
Updated for the Microsoft C# 12.0 Long Term Support (LTS) release, Essential C# 12.0 is a well-organized, no-fluff C# guide, suitable for every programmer. Building on the proven, high-value content of previous editions, world-class C# expert Mark Michaelis illuminates key enhancements in C# 12.0, including any-type aliases, inline arrays, default lambda expression parameters, and expanded support for primary constructors.
Michaelis presents a comprehensive tutorial and reference for the entire C# language, helping you accelerate your journey to expert-level C# programmer. Succinct examples illustrate core constructs, and modern coding guidelines help you minimize bugs and write code that's easier to evolve. To help you quickly find what you need and maintain compatibility, the book includes version-specific icons and notes identifying when each innovation was introduced.
"Welcome to one of the most venerable and trusted franchises you could dream of in the world of C# book--and probably far beyond!"
--From the Foreword by Mads Torgersen, C# Lead Designer, Microsoft
Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.
Download the sample pages (includes Chapter 5)
Foreword xv
Preface xvii
Acknowledgments xxxi
About the Author xxxiii
Chapter 1: Introducing C# 1
Hello, World 2
C# Syntax Fundamentals 12
Working with Variables 24
Console Input and Output 28
Managed Execution and the Common Language Infrastructure 38
Multiple .NET Frameworks 44
Summary 48
Chapter 2: Data Types 49
Type Name Forms 50
Fundamental Numeric Types 52
More Fundamental Types 63
Conversions between Data Types 84
Summary 92
Chapter 3: More with Data Types 93
Categories of Types 93
Declaring Types That Allow null 96
Implicitly Typed Local Variables 102
Tuples 103
Arrays 112
Summary 134
Chapter 4: Operators and Control Flow 137
Operators 137
Introducing Flow Control 156
Code Blocks ({}) 161
Code Blocks, Scopes, and Declaration Spaces 164
Boolean Expressions 166
Programming with null 173
Bitwise Operators (<<, >>, |, &, ^, ~) 181
Control Flow Statements, Continued 187
Jump Statements 200
C# Preprocessor Directives 206
Summary 215
Chapter 5: Parameters and Methods 217
Calling a Method 218
Declaring a Method 225
Local Functions 232
Using Directives 233
Returns and Parameters on Main Method 242
Top-Level Statements 246
Advanced Method Parameters 247
Recursion 261
Method Overloading 264
Optional Parameters 267
Basic Error Handling with Exceptions 272
Summary 291
Chapter 6: Classes 293
Declaring and Instantiating a Class 298
Instance Fields 302
Instance Methods 305
Using the this Keyword 306
Access Modifiers 314
Properties 316
Constructors 333
Non-Nullable Reference Type Properties with Constructors 346
Nullable Attributes 354
Deconstructors 357
Static Members 359
Extension Methods 370
Encapsulating the Data 372
Nested Classes 376
Partial Classes 379
Summary 384
Chapter 7: Inheritance 385
Derivation 386
Overriding the Base Class 397
Abstract Classes 410
All Classes Derive from System.Object 417
Type Checking 419
Pattern Matching 423
Avoid Pattern Matching When Polymorphism Is Possible 438
Summary 440
Chapter 8: Interfaces 443
Introducing Interfaces 444
Polymorphism through Interfaces 446
Interface Implementation 451
Converting between the Implementing Class and Its Interfaces 457
Interface Inheritance 458
Multiple Interface Inheritance 461
Extension Methods on Interfaces 461
Versioning 464
Extension Methods versus Default Interface Members 480
Interfaces Compared with Abstract Classes 482
Interfaces Compared with Attributes 484
Summary 484
Chapter 9: Introducing Structs and Records 487
Reference Equality versus Value Equality 493
Structs 494
Record Classes 500
Record Class Inheritance 503
Records 504
Overriding object Members 513
Customizing Record Behavior 521
Boxing 523
Enums 532
Summary 544
Chapter 10: Well-Formed Types 547
Operator Overloading 548
Referencing Other Assemblies 557
Encapsulation of Types 564
Defining Namespaces 567
XML Comments 571
Garbage Collection and Weak References 576
Resource Cleanup 580
Lazy Initialization 596
Summary 598
Chapter 11: Exception Handling 601
Multiple Exception Types 601
Catching Exceptions 604
Rethrowing an Existing Exception 607
General Catch Block 609
Guidelines for Exception Handling 610
Defining Custom Exceptions 614
Rethrowing a Wrapped Exception 618
Summary 622
Chapter 12: Generics 623
C# without Generics 624
Introducing Generic Types 630
Constraints 646
Generic Methods 663
Covariance and Contravariance 669
Generic Internals 676
Summary 681
Chapter 13: Delegates and Lambda Expressions 683
Introducing Delegates 684
Declaring Delegate Types 688
Lambda Expressions 698
Statement Lambdas 699
Expression Lambdas 702
Anonymous Methods 705
Delegates Do Not Have Structural Equality 707
Outer Variables 710
Static Anonymous Functions 712
Expression Trees 716
Summary 724
Chapter 14: Events 727
Coding the PublishSubscribe Pattern with Multicast Delegates 728
Understanding Events 743
Summary 753
Chapter 15: Collection Interfaces with Standard Query Operators 755
Collection Initializers 756
What Makes a Class a Collection: IEnumerable 759
Standard Query Operators 766
Anonymous Types with LINQ 796
Summary 806
Chapter 16: LINQ with Query Expressions 809
Introducing Query Expressions 810
Query Expressions Are Just Method Invocations 829
Summary 831
Chapter 17: Building Custom Collections 833
More Collection Interfaces 834
Primary Collection Classes 837
Providing an Indexer 859
Returning null or an Empty Collection 862
Iterators 863
Summary 879
Chapter 18: Reflection, Attributes, and Dynamic Programming 881
Reflection 881
nameof Operator 894
Attributes 895
Programming with Dynamic Objects 920
Summary 931
Chapter 19: Introducing Multithreading 933
Multithreading Basics 935
Asynchronous Tasks 943
Canceling a Task 965
Working with System.Threading 972
Summary 973
Chapter 20: Programming the Task-Based Asynchronous Pattern 975
Synchronously Invoking a High-Latency Operation 975
Asynchronously Invoking a High-Latency Operation Using the TPL 979
The Task-Based Asynchronous Pattern with async and await 984
Introducing Asynchronous Return of ValueTask<T> 991
Asynchronous Streams 994
IAsyncDisposable and the await using Declaration and Statement 998
Using LINQ with IAsyncEnumerable 999
Returning void from an Asynchronous Method 1001
Asynchronous Lambdas and Local Functions 1006
Task Schedulers and the Synchronization Context 1013
async/await with the Windows UI 1015
Summary 1019
Chapter 21: Iterating in Parallel 1021
Executing Loop Iterations in Parallel 1021
Running LINQ Queries in Parallel 1032
Summary 1039
Chapter 22: Thread Synchronization 1041
Why Synchronization? 1042
Timers 1073
Summary 1076
Chapter 23: Platform Interoperability and Unsafe Code 1077
Platform Invoke 1078
Pointers and Addresses 1093
Executing Unsafe Code via a Delegate 1104
Summary 1105
Chapter 24: The Common Language Infrastructure 1107
Defining the Common Language Infrastructure 1107
CLI Implementations 1109
.NET Standard 1113
Base Class Library 1113
C# Compilation to Machine Code 1114
Runtime 1116
Assemblies, Manifests, and Modules 1121
Common Intermediate Language 1124
Common Type System 1125
Common Language Specification 1125
Metadata 1126
.NET Native and Ahead of Time Compilation 1127
Summary 1128
Index 1131
Index of 8.0 Topics 1187
Index of 9.0 Topics 1190
Index of 10.0 Topics 1191
Index of 11.0 Topics 1192
Index of 12.0 Topics 1193