C# Cookbook

Book description

Even if you're familiar with C# syntax, knowing how to combine various language features is a critical skill when you're building applications. This cookbook is packed full of recipes to help you solve issues for C# programming tasks you're likely to encounter. You'll learn tried-and-true techniques to help you achieve greater productivity and improve the quality of your code.

Author and independent consultant Joe Mayo shares some of the most important practices you'll need to be successful as a C# developer. Each section of this cookbook describes some useful facet of the C# programming language. These recipes--the result of many years of experience--are proven concepts for solving real-world problems with C#.

Recipes in this book will help you:

  • Set up your project, manage object lifetime, and establish patterns
  • Improve code quality through maintainability, error prevention, and correct syntax
  • Use LINQ to Objects for in-memory data manipulation and querying
  • Understand the differences between dynamic programming and reflection
  • Apply several async programming features you may not be aware of
  • Work with data using newer libraries and algorithms
  • Learn different ways to use new C# features, such as pattern matching and records

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Why I Wrote This Book
    2. Who This Book Is For
    3. How This Book Is Organized
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
  2. 1. Constructing Types and Apps
    1. 1.1. Managing Object End-of-Lifetime
    2. 1.2. Removing Explicit Dependencies
    3. 1.3. Delegating Object Creation to a Class
    4. 1.4. Delegating Object Creation to a Method
    5. 1.5. Designing Application Layers
    6. 1.6. Returning Multiple Values from a Method
    7. 1.7. Converting from Legacy to Strongly Typed Classes
    8. 1.8. Making Classes Adapt to Your Interface
    9. 1.9. Designing a Custom Exception
    10. 1.10. Constructing Objects with Complex Configuration
  3. 2. Coding Algorithms
    1. 2.1. Processing Strings Efficiently
    2. 2.2. Simplifying Instance Cleanup
    3. 2.3. Keeping Logic Local
    4. 2.4. Operating on Multiple Classes the Same Way
    5. 2.5. Checking for Type Equality
    6. 2.6. Processing Data Hierarchies
    7. 2.7. Converting from/to Unix Time
    8. 2.8. Caching Frequently Requested Data
    9. 2.9. Delaying Type Instantiation
    10. 2.10. Parsing Data Files
  4. 3. Ensuring Quality
    1. 3.1. Writing a Unit Test
    2. 3.2. Versioning Interfaces Safely
    3. 3.3. Simplifying Parameter Validation
    4. 3.4. Protecting Code from NullReferenceException
    5. 3.5. Avoiding Magic Strings
    6. 3.6. Customizing Class String Representation
    7. 3.7. Rethrowing Exceptions
    8. 3.8. Managing Process Status
    9. 3.9. Building Resilient Network Connections
    10. 3.10. Measuring Performance
  5. 4. Querying with LINQ
    1. 4.1. Transforming Object Shape
    2. 4.2. Joining Data
    3. 4.3. Performing Left Joins
    4. 4.4. Grouping Data
    5. 4.5. Building Incremental Queries
    6. 4.6. Querying Distinct Objects
    7. 4.7. Simplifying Queries
    8. 4.8. Operating on Sets
    9. 4.9. Building a Query Filter with Expression Trees
    10. 4.10. Querying in Parallel
  6. 5. Implementing Dynamic and Reflection
    1. 5.1. Reading Attributes with Reflection
    2. 5.2. Accessing Type Members with Reflection
    3. 5.3. Instantiating Type Members with Reflection
    4. 5.4. Invoking Methods with Reflection
    5. 5.5. Replacing Reflection with Dynamic Code
    6. 5.6. Performing Interop with Office Apps
    7. 5.7. Creating an Inherently Dynamic Type
    8. 5.8. Adding and Removing Type Members Dynamically
    9. 5.9. Calling Python Code from C#
    10. 5.10. Calling C# Code from Python
  7. 6. Programming Asynchronously
    1. 6.1. Creating Async Console Applications
    2. 6.2. Reducing Memory Allocations for Async Return Values
    3. 6.3. Creating Async Iterators
    4. 6.4. Writing Safe Async Libraries
    5. 6.5. Updating Progress Asynchronously
    6. 6.6. Calling Synchronous Code from Async Code
    7. 6.7. Waiting for Parallel Tasks to Complete
    8. 6.8. Handling Parallel Tasks as They Complete
    9. 6.9. Cancelling Async Operations
    10. 6.10. Disposing of Async Resources
  8. 7. Manipulating Data
    1. 7.1. Generating Password Hashes
    2. 7.2. Encrypting and Decrypting Secrets
    3. 7.3. Hiding Development Secrets
    4. 7.4. Producing JSON
    5. 7.5. Consuming JSON
    6. 7.6. Working with JSON Data
    7. 7.7. Consuming XML
    8. 7.8. Producing XML
    9. 7.9. Encoding and Decoding URL Parameters
    10. 7.10. Flexible DateTime Reading
  9. 8. Matching with Patterns
    1. 8.1. Converting Instances Safely
    2. 8.2. Catching Filtered Exceptions
    3. 8.3. Simplifying Switch Assignments
    4. 8.4. Switching on Property Values
    5. 8.5. Switching on Tuples
    6. 8.6. Switching on Position
    7. 8.7. Switching on Value Ranges
    8. 8.8. Switching with Complex Conditions
    9. 8.9. Using Logical Conditions
    10. 8.10. Switching on Type
  10. 9. Examining Recent C# Language Highlights
    1. 9.1. Simplifying Application Startup
    2. 9.2. Reducing Instantiation Syntax
    3. 9.3. Initializing Immutable State
    4. 9.4. Creating Immutable Types
    5. 9.5. Simplifying Immutable Type Assignments
    6. 9.6. Designing for Record Reuse
    7. 9.7. Returning Different Method Override Types
    8. 9.8. Implementing Iterators as Extension Methods
    9. 9.9. Slicing Arrays
    10. 9.10. Initializing Entire Modules
  11. Summary
  12. Index

Product information

  • Title: C# Cookbook
  • Author(s): Joe Mayo
  • Release date: September 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492093695