C# Cookbook
by Joe Mayo
Copyright 2022 Mayo Software, LLC. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Amanda Quinn
- Development Editor: Angela Rufino
- Production Editor: Katherine Tozer
- Copyeditor: Justin Billing
- Proofreader: Piper Editorial Consulting, LLC
- Indexer: WordCo Indexing Services, Inc.
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- October 2021: First Edition
Revision History for the First Edition
- 2021-09-29: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492093695 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. C# Cookbook, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-09369-5
[LSI]
Preface
Why I Wrote This Book
In the course of a career, we collect many tools. Whether concepts, techniques, patterns , or reusable code, these tools help us get our job done. The more we collect, the better, because we have so many problems to solve and applications to build. C# Cookbook contributes to your toolset by providing you with a variety of recipes.
Things change over time, including programming languages. As of this writing, the C# programming language is over 20 years old, and software development has changed during its lifetime. There are a lot of recipes that could be written; this book acknowledges the evolution of C# over time and the fact that modern C# code makes us more productive.
This book is full of recipes that Ive used throughout my career. In addition to stating a problem, presenting code, and explaining the solution, each discussion includes deeper insight into why each recipe is important. Throughout the book, Ive avoided advocacy of process or absolute declarations of you must do it this way because much of what we do in creating software requires trade-offs. In fact, youll find several discussions of what the consequences or trade-offs are with a recipe. This respects the fact that you can consider to what extent a recipe applies to you.
Who This Book Is For
This book assumes that you already know basic C# syntax. That said, there are recipes for various levels of developers. Whether youre a beginner, intermediate, or senior developer, there should be something for you. If youre an architect, there might be some interesting recipes that help you get back up to speed on the latest C# techniques .
How This Book Is Organized
When brainstorming for this book, the entire focus was on answering the question What do C# developers need to do? Looking at the list, certain patterns emerged and evolved into chapters:
One of the first things I do when writing code is to build the types and organize the application. So I wrote to show how to create and organize types. Youll see recipes dealing with patterns because thats how I code.
After creating types, we add type members, like methods, and the logic they contain, which is a natural category of recipes for .
What good is code unless it works well? Thats why I added , which contains recipes that help improve the quality of code. While this chapter is packed with useful recipes, youll want to check out the recipe that shows how to use nullable reference types.
While Chapters to the end of the book I break away, taking a technology-specific focus:
Many people think of Language Integrated Query (LINQ) as a database technology. While LINQ is useful for working with databases, its also excellent for in-memory data manipulation and querying. Thats why discusses what you can do with the in-memory provider, called LINQ to Objects.
Reflection was part of C# 1, but dynamic programming came along later in C# 4. I think its important to discuss both technologies in and even show how dynamic programing can be better than reflection in some situations. Also, be sure to check out the Python interop recipes.
Async programming was a great addition to C# and seems straightforward, on the surface. covers async with recipes that explain several important features you might not be aware of.
All apps use data, whether securing, parsing, or serializing. includes several recipes covering different things you want to do with data. It focuses on some of the newer libraries and algorithms you might want to use for working with data.
One of the largest transformations of the C# language occurred over the last few versions in the area of pattern matching. There are so many that I was able to fill with only pattern-matching recipes.
C# continues to evolve and captures recipes dedicated to C# 9. Well look at some of the new features and discuss how to apply them. While I provide insight in the discussion, remember that sometimes new features can become more integral to a language in later versions. If youre into the cutting edge, these recipes are pretty interesting.
Conventions Used in This Book
The following typographical conventions are used in this book:
ItalicIndicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Tip
This element signifies a tip or suggestion.
Note
This element signifies a general note.
Warning
This element indicates a warning or caution.
Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/JoeMayo/csharp-nine-cookbook.
If you have a technical question or a problem using the code examples, please send email to .