C# 6.0 in a Nutshell
by Joseph Albahari and Ben Albahari
Copyright 2016 Joseph Albahari and 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://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Brian MacDonald
- Production Editor: Kristen Brown
- Proofreader: Amanda Kersey
- Indexer: Angela Howard
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
- December 2015: Sixth Edition
Revision History for the Sixth Edition
- 2015-11-03: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491927069 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. C# 6.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-92706-9
[M]
Preface
C# 6.0 represents the fifth 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 the optional use of 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 three editions, C# 6.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.0 and the associated Framework are flagged so that you can also use this book as a C# 5.0 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.
If youre already familiar with C# 5.0, youll find updated language sections, and a new chapter on Roslyn, the compiler-as-a-service.
This book is an ideal companion to any of the vast array of books that focus on an applied technology such as WPF, ASP.NET, or WCF. The areas of the language and .NET Framework that such books omit, C# 6.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 tablet or Windows Phone 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, code contracts, 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# 6.0 compiler and Microsoft .NET Framework 4.6. 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 or later from http://www.linqpad.net (free). LINQPad fully supports C# 6.0 and is maintained by one of the authors.
For an IDE, download Microsoft Visual Studio 2015: any edition, except the free express edition, is suitable for whats taught in this book.
Note
and choose C# 6.0 in a Nutshell.
Conventions Used in This Book
. 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
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 LINQPads Sample Libraries page: choose C# 6.0 in a Nutshell.
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 a CD-ROM of 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.