C# 8.0 in a Nutshell
by Joseph Albahari and Eric Johannsen
Copyright 2020 Joseph Albahari and Eric Johannsen. 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 (.
- Acquisitions Editor: Amanda Quinn
- Developmental Editor: Corbin Collins
- Production Editor: Kate Galloway
- Copyeditor: Octal Publishing, LLC
- Proofreader: Piper Editorial
- Indexer: WordCo Indexing Services, Inc.
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
Revision History for the First Edition
- 2020-04-20: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492051138 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. C# 8.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 authors and do not represent the publishers views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors 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-05113-8
[LSCH]
Preface
C# 8.0 represents the seventh 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 five editions, C# 8.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 essential .NET Core assemblies. Weve chosen this focus to allow space for difficult topics such as concurrency, security, and access to operating system functionalitywithout compromising depth or readability. Features new to C# 8 are flagged so that you can also use this book as a reference for 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, Windows Presentation Foundation (WPF), and Universal Windows Platform (UWP). The areas of the language and .NET Core that such books omit, C# 8.0 in a Nutshell covers in detail, 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 cover essential elements of .NET Core, including such topics as Language-Integrated Query (LINQ), XML, collections, concurrency, I/O and networking, memory management, reflection, dynamic programming, attributes, security, 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 Core 3. 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# 8.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# 8.0 in a Nutshell.
.NET Core is available for Windows, Linux, and macOS. Certain cross-platform features were tested on Ubuntu Linux 18.04. That code is available on GitHub.
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.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book 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