Preface

C# 5.0 represents the fourth 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 provides low-level power through constructs such as custom value types and the optional use of 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 two editions, C# 5.0 in a Nutshell is organized entirely 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# 5.0 and the associated Framework are flagged so that you can also use this book as a C# 4.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 you’re already familiar with C# 4.0, you’ll find a reorganized section on concurrency, including thorough coverage of C# 5.0’s asynchronous functions and its associated types. We also describe the principles of asynchronous programming and how it helps with efficiency and thread-safety.

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# 5.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 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 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, 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 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# 5.0 compiler and Microsoft .NET Framework 4.5. 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 4.40 or later from www.linqpad.net (free). LINQPad fully supports C# 5.0 and is maintained by one of the authors.

For an IDE, download Microsoft Visual Studio 2012: any edition is suitable for what’s taught in this book, except the free express edition.

Note

All code listings for Chapter 2 through Chapter 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# 5.0 in a Nutshell.”

Conventions Used in This Book

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

The following typographical conventions are used in this book:

Italic

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

Tip

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

Caution

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# 5.0 in a Nutshell by Joseph Albahari and Ben Albahari. Copyright 2012 Joseph Albahari and Ben Albahari, 978-1-449-32010-2.”

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

Safari® Books Online

Note

Safari Books Online (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, 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, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

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://oreil.ly/csharp5_IAN

To comment or ask technical questions about this book, send email to:

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

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 two 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. This edition owes much to two legendary individuals at Microsoft: Eric Lippert (C# compiler team) and Stephen Toub (Parallel Programming team). I can’t thank you enough for your extensive and useful feedback—and for answering all my questions. I’m also immensely grateful to C# MVP Nicholas Paldino, whose keen eye and ability to pick up things that others miss, shaped this book and two previous editions.

This book was built on C# 4.0 in a Nutshell, whose technical reviewers I owe a similar honor. Chris Burrows (C# compiler team) significantly polished the chapters on concurrency, dynamic programming, and the C# language. From the CLR team, I received invaluable input on security and memory management from Shawn Farkas, Brian Grunkemeyer, Maoni Stephens, and David DeWinter. And on Code Contracts, the feedback from Brian Grunkemeyer, Mike Barnett, and Melitta Andersen raised the chapter to the next quality bar.

I have the highest praise for Jon Skeet (author of C# in Depth and Stack Overflow extraordinaire), whose perceptive suggestions shaped the previous edition, C# MVPs Mitch Wheat and Brian Peek, and reviewers of the 3.0 edition, including Krzysztof Cwalina, Matt Warren, Joel Pobar, Glyn Griffiths, Ion Vasilian, Brad Abrams, Sam Gentile, and Adam Nathan.

Finally, I want to thank the O’Reilly team, including my editor, Rachel Roumeliotis (a joy to work with), my excellent copy editor, Nancy Reinhardt, and members of my family, 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.

Get C# 5.0 in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.