C# 7.0 in a Nutshell
by Joseph Albahari and Ben Albahari
Copyright 2018 Joseph Albahari, Ben 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/safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
Editors: Rachel Roumeliotis | Indexer: Judith McConville |
Production Editor: Colleen Cole | Interior Designer: David Futato |
Copyeditor: Jasmine Kwityn | Cover Designer: Karen Montgomery |
Proofreader: Kim Cofer | Illustrator: Rebecca Demarest |
- October 2017: First Edition
Revision History for the First Edition
- 2015-11-03: First Release
- 2015-12-18: Second Release
- 2016-04-01: Third Release
- 2016-08-12: Fourth Release
- 2016-12-22: Fifth Release
- 2017-04-14: Sixth Release
- 2017-10-03: Seventh Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491987650 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. C# 7.0 in a Nutshell, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
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-491-98765-0
[M]
Preface
C# 7.0 represents the sixth 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, while 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 four editions, C# 7.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 knowledgemaking it accessible to intermediate as well as advanced readers.
This book covers C#, the CLR, and the core Framework assemblies. Weve chosen this focus to allow space for difficult topics such as concurrency, security, and application domainswithout compromising depth or readability. Features new to C# 6 and C# 7 and the associated Framework are flagged so that you can also use this book as a C# 5 and C# 6 reference.
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, WPF, UWP, or WCF. The areas of the language and .NET Framework that such books omit, C# 7.0 in a Nutshell covers in detailand vice versa.
If youre looking for a book that skims every .NET Framework 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
The first three chapters after the introduction 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 the core .NET Framework, including such topics as LINQ, XML, collections, concurrency, I/O and networking, memory management, reflection, dynamic programming, attributes, security, application domains, 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 a C# 7.0 compiler and Microsoft .NET Framework 4.6/4.7. You will also find Microsofts .NET documentation useful to look up individual types and members (which is available online).
While its possible to write source code in Notepad and invoke the compiler 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 code scratchpad, download LINQPad 5 from www.linqpad.net (free). LINQPad fully supports C# 7.0 and is maintained by one of the authors.
For an IDE, download Microsoft Visual Studio 2017: any edition is suitable for whats taught in this book, except the free express edition.
Note
All , plus the chapters on concurrency, parallel programming, and dynamic programming are available as interactive (editable) LINQPad samples. You can download the whole lot in a single click: go to LINQPads Samples tab at the bottom left, click Download more samples, and choose C# 7.0 in a Nutshell.
Conventions Used in This Book
The . 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 icon signifies a tip, suggestion, or general note.
Warning
This icon indicates a warning or caution.
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 a CD-ROM of examples from OReilly books