C# 4.0 in a Nutshell
Joseph Albahari
Ben Albahari
Copyright 2010 Joseph Albahari and Ben Albahari
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (.
Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo are registered trademarks of OReilly Media, Inc. C# 4.0 in a Nutshell, the image of a Numidian crane, and related trade dress are trademarks of OReilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and OReilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
O'Reilly Media
Preface
C# 4.0 further enhances Microsofts flagship programming language with much-requested featuresincluding support for dynamic programming, type parameter variance, and optional and named parameters. At the same time, the CLR and .NET Framework have grown to include a rich set of features for parallel programming, code contracts, and a new code security model.
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 previous edition, C# 4.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. 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# 4.0 and the associated Framework are flagged so that you can also use this book as a C# 3.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# 3.0, youll find more than 100 pages dedicated to the new features of C# 4.0 and Framework 4.0. In addition, many chapters have been enhanced from the previous edition, most notably the chapters on the C# language, .NET Framework fundamentals, memory management, threading, and COM interoperability. Weve also updated the LINQ chapters to make the examples friendly to both LINQ to SQL and Entity Framework programmers.
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# 4.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 a replacement for IntelliSense (i.e., the alphabetical listings of types and type members that appeared in the C# 1.1 edition of this book).
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, I/O and networking, memory management, reflection, dynamic programming, attributes, security, concurrency, application domains, and native interoperability. You can read most of these chapters randomly, except for Chapters , which lay a foundation for subsequent topics. The three chapters on LINQ are also best read in sequence.
What You Need to Use This Book
The examples in this book require a C# 4.0 compiler and Microsoft .NET Framework 4.0. You will also find Microsofts .NET documentation useful to look up individual types and members. The easiest way to get all threealong with an integrated development environmentis to install Microsoft Visual Studio 2010. Any edition is suitable for whats taught in this book, including Visual Studio Express (a free download). Visual Studio also includes an express edition of SQL Server, required to run the LINQ to SQL and Entity Framework examples, and IntelliSense, which pops up type member listings as you type.
For Chapters ; to obtain the additional samples, click Download more samples in the Samples tab at the bottom left. You can then advance through each sample with a single click.
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 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 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 .
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: C# 4.0 in a Nutshell by Joseph Albahari and Ben Albahari. Copyright 2010 Joseph Albahari and Ben Albahari, 978-0-596-80095-6.