About the Book
So what is "C# 3.0 in a Nutshell"... in a Nutshell?
It's a radical revision of the earlier O'Reilly book, C# in a Nutshell. The new edition has a totally new format—making it easier to follow while explaining topics in greater depth. And of course the new edition covers LINQ and C#3.0 in depth—as well as C# 2.0 and the core .NET Framework.
Where can I get it?
You can get it online from amazon.com, or your local bookstore.
How does the book differ from the old (C# 1.1) edition of the book?
We've significantly enhanced the language section and have rewritten nearly all of the Framework section. We've abandoned the model of a divorced reference: everything on a given topic is now in one place. This has allowed us to plumb greater depths while remaining concise—in the style of a Nutshell. The computer-generated type member listings have gone—in their place are examples and real-world scenarios. We've maintained strict vigilance in keeping our examples short and clutter-free. We have a policy of being useful and getting straight to the point.
We've also done away with the static help CD. Instead, you get an interactive tool for learning LINQ, preloaded with all the LINQ queries in the book.
Can I see the table of contents and a sample chapter?
Click here for a detailed Table of Contents; click here for the index.
For a sample chapter, read Threading in C#. Chapter 18 of the book is a later revision of this article.
Chapter 4 is also available online.
Aside from threading, what sections of the .NET Framework does this book cover?
We cover the CLR, LINQ, and core .NET Framework, as illustrated in the circles below:
I already own book XYZ and am unhappy with its level of detail. Does this book do better?
Most likely, yes! The reason, in a word, is focus. We dedicate the entire book to what's inside the circles shown above. This gives us space to do justice to difficult topics such as security, application domains, Reflection.Emit, threading—and of course, LINQ. Books covering everything in the diagram cannot offer the same level of depth—this is why you might be frustrated. There's nothing wrong with one-stop-shop books; it's just that they serve a different purpose.
Our focus in C# 3.0 in a Nutshell means we can answer such questions as:
-
when is it advantageous to choose a static readonly field over a constant?
-
when would you reimplement an interface, and what are the alternatives?
-
what are the pitfalls of outer variables in lambda expressions?
-
how do you perform outer joins with SelectMany, and how does this differ from the join operator?
-
what are the killer scenarios for using query comprehension syntax over lambda syntax?
-
when shouldn't you dispose an object?
-
how do you programmatically get through forms-based authentication?
-
when would you choose the XML serialization engine over the Data Contract serializer?
-
how do you sandbox an application domain, and when is this useful?
-
how would you go about parsing IL so as to write a code analysis tool or disassembler?
-
what's the trick to packaging a multi-DLL application as a single-file executable?
By the way, what is LINQ?
LINQ or Language INtegrated Query is a set of language and framework features that allow you to write queries directly in C#. LINQ drastically cuts the plumbing code required for database applications while reducing runtime errors (LINQ queries are statically type-checked by the compiler). LINQ can also query local in-memory collections and XML trees. Framework 3.5 ships with a lightweight new XML DOM designed for this purpose.
C# 3.0 is the new version of C# that supports all the language extensions required for LINQ. C# 3.0 includes such things as lambda expressions, extension methods and anonymous types, as well as the new query comprehension syntax.
LINQ and C# 3.0 are set to make a huge impact: LINQ to SQL alone will halve the cost of writing and maintaining a data access layer. Further, a single new query syntax works across databases, local collections, XML documents, datasets, as well as third party products.
Sounds interesting. What does this new query language look like? Give me a sneak preview.
Here's an simple query. It extracts all customers in the state of Washington (or Western Australia) that have placed more than ten orders, and selects their name, order count, and total value:
var query = from c in db.Customers where c.Orders.Count > 10 && c.Address.State == "WA" orderby c.Name select new { CustomerName = c.Name, OrdersPlaced = c.Orders.Count, TotalValue = c.Orders.Sum (o => o.Price) };
This is valid C# 3.0 code, and is fully type-checked by the compiler. Here's the complete syntax for LINQ comprehension queries in C# 3.0.
Why do queries start with “from”? In SQL they start with “select”!
In a LINQ query, data flows in order of the clauses. In the query above, we start with customers; then we filter them; then we sort them; finally, we project the result set. This approach has three major benefits:
-
Visual Studio can prompt you with IntelliSense as you type the query.
-
No special scoping rules are required to handle more complex kinds of query.
-
Once you've got used to it, queries are much easier to write.
Notice in our example query, we didn't need to perform any grouping or joining. Taking this one step further, suppose we wanted to perform pagination: for example, retrieve the 61st to 80th row in our query, and perform that filtering on the server. Here's all we'd have to add to our query:
var pagedQuery = query.Skip(60).Take(20);
Try writing the same in SQL!
Sounds interesting. Is it hard to learn?
LINQ borrows from the functional programming paradigm—the basis of such languages as LISP and Haskell. As most people are unfamiliar with functional programming, this creates an initial hump in the learning curve. If—like me—you start by jumping in and writing queries, there can be a frustrating period when nothing you write will compile, let alone run! Once you've understood LINQ's core concepts, however, it becomes highly rewarding. LINQ is highly consistent in its syntax and semantics, and relatively free of the nuances that plague SQL. The hardest bit then—is having to go back to SQL on other projects!
Is the book for beginners?
We've taken care in this edition not to alienate readers by presuming too much prior knowledge. Nonetheless, C# 3.0 in a Nutshell assumes some general background in programming and so is aimed at intermediate and advanced audiences. As a beginner you'll certainly benefit from this book—but not as your sole book. For something more introductory, I'd recommend a title such as Head First C#, by Andrew Stellman and Jennifer Greene, or Learning C# / Programming C# by Jesse Liberty.
So how much of LINQ do you cover?
We devote 120 pages to LINQ. We start by going deep into the underlying principles—so you'll really know what makes a LINQ query tick. We describe local and interpreted query architecture in detail—as well as covering all the query operators and the LINQ to XML DOM. We also cover the querying aspects of LINQ to SQL, including such topics as associations and DataLoadOptions. The book assumes no prior LINQ or functional programming knowledge.
How much of the book is computer-generated?
None in this edition. Gone are the alphabetical listings of type members. We have far more useful information to share!
What are your credentials?
I have 17 years' experience in software development, and have been working as a senior architect and developer on a wide range of commercial projects in C# since its inception. Ben has worked for five years at Microsoft as a program manager on three .NET teams, including ADO.NET and the .NET Compact Framework.
The two of you have the same unusual surname... are you related?
We're brothers.
How can I contact you?