O'Reilly logo

C# 7.0 in a Nutshell by Ben Albahari, Joseph Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required


C# 7.0 represents the sixth major update to Microsoft’s 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 there’s more than ever to learn. Although tools such as Microsoft’s IntelliSense—and online references—are 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 style—free 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 knowledge—making it accessible to intermediate as well as advanced readers.

This book covers C#, the CLR, and the core Framework assemblies. We’ve chosen this focus to allow space for difficult topics such as concurrency, security, and application domains—without 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 detail—and vice versa.

If you’re 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 you’re 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 6 and 7, which lay a foundation for subsequent topics. The three chapters on LINQ are also best read in sequence, and some chapters assume some knowledge of concurrency, which we cover in Chapter 14.

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 Microsoft’s .NET documentation useful to look up individual types and members (which is available online).

While it’s possible to write source code in Notepad and invoke the compiler from the command line, you’ll 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 what’s taught in this book, except the free express edition.


All code listings for Chapters 2 through 10, 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 LINQPad’s Samples tab at the bottom left, click “Download more samples,” and choose “C# 7.0 in a Nutshell.”

Conventions Used in This Book

The book uses basic UML notation to illustrate relationships between types, as shown in Figure P-1. 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.

Sample diagram
Figure P-1. Sample diagram

The following typographical conventions are used in this book:

Indicates 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

This icon signifies a tip, suggestion, or general note.


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 you’re 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 O’Reilly 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 product’s documentation does require permission.

We appreciate, but do not require, attribution. For example: “C# 7.0 in a Nutshell, by Joseph Albahari and Ben Albahari (O’Reilly) Copyright 2018, Joseph Albahari, Ben Albahari, 978-1-491-98765-0.”

If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at permissions@oreilly.com.

O’Reilly Safari


Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

We’d Like to Hear from You

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: http://bit.ly/c-sharp7_nutshell.

Code listings and additional resources are provided at: http://www.albahari.com/nutshell/.

To comment or ask technical questions about this book, send email to: bookquestions@oreilly.com.

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at: http://www.oreilly.com.


Joseph Albahari

First, I want to thank my brother, Ben Albahari, for persuading me to take on C# 3.0 in a Nutshell, whose success has spawned three subsequent editions. Ben shares my willingness to question conventional wisdom, and the tenacity to pull things apart until it becomes clear how they really work.

It’s been an honor to have superb technical reviewers on the team. In this and the last edition, we had invaluable and extensive feedback from Rod Stephens, Jared Parsons, Stephen Toub, Matthew Groves, Dixin Yan, Lee Coward, Bonnie DeWitt, Wonseok Chae, Lori Lalonde and James Montemagno.

The book was built on previous editions, whose technical reviewers I owe a similar honor: Eric Lippert, Jon Skeet, Stephen Toub, Nicholas Paldino, Chris Burrows, Shawn Farkas, Brian Grunkemeyer, Maoni Stephens, David DeWinter, Mike Barnett, Melitta Andersen, Mitch Wheat, Brian Peek, Krzysztof Cwalina, Matt Warren, Joel Pobar, Glyn Griffiths, Ion Vasilian, Brad Abrams, Sam Gentile, and Adam Nathan.

I appreciate that many of the technical reviewers are accomplished individuals at Microsoft, and I particularly thank you for taking out time to raise this book to the next quality bar.

Finally, I want to thank the O’Reilly team and extend personal thanks to Li, Miri, and Sonia.

Ben Albahari

Because my brother wrote his acknowledgments first, you can infer most of what I want to say. :) We’ve actually both been programming since we were kids (we shared an Apple IIe; he was writing his own operating system while I was writing Hangman), so it’s cool that we’re now writing books together. I hope the enriching experience we had writing the book will translate into an enriching experience for you reading the book.

I’d also like to thank my former colleagues at Microsoft. Many smart people work there, not just in terms of intellect but also in a broader emotional sense, and I miss working with them. In particular, I learned a lot from Brian Beckman, to whom I am indebted.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required