Programming C# 10

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# 10.0 and .NET 6 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 6 and the latest C# 9.0 and 10.0 additions, including records, enhanced pattern matching, and new features designed to remove "ceremony" to improve productivity.

  • Understand how .NET has changed in the most recent releases, and learn what it means for application development
  • Select the most 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 available in .NET's class libraries
  • Learn how you can apply these class libraries to practical programming tasks
  • Explore numerous small additions to .NET that improve expressiveness

"Unlike books that focus on Visual Studio and technologies that interact with C#, this one covers the core language, and mastery of this core is essential to successfully building good software. It covers important concepts followed by generous code examples to explain them. It's thorough, detailed, and gets at the nooks and crannies of the language rarely covered elsewhere. It's a complete course on C#."--Jeremy MorganSoftware/DevOps Engineer

Ian Griffiths has worked in various aspects of computing, including computer networking, embedded real-time systems, broadcast television systems, medical imaging, and all forms of cloud computing. Ian is a Technical Fellow at endjin, and a Microsoft MVP in Developer Technologies. He's the author of several O'Reilly books and has written courses on Windows Presentation Foundation (WPF) and TPL Tables. Technology brings him joy.

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. Target Multiple .NET Versions with .NET Standard
    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. Static Members
      2. 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
    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. Generics and Tuples
    6. Inside Generics
    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. 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
    7. Sealed Methods and Classes
    8. Accessing Base Members
    9. Inheritance and Construction
    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. Garbage Collector Modes
      6. Temporarily Suspending Garbage Collections
      7. Accidentally Defeating Compaction
      8. 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. Captured Variables
      2. 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. Supporting Query Expressions
    2. Deferred Evaluation
    3. LINQ, Generics, and IQueryable<T>
    4. Standard LINQ Operators
      1. Filtering
      2. Select
      3. SelectMany
      4. Chunking
      5. Ordering
      6. Containment Tests
      7. Specific Items and Subranges
      8. Aggregation
      9. Set Operations
      10. Whole-Sequence, Order-Preserving Operations
      11. Grouping
      12. Joins
      13. 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. 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 Operations
      1. Interval
      2. Timer
      3. Timestamp
      4. TimeInterval
      5. Throttle
      6. Sample
      7. Timeout
      8. Windowing Operators
      9. Delay
      10. DelaySubscription
    10. Reaqtor—Rx as a Service
    11. Summary
  13. 12. Assemblies
    1. Anatomy of an Assembly
      1. .NET Metadata
      2. Resources
      3. Multifile Assemblies
      4. Other PE Features
    2. Type Identity
    3. Loading Assemblies
      1. Assembly Resolution
      2. Explicit Loading
      3. Isolation and Plug-ins with AssemblyLoadContext
    4. Assembly Names
      1. Strong Names
      2. Version
      3. Version Numbers and Assembly Loading
      4. Culture
    5. Protection
    6. Target Frameworks and .NET Standard
    7. 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
    2. Defining and Consuming Attributes
      1. Attribute Types
      2. Retrieving Attributes
      3. Metadata-Only Load
    3. 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. Random Access and Scatter/Gather I/O Without Stream
    3. Text-Oriented Types
      1. TextReader and TextWriter
      2. Concrete Reader and Writer Types
      3. Encoding
    4. Files and Directories
      1. FileStream Class
      2. File Class
      3. Directory Class
      4. Path Class
      5. FileInfo, DirectoryInfo, and FileSystemInfo
      6. Known Folders
    5. Serialization
      1. BinaryReader, BinaryWriter, and BinaryPrimitives
      2. CLR Serialization
      3. JSON
    6. 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. SpinLock
      3. Reader/Writer Locks
      4. Event Objects
      5. Barrier
      6. CountdownEvent
      7. Semaphores
      8. Mutex
      9. Interlocked
      10. Lazy Initialization
      11. 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. Stack Only
    3. Representing Sequential Elements with Memory<T>
    4. ReadOnlySequence<T>
    5. Processing Data Streams with Pipelines
      1. Processing JSON in ASP.NET Core
    6. Summary
  20. Index
  21. About the Author

Product information

  • Title: Programming C# 10
  • Author(s): Ian Griffiths
  • Release date: August 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098117818