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. • Use structured programming constructs to write functioning code immediately • Learn both the complexities and solutions to nullable reference types • Thoroughly master C# object constructs, including classes, inheritance, and interfaces • Reduce code redundancy with generics, delegates, and lambda expressions • Take full advantage of collections with LINQ • Improve multithreading with the taskbased async pattern and asynchronous streams • Enhance performance through the parallel processing of data and multithreading tasks • Make the most of refl ection, attributes, and the declarative programming paradigm • Program complex types with enhanced pattern matching syntax • Write succinct type defi nitions with record structs and classes • Explore the new features of C# 8.0-C# 12.0 Cover Title Page Copyright Page Contents at a Glance Contents Foreword Preface Acknowledgments About the Author 1 Introducing C# Hello, World C# Syntax Fundamentals Working with Variables Console Input and Output Managed Execution and the Common Language Infrastructure Multiple .NET Frameworks Summary 2 Data Types Type Name Forms Fundamental Numeric Types More Fundamental Types Conversions between Data Types Summary 3 More with Data Types Categories of Types Declaring Types That Allow null Implicitly Typed Local Variables Tuples Arrays Summary 4 Operators and Control Flow Operators Introducing Flow Control Code Blocks ({}) Code Blocks, Scopes, and Declaration Spaces Boolean Expressions Programming with null Bitwise Operators (< >, |, &, ^, ~) Control Flow Statements, Continued Jump Statements C# Preprocessor Directives Summary 5 Parameters and Methods Calling a Method Declaring a Method Local Functions Using Directives Returns and Parameters on Main Method Top-Level Statements Advanced Method Parameters Recursion Method Overloading Optional Parameters Basic Error Handling with Exceptions Summary 6 Classes Declaring and Instantiating a Class Instance Fields Instance Methods Using the this Keyword Access Modifiers Properties Constructors Non-Nullable Reference Type Properties with Constructors Nullable Attributes Deconstructors Static Members Extension Methods Encapsulating the Data Nested Classes Partial Classes Summary 7 Inheritance Derivation Overriding the Base Class Abstract Classes All Classes Derive from System.Object Type Checking Pattern Matching Avoid Pattern Matching When Polymorphism Is Possible Summary 8 Interfaces Introducing Interfaces Polymorphism through Interfaces Interface Implementation Converting between the Implementing Class and Its Interfaces Interface Inheritance Multiple Interface Inheritance Extension Methods on Interfaces Versioning Extension Methods versus Default Interface Members Interfaces Compared with Abstract Classes Interfaces Compared with Attributes Summary 9 Introducing Structs and Records Reference Equality versus Value Equality Structs Record Classes Record Class Inheritance Records Overriding object Members Customizing Record Behavior Boxing Enums Summary 10 Well-Formed Types Operator Overloading Referencing Other Assemblies Encapsulation of Types Defining Namespaces XML Comments Garbage Collection and Weak References Resource Cleanup Lazy Initialization Summary 11 Exception Handling Multiple Exception Types Catching Exceptions Rethrowing an Existing Exception General Catch Block Guidelines for Exception Handling Defining Custom Exceptions Rethrowing a Wrapped Exception Summary 12 Generics C# without Generics Introducing Generic Types Constraints Generic Methods Covariance and Contravariance Generic Internals Summary 13 Delegates and Lambda Expressions Introducing Delegates Declaring Delegate Types Lambda Expressions Statement Lambdas Expression Lambdas Anonymous Methods Delegates Do Not Have Structural Equality Outer Variables Static Anonymous Functions Expression Trees Summary 14 Events Coding the Publish–Subscribe Pattern with Multicast Delegates Understanding Events Summary 15 Collection Interfaces with Standard Query Operators Collection Initializers What Makes a Class a Collection: IEnumerable Standard Query Operators Anonymous Types with LINQ Summary 16 LINQ with Query Expressions Introducing Query Expressions Query Expressions Are Just Method Invocations Summary 17 Building Custom Collections More Collection Interfaces Primary Collection Classes Providing an Indexer Returning null or an Empty Collection Iterators Summary 18 Reflection, Attributes, and Dynamic Programming Reflection nameof Operator Attributes Programming with Dynamic Objects Summary 19 Introducing Multithreading Multithreading Basics Asynchronous Tasks Canceling a Task Working with System.Threading Summary 20 Programming the Task-Based Asynchronous Pattern Synchronously Invoking a High-Latency Operation Asynchronously Invoking a High-Latency Operation Using the TPL The Task-Based Asynchronous Pattern with async and await Introducing Asynchronous Return of ValueTask Asynchronous Streams IAsyncDisposable and the await using Declaration and Statement Using LINQ with IAsyncEnumerable Returning void from an Asynchronous Method Asynchronous Lambdas and Local Functions Task Schedulers and the Synchronization Context async/await with the Windows UI Summary 21 Iterating in Parallel Executing Loop Iterations in Parallel Running LINQ Queries in Parallel Summary 22 Thread Synchronization Why Synchronization? Timers Summary 23 Platform Interoperability and Unsafe Code Platform Invoke Pointers and Addresses Executing Unsafe Code via a Delegate Summary 24 The Common Language Infrastructure Defining the Common Language Infrastructure CLI Implementations NET Standard Base Class Library C# Compilation to Machine Code Runtime Assemblies, Manifests, and Modules Common Intermediate Language Common Type System Common Language Specification Metadata .NET Native and Ahead of Time Compilation Summary Index A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Index of 8.0 Topics A B C D I N P R S T U Index of 9.0 Topics F I P R S T Index of 10.0 Topics A C E F G I R Index of 11.0 Topics A E F G I L N P R U Index of 12.0 Topics A D I P 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, including any-type aliases, inline arrays, default lambda expression parameters, expanded support for primary constructors, and experimental interceptors. Michaelis presents a comprehensive tutorial and reference for the entire C# 12 language, helping you accelerate your journey to expert-level C# programmer. Succinct examples illustrate central 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 indexes of recent C# versions, and icons and notes identifying when each innovation was introduced.