C# 9.0 in a Nutshell
by Joseph Albahari
Copyright 2021 Joseph Albahari. 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: Corbin Collins
- Production Editor: Kristen Brown
- Copyeditor: Charles Roumeliotis
- Proofreader: Piper Editorial Consulting, LLC
- Indexer: WordCo Indexing Services, Inc.
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- March 2021: First Edition
Revision History for the First Edition
- 2021-02-26: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781098100964 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. C# 9.0 in a Nutshell, 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-098-10096-4
[LSI]
Preface
C# 9.0 represents the eighth major update to Microsofts flagship programming language, positioning C# as a language with unusual flexibility and breadth. At one end, it offers high-level abstractions such as query expressions and asynchronous continuations, whereas at the other end, it allows low-level efficiency through constructs such as custom value types and optional pointers.
The price of this growth is that theres more than ever to learn. Although tools such as Microsofts IntelliSenseand online referencesare excellent in helping you on the job, they presume an existing map of conceptual knowledge. This book provides exactly that map of knowledge in a concise and unified stylefree of clutter and long introductions.
Like the past six editions, C# 9.0 in a Nutshell is organized around concepts and use cases, making it friendly both to sequential reading and to random browsing. It also plumbs significant depths while assuming only basic background knowledge, making it accessible to intermediate as well as advanced readers.
This book covers C#, the Common Language Runtime (CLR), and the .NET 5 Base Class Library (BCL). Weve chosen this focus to allow space for difficult and advanced topics without compromising depth or readability. Features recently added to C# are flagged so that you can also use this book as a reference for C# 8 and C# 7.
Intended Audience
This book targets intermediate to advanced audiences. No prior knowledge of C# is required, but some general programming experience is necessary. For the beginner, this book complements, rather than replaces, a tutorial-style introduction to programming.
This book is an ideal companion to any of the vast array of books that focus on an applied technology such as ASP.NET Core or Windows Presentation Foundation (WPF). C# 9.0 in a Nutshell covers the areas of the language and .NET that such books omit, and vice versa.
If youre looking for a book that skims every .NET technology, this is not for you. This book is also unsuitable if you want to learn about APIs specific to mobile device development.
How This Book Is Organized
Chapters concentrate purely on C#, starting with the basics of syntax, types, and variables, and finishing with advanced topics such as unsafe code and preprocessor directives. If youre new to the language, you should read these chapters sequentially.
The remaining chapters focus on .NET 5s Base Class Libraries, covering such topics as Language-Integrated Query (LINQ), XML, collections, concurrency, I/O and networking, memory management, reflection, dynamic programming, attributes, cryptography, and native interoperability. You can read most of these chapters randomly, except for Chapters .
What You Need to Use This Book
The examples in this book require .NET 5. You will also find Microsofts .NET documentation useful to look up individual types and members (which is available online).
Although its possible to write source code in Notepad and build your program from the command line, youll be much more productive with a code scratchpad for instantly testing code snippets, plus an integrated development environment (IDE) for producing executables and libraries.
For a Windows code scratchpad, download LINQPad 6 from www.linqpad.net (free). LINQPad fully supports C# 9.0 and is maintained by one of the authors.
For a Windows IDE, download Visual Studio 2019: any edition is suitable for whats taught in this book. For a cross-platform IDE, download Visual Studio Code.
Note
All code listings for all chapters are available as interactive (editable) LINQPad samples. You can download the entire lot in a single click: at the bottom left, click the LINQPads Samples tab, click Download more samples, and then choose C# 9.0 in a Nutshell.
Conventions Used in This Book
The book uses basic UML notation to illustrate relationships between types, as shown in . A slanted rectangle means an abstract class; a circle means an interface. A line with a hollow triangle denotes inheritance, with the triangle pointing to the base type. A line with an arrow denotes a one-way association; a line without an arrow denotes a two-way association.
Figure P-1. Sample diagram
The following typographical conventions are used in this book:
ItalicIndicates new terms, URIs, filenames, and directories
Constant width
Indicates C# code, keywords and identifiers, and program output
Constant width bold
Shows a highlighted section of code
Constant width italic
Shows text that should be replaced with user-supplied values
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 http://www.albahari.com/nutshell.
If you have a technical question or a problem using the code examples, please send email to .
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.