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