C# 12 in a Nutshell: The Definitive Reference

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"

When you have questions about C# 12 or .NET 8, this best-selling guide has the answers you need. C# is a language of unusual flexibility and breadth, and with its continual growth, there's always so much more to learn. In the tradition of O'Reilly's Nutshell guides, this thoroughly updated edition is simply the best one-volume reference to the C# language available today.

Aimed at intermediate and advanced programmers, this is a book whose explanations get straight to the point, covering C#, the CLR, and the core .NET libraries in depth without long intros or bloated samples.

  • Get up to speed on C# from syntax and variables to advanced topics such as pointers, closures, and patterns
  • Dig deep into LINQ, with three chapters dedicated to the topic
  • Explore concurrency and asynchrony, advanced threading, and parallel programming
  • Work with .NET features including regular expressions, networking, assemblies, spans, cryptography, and reflection.emit

Author(s): Joseph Albahari
Edition: 1
Publisher: O'Reilly Media
Year: 2023

Language: English
Commentary: Publisher PDF | Published: November 2023 | 2023-11-14: First Release
Pages: 1083
City: Sebastopol, CA
Tags: C# Programming Language; .NET; Object-Oriented Programming; Lambda Expressions; Events; Collections; LINQ; XML; Concurrency; Networking; Reflection; Metadata; Dynamic Programming; Cryptography; Advanced Threading; Parallel Programming; COM; Regular Expressions

Cover
Copyright
Table of Contents
Preface
Intended Audience
How This Book Is Organized
What You Need to Use This Book
Conventions Used in This Book
Using Code Examples
How to Contact Us
Acknowledgments
Joseph Albahari
Chapter 1. Introducing C# and .NET
Object Orientation
Type Safety
Memory Management
Platform Support
CLRs, BCLs, and Runtimes
Common Language Runtime
Base Class Library
Runtimes
Niche Runtimes
A Brief History of C#
What’s New in C# 12
What’s New in C# 11
What’s New in C# 10
What’s New in C# 9.0
What’s New in C# 8.0
What’s New in C# 7.x
What’s New in C# 6.0
What’s New in C# 5.0
What’s New in C# 4.0
What’s New in C# 3.0
What’s New in C# 2.0
Chapter 2. C# Language Basics
A First C# Program
Compilation
Syntax
Identifiers and Keywords
Literals, Punctuators, and Operators
Comments
Type Basics
Predefined Type Examples
Custom Types
Types and Conversions
Value Types Versus Reference Types
Predefined Type Taxonomy
Numeric Types
Numeric Literals
Numeric Conversions
Arithmetic Operators
Increment and Decrement Operators
Specialized Operations on Integral Types
8- and 16-Bit Integral Types
Special Float and Double Values
double Versus decimal
Real Number Rounding Errors
Boolean Type and Operators
bool Conversions
Equality and Comparison Operators
Conditional Operators
Strings and Characters
Char Conversions
String Type
UTF-8 Strings
Arrays
Default Element Initialization
Indices and Ranges
Multidimensional Arrays
Simplified Array Initialization Expressions
Bounds Checking
Variables and Parameters
The Stack and the Heap
Definite Assignment
Default Values
Parameters
Ref Locals
Ref Returns
var—Implicitly Typed Local Variables
Target-Typed new Expressions
Expressions and Operators
Primary Expressions
Void Expressions
Assignment Expressions
Operator Precedence and Associativity
Operator Table
Null Operators
Null-Coalescing Operator
Null-Coalescing Assignment Operator
Null-Conditional Operator
Statements
Declaration Statements
Expression Statements
Selection Statements
Iteration Statements
Jump Statements
Miscellaneous Statements
Namespaces
File-Scoped Namespaces
The using Directive
The global using Directive
using static
Rules Within a Namespace
Aliasing Types and Namespaces
Advanced Namespace Features
Chapter 3. Creating Types in C#
Classes
Fields
Constants
Methods
Instance Constructors
Deconstructors
Object Initializers
The this Reference
Properties
Indexers
Primary Constructors (C# 12)
Static Constructors
Static Classes
Finalizers
Partial Types and Methods
The nameof operator
Inheritance
Polymorphism
Casting and Reference Conversions
Virtual Function Members
Abstract Classes and Abstract Members
Hiding Inherited Members
Sealing Functions and Classes
The base Keyword
Constructors and Inheritance
Overloading and Resolution
The object Type
Boxing and Unboxing
Static and Runtime Type Checking
The GetType Method and typeof Operator
The ToString Method
Object Member Listing
Structs
Struct Construction Semantics
Read-Only Structs and Functions
Ref Structs
Access Modifiers
Examples
Friend Assemblies
Accessibility Capping
Restrictions on Access Modifiers
Interfaces
Extending an Interface
Explicit Interface Implementation
Implementing Interface Members Virtually
Reimplementing an Interface in a Subclass
Interfaces and Boxing
Default Interface Members
Static Interface Members
Enums
Enum Conversions
Flags Enums
Enum Operators
Type-Safety Issues
Nested Types
Generics
Generic Types
Why Generics Exist
Generic Methods
Declaring Type Parameters
typeof and Unbound Generic Types
The default Generic Value
Generic Constraints
Subclassing Generic Types
Self-Referencing Generic Declarations
Static Data
Type Parameters and Conversions
Covariance
Contravariance
C# Generics Versus C++ Templates
Chapter 4. Advanced C#
Delegates
Writing Plug-in Methods with Delegates
Instance and Static Method Targets
Multicast Delegates
Generic Delegate Types
The Func and Action Delegates
Delegates Versus Interfaces
Delegate Compatibility
Events
Standard Event Pattern
Event Accessors
Event Modifiers
Lambda Expressions
Explicitly Specifying Lambda Parameter and Return Types
Default Lambda Parameters (C# 12)
Capturing Outer Variables
Lambda Expressions Versus Local Methods
Anonymous Methods
try Statements and Exceptions
The catch Clause
The finally Block
Throwing Exceptions
Key Properties of System.Exception
Common Exception Types
The TryXXX Method Pattern
Alternatives to Exceptions
Enumeration and Iterators
Enumeration
Collection Initializers and Collection Expressions
Iterators
Iterator Semantics
Composing Sequences
Nullable Value Types
Nullable Struct
Implicit and Explicit Nullable Conversions
Boxing and Unboxing Nullable Values
Operator Lifting
bool? with & and | Operators
Nullable Value Types and Null Operators
Scenarios for Nullable Value Types
Alternatives to Nullable Value Types
Nullable Reference Types
The Null-Forgiving Operator
Separating the Annotation and Warning Contexts
Treating Nullable Warnings as Errors
Extension Methods
Extension Method Chaining
Ambiguity and Resolution
Anonymous Types
Tuples
Naming Tuple Elements
Aliasing Tuples (C# 12)
ValueTuple.Create
Deconstructing Tuples
Equality Comparison
The System.Tuple classes
Records
Background
Defining a Record
Nondestructive Mutation
Property Validation
Calculated Fields and Lazy Evaluation
Primary Constructors
Records and Equality Comparison
Patterns
Constant Pattern
Relational Patterns
Pattern Combinators
var Pattern
Tuple and Positional Patterns
Property Patterns
List Patterns
Attributes
Attribute Classes
Named and Positional Attribute Parameters
Applying Attributes to Assemblies and Backing Fields
Applying Attributes to Lambda Expressions
Specifying Multiple Attributes
Caller Info Attributes
CallerArgumentExpression
Dynamic Binding
Static Binding Versus Dynamic Binding
Custom Binding
Language Binding
RuntimeBinderException
Runtime Representation of Dynamic
Dynamic Conversions
var Versus dynamic
Dynamic Expressions
Dynamic Calls Without Dynamic Receivers
Static Types in Dynamic Expressions
Uncallable Functions
Operator Overloading
Operator Functions
Overloading Equality and Comparison Operators
Custom Implicit and Explicit Conversions
Overloading true and false
Static Polymorphism
Polymorphic Operators
Generic Math
Unsafe Code and Pointers
Pointer Basics
Unsafe Code
The fixed Statement
The Pointer-to-Member Operator
The stackalloc Keyword
Fixed-Size Buffers
void*
Native-Sized Integers
Function Pointers
[SkipLocalsInit]
Preprocessor Directives
Conditional Attributes
Pragma Warning
XML Documentation
Standard XML Documentation Tags
User-Defined Tags
Type or Member Cross-References
Chapter 5. .NET Overview
Runtime Targets and TFMs
.NET Standard
.NET Standard 2.0
Other .NET Standards
.NET Framework and .NET 8 Compatibility
Reference Assemblies
Runtime and C# Language Versions
The CLR and BCL
System Types
Text Processing
Collections
Querying
XML and JSON
Diagnostics
Concurrency and Asynchrony
Streams and Input/Output
Networking
Assemblies, Reflection, and Attributes
Dynamic Programming
Cryptography
Advanced Threading
Parallel Programming
Span and Memory
Native and COM Interoperability
Regular Expressions
Serialization
The Roslyn Compiler
Application Layers
ASP.NET Core
Windows Desktop
UWP and WinUI 3
MAUI
Chapter 6. .NET Fundamentals
String and Text Handling
Char
String
Comparing Strings
StringBuilder
Text Encodings and Unicode
Dates and Times
TimeSpan
DateTime and DateTimeOffset
DateOnly and TimeOnly
Dates and Time Zones
DateTime and Time Zones
DateTimeOffset and Time Zones
TimeZoneInfo
Daylight Saving Time and DateTime
Formatting and Parsing
ToString and Parse
Format Providers
Standard Format Strings and Parsing Flags
Numeric Format Strings
NumberStyles
Date/Time Format Strings
DateTimeStyles
Enum Format Strings
Other Conversion Mechanisms
Convert
XmlConvert
Type Converters
BitConverter
Globalization
Globalization Checklist
Testing
Working with Numbers
Conversions
Math
BigInteger
Half
Complex
Random
BitOperations
Enums
Enum Conversions
Enumerating Enum Values
How Enums Work
The Guid Struct
Equality Comparison
Value Versus Referential Equality
Standard Equality Protocols
Equality and Custom Types
Order Comparison
IComparable
< and >
Implementing the IComparable Interfaces
Utility Classes
Console
Environment
Process
AppContext
Chapter 7. Collections
Enumeration
IEnumerable and IEnumerator
IEnumerable and IEnumerator
Implementing the Enumeration Interfaces
The ICollection and IList Interfaces
ICollection and ICollection
IList and IList
IReadOnlyCollection and IReadOnlyList
The Array Class
Construction and Indexing
Enumeration
Length and Rank
Searching
Sorting
Reversing Elements
Copying
Converting and Resizing
Lists, Queues, Stacks, and Sets
List and ArrayList
LinkedList
Queue and Queue
Stack and Stack
BitArray
HashSet and SortedSet
Dictionaries
IDictionary
IDictionary
Dictionary and Hashtable
OrderedDictionary
ListDictionary and HybridDictionary
Sorted Dictionaries
Customizable Collections and Proxies
Collection and CollectionBase
KeyedCollection and DictionaryBase
ReadOnlyCollection
Immutable Collections
Creating Immutable Collections
Manipulating Immutable Collections
Builders
Immutable Collections and Performance
Frozen Collections
Plugging in Equality and Order
IEqualityComparer and EqualityComparer
IComparer and Comparer
StringComparer
IStructuralEquatable and IStructuralComparable
Chapter 8. LINQ Queries
Getting Started
Fluent Syntax
Chaining Query Operators
Composing Lambda Expressions
Natural Ordering
Other Operators
Query Expressions
Range Variables
Query Syntax Versus SQL Syntax
Query Syntax Versus Fluent Syntax
Mixed-Syntax Queries
Deferred Execution
Reevaluation
Captured Variables
How Deferred Execution Works
Chaining Decorators
How Queries Are Executed
Subqueries
Subqueries and Deferred Execution
Composition Strategies
Progressive Query Building
The into Keyword
Wrapping Queries
Projection Strategies
Object Initializers
Anonymous Types
The let Keyword
Interpreted Queries
How Interpreted Queries Work
Combining Interpreted and Local Queries
AsEnumerable
EF Core
EF Core Entity Classes
DbContext
Object Tracking
Change Tracking
Navigation Properties
Deferred Execution
Building Query Expressions
Delegates Versus Expression Trees
Expression Trees
Chapter 9. LINQ Operators
Overview
Sequence→Sequence
Sequence→Element or Value
Void→Sequence
Filtering
Where
Take, TakeLast, Skip, SkipLast
TakeWhile and SkipWhile
Distinct and DistinctBy
Projecting
Select
SelectMany
Joining
Join and GroupJoin
The Zip Operator
Ordering
OrderBy, OrderByDescending, ThenBy, ThenByDescending
Grouping
GroupBy
Chunk
Set Operators
Concat, Union, UnionBy
Intersect, Intersect By, Except, and ExceptBy
Conversion Methods
OfType and Cast
ToArray, ToList, ToDictionary, ToHashSet, ToLookup
AsEnumerable and AsQueryable
Element Operators
First, Last, and Single
ElementAt
MinBy and MaxBy
DefaultIfEmpty
Aggregation Methods
Count and LongCount
Min and Max
Sum and Average
Aggregate
Quantifiers
Contains and Any
All and SequenceEqual
Generation Methods
Empty
Range and Repeat
Chapter 10. LINQ to XML
Architectural Overview
What Is a DOM?
The LINQ to XML DOM
X-DOM Overview
Loading and Parsing
Saving and Serializing
Instantiating an X-DOM
Functional Construction
Specifying Content
Automatic Deep Cloning
Navigating and Querying
Child Node Navigation
Parent Navigation
Peer Node Navigation
Attribute Navigation
Updating an X-DOM
Simple Value Updates
Updating Child Nodes and Attributes
Updating Through the Parent
Working with Values
Setting Values
Getting Values
Values and Mixed Content Nodes
Automatic XText Concatenation
Documents and Declarations
XDocument
XML Declarations
Names and Namespaces
Namespaces in XML
Specifying Namespaces in the X-DOM
The X-DOM and Default Namespaces
Prefixes
Annotations
Projecting into an X-DOM
Eliminating Empty Elements
Streaming a Projection
Chapter 11. Other XML and JSON Technologies
XmlReader
Reading Nodes
Reading Elements
Reading Attributes
Namespaces and Prefixes
XmlWriter
Writing Attributes
Writing Other Node Types
Namespaces and Prefixes
Patterns for Using XmlReader/XmlWriter
Working with Hierarchical Data
Mixing XmlReader/XmlWriter with an X-DOM
Working with JSON
Utf8JsonReader
Utf8JsonWriter
JsonDocument
JsonNode
Chapter 12. Disposal and Garbage Collection
IDisposable, Dispose, and Close
Standard Disposal Semantics
When to Dispose
Clearing Fields in Disposal
Anonymous Disposal
Automatic Garbage Collection
Roots
Finalizers
Calling Dispose from a Finalizer
Resurrection
How the GC Works
Optimization Techniques
Forcing Garbage Collection
Tuning Garbage Collection at Runtime
Memory Pressure
Array Pooling
Managed Memory Leaks
Timers
Diagnosing Memory Leaks
Weak References
Weak References and Caching
Weak References and Events
Chapter 13. Diagnostics
Conditional Compilation
Conditional Compilation Versus Static Variable Flags
The Conditional Attribute
Debug and Trace Classes
Fail and Assert
TraceListener
Flushing and Closing Listeners
Debugger Integration
Attaching and Breaking
Debugger Attributes
Processes and Process Threads
Examining Running Processes
Examining Threads in a Process
StackTrace and StackFrame
Windows Event Logs
Writing to the Event Log
Reading the Event Log
Monitoring the Event Log
Performance Counters
Enumerating the Available Counters
Reading Performance Counter Data
Creating Counters and Writing Performance Data
The Stopwatch Class
Cross-Platform Diagnostic Tools
dotnet-counters
dotnet-trace
dotnet-dump
Chapter 14. Concurrency and Asynchrony
Introduction
Threading
Creating a Thread
Join and Sleep
Blocking
Local Versus Shared State
Locking and Thread Safety
Passing Data to a Thread
Exception Handling
Foreground Versus Background Threads
Thread Priority
Signaling
Threading in Rich Client Applications
Synchronization Contexts
The Thread Pool
Tasks
Starting a Task
Returning Values
Exceptions
Continuations
TaskCompletionSource
Task.Delay
Principles of Asynchrony
Synchronous Versus Asynchronous Operations
What Is Asynchronous Programming?
Asynchronous Programming and Continuations
Why Language Support Is Important
Asynchronous Functions in C#
Awaiting
Writing Asynchronous Functions
Asynchronous Lambda Expressions
Asynchronous Streams
Asynchronous Methods in WinRT
Asynchrony and Synchronization Contexts
Optimizations
Asynchronous Patterns
Cancellation
Progress Reporting
The Task-Based Asynchronous Pattern
Task Combinators
Asynchronous Locking
Obsolete Patterns
Asynchronous Programming Model
Event-Based Asynchronous Pattern
BackgroundWorker
Chapter 15. Streams and I/O
Stream Architecture
Using Streams
Reading and Writing
Seeking
Closing and Flushing
Timeouts
Thread Safety
Backing Store Streams
FileStream
MemoryStream
PipeStream
BufferedStream
Stream Adapters
Text Adapters
Binary Adapters
Closing and Disposing Stream Adapters
Compression Streams
Compressing in Memory
Unix gzip File Compression
Working with ZIP Files
Working with Tar Files
File and Directory Operations
The File Class
The Directory Class
FileInfo and DirectoryInfo
Path
Special Folders
Querying Volume Information
Catching Filesystem Events
OS Security
Running in a Standard User Account
Administrative Elevation and Virtualization
Memory-Mapped Files
Memory-Mapped Files and Random File I/O
Memory-Mapped Files and Shared Memory (Windows)
Cross-Platform Interprocess Shared Memory
Working with View Accessors
Chapter 16. Networking
Network Architecture
Addresses and Ports
URIs
HttpClient
GetAsync and Response Messages
SendAsync and Request Messages
Uploading Data and HttpContent
HttpMessageHandler
Proxies
Authentication
Headers
Query Strings
Uploading Form Data
Cookies
Writing an HTTP Server
Using DNS
Sending Mail with SmtpClient
Using TCP
Concurrency with TCP
Receiving POP3 Mail with TCP
Chapter 17. Assemblies
What’s in an Assembly
The Assembly Manifest
The Application Manifest (Windows)
Modules
The Assembly Class
Strong Names and Assembly Signing
How to Strongly Name an Assembly
Assembly Names
Fully Qualified Names
The AssemblyName Class
Assembly Informational and File Versions
Authenticode Signing
How to Sign with Authenticode
Resources and Satellite Assemblies
Directly Embedding Resources
.resources Files
.resx Files
Satellite Assemblies
Cultures and Subcultures
Loading, Resolving, and Isolating Assemblies
Assembly Load Contexts
The Default ALC
The “Current” ALC
Assembly.Load and Contextual ALCs
Loading and Resolving Unmanaged Libraries
AssemblyDependencyResolver
Unloading ALCs
The Legacy Loading Methods
Writing a Plug-In System
Chapter 18. Reflection and Metadata
Reflecting and Activating Types
Obtaining a Type
Type Names
Base Types and Interfaces
Instantiating Types
Generic Types
Reflecting and Invoking Members
Member Types
C# Members Versus CLR Members
Generic Type Members
Dynamically Invoking a Member
Method Parameters
Using Delegates for Performance
Accessing Nonpublic Members
Generic Methods
Anonymously Calling Members of a Generic Interface
Calling Static Virtual/Abstract Interface Members
Reflecting Assemblies
Modules
Working with Attributes
Attribute Basics
The AttributeUsage Attribute
Defining Your Own Attribute
Retrieving Attributes at Runtime
Dynamic Code Generation
Generating IL with DynamicMethod
The Evaluation Stack
Passing Arguments to a Dynamic Method
Generating Local Variables
Branching
Instantiating Objects and Calling Instance Methods
Exception Handling
Emitting Assemblies and Types
The Reflection.Emit Object Model
Emitting Type Members
Emitting Methods
Emitting Fields and Properties
Emitting Constructors
Attaching Attributes
Emitting Generic Methods and Types
Defining Generic Methods
Defining Generic Types
Awkward Emission Targets
Uncreated Closed Generics
Circular Dependencies
Parsing IL
Writing a Disassembler
Chapter 19. Dynamic Programming
The Dynamic Language Runtime
Dynamic Member Overload Resolution
Simplifying the Visitor Pattern
Anonymously Calling Members of a Generic Type
Implementing Dynamic Objects
DynamicObject
ExpandoObject
Interoperating with Dynamic Languages
Passing State Between C# and a Script
Chapter 20. Cryptography
Overview
Windows Data Protection
Hashing
Hash Algorithms in .NET
Hashing Passwords
Symmetric Encryption
Encrypting in Memory
Chaining Encryption Streams
Disposing Encryption Objects
Key Management
Public-Key Encryption and Signing
The RSA Class
Digital Signing
Chapter 21. Advanced Threading
Synchronization Overview
Exclusive Locking
The lock Statement
Monitor.Enter and Monitor.Exit
Choosing the Synchronization Object
When to Lock
Locking and Atomicity
Nested Locking
Deadlocks
Performance
Mutex
Locking and Thread Safety
Thread Safety and .NET Types
Thread Safety in Application Servers
Immutable Objects
Nonexclusive Locking
Semaphore
Reader/Writer Locks
Signaling with Event Wait Handles
AutoResetEvent
ManualResetEvent
CountdownEvent
Creating a Cross-Process EventWaitHandle
Wait Handles and Continuations
WaitAny, WaitAll, and SignalAndWait
The Barrier Class
Lazy Initialization
Lazy
LazyInitializer
Thread-Local Storage
[ThreadStatic]
ThreadLocal
GetData and SetData
AsyncLocal
Timers
PeriodicTimer
Multithreaded Timers
Single-Threaded Timers
Chapter 22. Parallel Programming
Why PFX?
PFX Concepts
PFX Components
When to Use PFX
PLINQ
Parallel Execution Ballistics
PLINQ and Ordering
PLINQ Limitations
Example: Parallel Spellchecker
Functional Purity
Setting the Degree of Parallelism
Cancellation
Optimizing PLINQ
The Parallel Class
Parallel.Invoke
Parallel.For and Parallel.ForEach
Task Parallelism
Creating and Starting Tasks
Waiting on Multiple Tasks
Canceling Tasks
Continuations
Task Schedulers
TaskFactory
Working with AggregateException
Flatten and Handle
Concurrent Collections
IProducerConsumerCollection
ConcurrentBag
BlockingCollection
Writing a Producer/Consumer Queue
Chapter 23. Span and Memory
Spans and Slicing
CopyTo and TryCopyTo
Searching in Spans
Working with Text
Memory
Forward-Only Enumerators
Working with Stack-Allocated and Unmanaged Memory
Chapter 24. Native and COM Interoperability
Calling into Native DLLs
Type and Parameter Marshaling
Marshaling Common Types
Marshaling Classes and Structs
In and Out Marshaling
Calling Conventions
Callbacks from Unmanaged Code
Callbacks with Function Pointers
Callbacks with Delegates
Simulating a C Union
Shared Memory
Mapping a Struct to Unmanaged Memory
fixed and fixed {...}
COM Interoperability
The Purpose of COM
The Basics of the COM Type System
Calling a COM Component from C#
Optional Parameters and Named Arguments
Implicit ref Parameters
Indexers
Dynamic Binding
Embedding Interop Types
Type Equivalence
Exposing C# Objects to COM
Enabling Registry-Free COM
Chapter 25. Regular Expressions
Regular Expression Basics
Compiled Regular Expressions
RegexOptions
Character Escapes
Character Sets
Quantifiers
Greedy Versus Lazy Quantifiers
Zero-Width Assertions
Lookahead and Lookbehind
Anchors
Word Boundaries
Groups
Named Groups
Replacing and Splitting Text
MatchEvaluator Delegate
Splitting Text
Cookbook Regular Expressions
Recipes
Regular Expressions Language Reference
Index