Programming C# 12

Book description

C# is undeniably one of the most versatile programming languages available to engineers today. With this comprehensive guide, you'll learn just how powerful the combination of C# and .NET can be. Author Ian Griffiths guides you through C# 12.0 and .NET 8 fundamentals and techniques for building cloud, web, and desktop applications.

Designed for experienced programmers, this book provides many code examples to help you work with the nuts and bolts of C#, such as generics, LINQ, and asynchronous programming features. You'll get up to speed on .NET 8 and the latest C# 11.0 and 12.0 additions, including generic math, new polymorphism options, enhanced pattern matching, and new features designed to improve productivity.

  • Understand how .NET has changed in recent releases, and learn what it means for application development
  • Select the appropriate C# language features for any task
  • Learn when to use the new features and when to stick with older ones
  • Examine the range of functionality in .NET's class libraries
  • Apply these class libraries to practical programming tasks
  • Explore numerous small additions to .NET that improve expressiveness

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who This Book Is For
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. Introducing C#
    1. Why C#?
      1. Managed Code and the CLR
      2. C# Prefers Generality to Specialization
    2. C# Standards and Implementations
      1. Many .NETs
      2. Release Cycles and Long Term Support
      3. Targeting Multiple .NET Runtimes
    3. Visual Studio, Visual Studio Code, and JetBrains Rider
    4. Anatomy of a Simple Program
      1. Writing a Unit Test
      2. Namespaces
      3. Classes
      4. Unit Tests
    5. Summary
  3. 2. Basic Coding in C#
    1. Local Variables
      1. Scope
      2. Variable Name Ambiguity
      3. Local Variable Instances
    2. Statements and Expressions
      1. Statements
      2. Expressions
    3. Comments and Whitespace
    4. Preprocessing Directives
      1. Compilation Symbols
      2. #error and #warning
      3. #line
      4. #pragma
      5. #nullable
      6. #region and #endregion
    5. Fundamental Data Types
      1. Numeric Types
      2. Booleans
      3. Strings and Characters
      4. Tuples
      5. Dynamic
      6. Object
    6. Operators
    7. Flow Control
      1. Boolean Decisions with if Statements
      2. Multiple Choice with switch Statements
      3. Loops: while and do
      4. C-Style for Loops
      5. Collection Iteration with foreach Loops
    8. Patterns
      1. Combining and Negating Patterns
      2. Relational Patterns
      3. Getting More Specific with when
      4. Patterns in Expressions
    9. Summary
  4. 3. Types
    1. Classes
      1. Initialization Inputs
      2. Static Members
      3. Static Classes
    2. Records
    3. References and Nulls
      1. Banishing Null with Non-Nullable References
    4. Structs
      1. When to Write a Value Type
      2. Guaranteeing Immutability
      3. Record Structs
    5. Class, Structs, Records, or Tuples?
    6. Members
      1. Accessibility
      2. Fields
      3. Constructors
      4. Deconstructors
      5. Methods
      6. Properties
      7. Operators
      8. Events
      9. Nested Types
    7. Interfaces
      1. Default Interface Implementation
      2. Static virtual members
    8. Enums
    9. Other Types
    10. Anonymous Types
    11. Partial Types and Methods
    12. Summary
  5. 4. Generics
    1. Generic Types
    2. Constraints
      1. Type Constraints
      2. Reference Type Constraints
      3. Value Type Constraints
      4. Value Types All the Way Down with Unmanaged Constraints
      5. Not Null Constraints
      6. Other Special Type Constraints
      7. Multiple Constraints
    3. Zero-Like Values
    4. Generic Methods
      1. Type Inference
    5. Generic Math
      1. Generic Math Interfaces
    6. Generics and Tuples
    7. Summary
  6. 5. Collections
    1. Arrays
      1. Array Initialization
      2. Searching and Sorting
      3. Multidimensional Arrays
      4. Copying and Resizing
    2. List<T>
    3. List and Sequence Interfaces
    4. Implementing Lists and Sequences
      1. Implementing IEnumerable<T> with Iterators
      2. Collection<T>
      3. ReadOnlyCollection<T>
    5. Addressing Elements with Index and Range Syntax
      1. System.Index
      2. System.Range
      3. Supporting Index and Range in Your Own Types
    6. Dictionaries
      1. Sorted Dictionaries
    7. Sets
    8. Queues and Stacks
    9. Linked Lists
    10. Concurrent Collections
    11. Immutable Collections
    12. Frozen Collections
    13. Summary
  7. 6. Inheritance
    1. Inheritance and Conversions
    2. Interface Inheritance
    3. Generics
      1. Covariance and Contravariance
    4. System.Object
      1. The Ubiquitous Methods of System.Object
    5. Accessibility and Inheritance
    6. Virtual Methods
      1. Abstract Methods
      2. Inheritance and Library Versioning
      3. Static Virtual Methods
      4. Default Constraints
    7. Sealed Methods and Classes
    8. Accessing Base Members
    9. Inheritance and Construction
      1. Primary Constructors
      2. Mandatory Properties
      3. Field Initialization
    10. Record Types
      1. Records, Inheritance, and the with Keyword
    11. Special Base Types
    12. Summary
  8. 7. Object Lifetime
    1. Garbage Collection
      1. Determining Reachability
      2. Accidentally Defeating the Garbage Collector
      3. Weak References
      4. Reclaiming Memory
      5. Lightening the Load with Inline Arrays
      6. Garbage Collector Modes
      7. Temporarily Suspending Garbage Collections
      8. Accidentally Defeating Compaction
      9. Forcing Garbage Collections
    2. Destructors and Finalization
    3. IDisposable
      1. Optional Disposal
    4. Boxing
      1. Boxing Nullable<T>
    5. Summary
  9. 8. Exceptions
    1. Exception Sources
      1. Exceptions from APIs
      2. Failures Detected by the Runtime
    2. Handling Exceptions
      1. Exception Objects
      2. Multiple catch Blocks
      3. Exception Filters
      4. Nested try Blocks
      5. finally Blocks
    3. Throwing Exceptions
      1. Rethrowing Exceptions
      2. Failing Fast
    4. Exception Types
    5. Custom Exceptions
    6. Unhandled Exceptions
    7. Summary
  10. 9. Delegates, Lambdas, and Events
    1. Delegate Types
      1. Creating a Delegate
      2. Multicast Delegates
      3. Invoking a Delegate
      4. Common Delegate Types
      5. Type Compatibility
      6. Behind the Syntax
    2. Anonymous Functions
      1. Lambdas and Default Arguments
      2. Captured Variables
      3. Lambdas and Expression Trees
    3. Events
      1. Standard Event Delegate Pattern
      2. Custom Add and Remove Methods
      3. Events and the Garbage Collector
      4. Events Versus Delegates
    4. Delegates Versus Interfaces
    5. Summary
  11. 10. LINQ
    1. Query Expressions
      1. How Query Expressions Expand
    2. Deferred Evaluation
    3. LINQ, Generics, and IQueryable<T>
    4. Standard LINQ Operators
      1. Filtering
      2. Select
      3. SelectMany
      4. Ordering
      5. Containment Tests
      6. Specific Items and Subranges
      7. Whole-Sequence, Order-Preserving Operations
      8. Aggregation
      9. Grouping
      10. Conversion
    5. Sequence Generation
    6. Other LINQ Implementations
      1. Entity Framework Core
      2. Parallel LINQ (PLINQ)
      3. LINQ to XML
      4. IAsyncEnumerable<T>
      5. Reactive Extensions
    7. Summary
  12. 11. Rx: Reactive Extensions
    1. Fundamental Interfaces
      1. IObserver<T>
      2. IObservable<T>
    2. Publishing and Subscribing with Delegates
      1. Creating an Observable Source with Delegates
      2. Subscribing to an Observable Source with Delegates
    3. Sequence Builders
      1. Empty
      2. Never
      3. Return
      4. Throw
      5. Range
      6. Repeat
      7. Generate
    4. LINQ Queries
      1. Grouping Operators
      2. Join Operators
      3. SelectMany Operator
      4. Aggregation and Other Single-Value Operators
      5. Concat Operator
    5. Rx Query Operators
      1. Merge
      2. Windowing Operators
      3. The Scan Operator
      4. The Amb Operator
      5. DistinctUntilChanged
    6. Schedulers
      1. Specifying Schedulers
      2. Built-in Schedulers
    7. Subjects
      1. Subject<T>
      2. BehaviorSubject<T>
      3. ReplaySubject<T>
      4. AsyncSubject<T>
    8. Adaptation
      1. IEnumerable<T> and IAsyncEnumerable<T>
      2. .NET Events
      3. Asynchronous APIs
    9. Timed Sequences
      1. Timed Sources
      2. Timed Operators
      3. Timed Windowing Operators
    10. Reaqtor—Rx as a Service
    11. Summary
  13. 12. Assemblies and Deployment
    1. Anatomy of an Assembly
      1. .NET Metadata
      2. Resources
      3. Multifile Assemblies
      4. Other PE Features
    2. Type Identity
    3. Deployment
      1. Framework-Dependent
      2. Self-Contained
      3. Ahead of Time (AOT) Compilation
    4. Loading Assemblies
      1. Assembly Resolution
      2. Explicit Loading
      3. Isolation and Plug-ins with AssemblyLoadContext
    5. Assembly Names
      1. Strong Names
      2. Version
      3. Version Numbers and Assembly Loading
      4. Culture
    6. Protection
    7. Target Frameworks and .NET Standard
    8. Summary
  14. 13. Reflection
    1. Reflection Types
      1. Assembly
      2. Module
      3. MemberInfo
      4. Type and TypeInfo
      5. MethodBase, ConstructorInfo, and MethodInfo
      6. ParameterInfo
      7. FieldInfo
      8. PropertyInfo
      9. EventInfo
    2. Reflection Contexts
    3. Summary
  15. 14. Attributes
    1. Applying Attributes
      1. Attribute Targets
      2. Compiler-Handled Attributes
      3. CLR-Handled Attributes
      4. Debugging Attributes
      5. Build-Time Attributes
    2. Defining and Consuming Attributes
      1. Attribute Types
      2. Retrieving Attributes
      3. Metadata-Only Load
    3. Generic Attribute Types
    4. Summary
  16. 15. Files and Streams
    1. The Stream Class
      1. Position and Seeking
      2. Flushing
      3. Copying
      4. Length
      5. Disposal
      6. Asynchronous Operation
      7. Concrete Stream Types
      8. One Type, Many Behaviors
    2. Text-Oriented Types
      1. TextReader and TextWriter
      2. Concrete Reader and Writer Types
      3. Encoding
    3. Files and Directories
      1. FileStream Class
      2. File Class
      3. Directory Class
      4. Path Class
    4. Serialization
      1. BinaryReader, BinaryWriter, and BinaryPrimitives
      2. CLR Serialization
      3. JSON
    5. Summary
  17. 16. Multithreading
    1. Threads
      1. Threads, Variables, and Shared State
      2. Thread-Local Storage
      3. The Thread Class
      4. The Thread Pool
      5. Thread Affinity and SynchronizationContext
      6. ExecutionContext
    2. Synchronization
      1. Monitors and the lock Keyword
      2. Other Synchronization Primitives
      3. Interlocked
      4. Lazy Initialization
      5. Other Class Library Concurrency Support
    3. Tasks
      1. The Task and Task<T> Classes
      2. Continuations
      3. Schedulers
      4. Error Handling
      5. Custom Threadless Tasks
      6. Parent/Child Relationships
      7. Composite Tasks
    4. Other Asynchronous Patterns
    5. Cancellation
    6. Parallelism
      1. The Parallel Class
      2. Parallel LINQ
      3. TPL Dataflow
    7. Summary
  18. 17. Asynchronous Language Features
    1. Asynchronous Keywords: async and await
      1. Execution and Synchronization Contexts
      2. Multiple Operations and Loops
      3. Returning a Task
      4. Applying async to Nested Methods
    2. The await Pattern
    3. Error Handling
      1. Validating Arguments
      2. Singular and Multiple Exceptions
      3. Concurrent Operations and Missed Exceptions
    4. Summary
  19. 18. Memory Efficiency
    1. (Don’t) Copy That
    2. Representing Sequential Elements with Span<T>
      1. Utility Methods
      2. Collection Expressions and Spans
      3. Pattern matching
      4. Stack Only
    3. Using ref With Fields
    4. Representing Sequential Elements with Memory<T>
    5. ReadOnlySequence<T>
    6. Processing Data Streams with Pipelines
      1. Processing JSON in ASP.NET Core
    7. Summary
  20. About the Author

Product information

  • Title: Programming C# 12
  • Author(s): Ian Griffiths
  • Release date: June 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098158361