Programming C# 8.0

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# 8.0 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 Core and the latest C# 8.0 additions, including asynchronous streams, nullable references, pattern matching, default interface implementation, ranges and new indexing syntax, and changes in the .NET tool chain.

  • Discover how C# supports fundamental coding features, such as classes, other custom types, collections, and error handling
  • Learn how to write high-performance memory-efficient code with .NET Core’s Span and Memory types
  • Query and process diverse data sources, such as in-memory object models, databases, data streams, and XML documents with LINQ
  • Use .NET’s multithreading features to exploit your computer’s parallel processing capabilities
  • Learn how asynchronous language features can help improve application responsiveness and scalability

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#?
    2. C#’s Defining Features
      1. Managed Code and the CLR
      2. Prefer Generality to Specialization
    3. C# Standards and Implementations
      1. Many Microsoft .NETs (Temporarily)
      2. Targeting Multiple .NET Versions with .NET Standard
    4. Visual Studio and Visual Studio Code
    5. Anatomy of a Simple Program
      1. Adding a Project to an Existing Solution
      2. Referencing One Project from Another
      3. Referencing External Libraries
      4. Writing a Unit Test
      5. Namespaces
      6. Classes
      7. Program Entry Point
      8. Unit Tests
    6. Summary
  3. 2. Basic Coding in C#
    1. Local Variables
      1. Scope
    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. Getting More Specific with when
      2. Patterns in Expressions
    9. Summary
  4. 3. Types
    1. Classes
      1. Static Members
      2. Static Classes
      3. Reference Types
    2. Structs
      1. When to Write a Value Type
      2. Guaranteeing Immutability
    3. Members
      1. Fields
      2. Constructors
      3. Deconstructors
      4. Methods
      5. Properties
      6. Indexers
      7. Initializer Syntax
      8. Operators
      9. Events
      10. Nested Types
    4. Interfaces
      1. Default Interface Implementation
    5. Enums
    6. Other Types
      1. Anonymous Types
    7. Partial Types and Methods
    8. 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
      1. ImmutableArray<T>
    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. Special Base Types
    11. 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
      1. Custom Exceptions
    5. Unhandled Exceptions
    6. 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. Ordering
      5. Containment Tests
      6. Specific Items and Subranges
      7. Aggregation
      8. Set Operations
      9. Whole-Sequence, Order-Preserving Operations
      10. Grouping
      11. Joins
      12. Conversion
    5. Sequence Generation
    6. Other LINQ Implementations
      1. Entity Framework
      2. Parallel LINQ (PLINQ)
      3. LINQ to XML
      4. Reactive Extensions
      5. Tx (LINQ to Logs and Traces)
    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. 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 Plugins with AssemblyLoadContext
    4. Assembly Names
      1. Strong Names
      2. Version
      3. Culture
    5. Protection
    6. 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. 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
      5. FileInfo, DirectoryInfo, and FileSystemInfo
      6. Known Folders
    4. Serialization
      1. BinaryReader, BinaryWriter, and BinaryPrimitives
      2. CLR Serialization
      3. JSON.NET
    5. Summary
  17. 16. Multithreading
    1. Threads
      1. Threads, Variables, and Shared State
      2. The Thread Class
      3. The Thread Pool
      4. Thread Affinity and SynchronizationContext
    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

Product information

  • Title: Programming C# 8.0
  • Author(s): Ian Griffiths
  • Release date: December 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492056812