About This Book

This is a book about C#2 and 3—it’s as simple as that. I barely cover C#1, and only cover the .NET Framework libraries and Common Language Runtime (CLR) when they’re related to the language. This is a deliberate decision, and the result is quite a different book from most of the C# and .NET books I’ve seen.

By assuming a reasonable amount of knowledge of C#1, I avoided spending hundreds of pages covering material that I think most people already understand. That gave me much more room to expand on the details of C#2 and 3, which is what I hope you’re reading the book for.

I believe that many developers would be less frustrated with their work if they had a deeper connection with the language they’re writing in. I know it sounds geeky in the extreme to talk about having a “relationship” with a programming language, but that’s the best way I can describe it. This book is my attempt to help you achieve that sort of understanding, or deepen it further. It won’t be enough on its own—it should be a companion to your coding, guiding you and suggesting some interesting avenues to explore, as well as explaining why your code behaves the way it does.

Who should read this book?

During the course of the multiple rounds of reviewing this book underwent as I was writing it, one comment worried me more than most: “This is a book for C# experts.” That was never the intention, and I hope that (partly thanks to that honest feedback) it’s not an accurate reflection of who will get the most out of this book.

I don’t particularly want to write for experts. Aside from anything else, I’ve got less to offer experts than I have “intermediate” developers. I want to write for people who want to become experts. That’s what this book is about. If you feel passionately about computing, but happen not to have studied C#2 or 3 in much detail, this book is aimed squarely at you. If you want to immerse yourself in C# until it’s part of your bloodstream, then I’d feel honored to be the one to push you under. If you feel frustrated when you arrive at working code, but don’t quite know why it works, I want to help you to understand.

Having said all that, this book is not meant for complete beginners. If you haven’t used C#1 before, you’ll find this book very hard work. That doesn’t mean it won’t be useful to you—but please go and find a book (or at least a tutorial) on C#1 before you go much further. The first chapter will tease you with the joys of C#2 and 3, but you won’t be able to appreciate them if you’re worrying about how variables are declared and where the semicolons go.

I’m not going to claim that reading this book will make you a fabulous coder. There’s so much more to software engineering than knowing the syntax of the language you happen to be using. I give some words of guidance here and there, but ultimately there’s a lot more gut instinct in development than most of us would like to admit. What I will claim is that if you read and understand this book, you should feel comfortable with C#2 and 3, and free to follow your instincts without too much apprehension. It’s not about being able to write code that no one else will understand because it uses unknown corners of the language: it’s about being confident that you know the options available to you, and know which path the C# idioms are encouraging you to follow.

Roadmap

The book’s structure is simple. There are three parts and a single appendix. The first part serves as an introduction, including a refresher on topics in C#1 that are important for understanding C#2 and 3, and that are often misunderstood. The second part covers the new features in C#2. The third part covers the new features in C#3.

There are occasions where organizing the material this way means we come back to a topic a couple of times—in particular delegates are improved in C#2 and then again in C#3—but there is method in my madness. I anticipate that a number of readers will initially only be using C#2 for the bulk of their professional work, but with an interest in learning C#3 for new projects and hobbies. That means that it is useful to clarify what is in which version. It also provides a feeling of context and evolution—it shows how the language has developed over time.

Chapter 1 sets the scene by taking a simple piece of C#1 code and evolving it, seeing how C#2 and 3 allow the source to become more readable and powerful. We look at the historical context in which C# has grown, and the technical context in which it operates as part of a complete platform: C# as a language builds on framework libraries and a powerful runtime to turn abstraction into reality.

Chapter 2 looks back at C#1, and three specific aspects: delegates, the type system characteristics, and the differences between value types and reference types. These topics are often understood “just well enough” by C#1 developers, but C#2 and 3 develop them significantly, so a solid grounding is required in order to make the most of the new features.

Chapter 3 tackles the biggest feature of C#2, and potentially the hardest to grasp: generics. Methods and types can be written generically, with type parameters standing in for real types which are specified in the calling code. Initially it’s as confusing as this description makes it sound, but once you understand generics you’ll wonder how you survived without them.

If you’ve ever wanted to represent a null integer, chapter 4 is for you. It introduces nullable types, a feature built on generics and taking advantage of support in the language, runtime, and framework.

Chapter 5 shows the improvements to delegates in C#2. You may have only used delegates for handling events such as button clicks before now. C#2 makes it easier to create delegates, and library support makes them more useful for situations other than events.

In chapter 6 we examine iterators, and the easy way to implement them in C#2. Few developers use iterator blocks, but as LINQ to Objects is built on iterators, they will become more and more important. The lazy nature of their execution is also a key part of LINQ.

Chapter 7 shows a number of smaller features introduced in C#2, each making life a little more pleasant. The language designers have smoothed over a few rough places in C#1, allowing more flexible interaction with code generators, better support for utility classes, more granular access to properties, and more.

Chapter 8 once again looks at a few relatively simple features—but this time in C#3. Almost all the new syntax is geared toward the common goal of LINQ but the building blocks are also useful in their own right. With anonymous types, automatically implemented properties, implicitly typed local variables, and greatly enhanced initialization support, C#3 gives a far richer language with which your code can express its behavior.

Chapter 9 looks at the first major topic of C#3—lambda expressions. Not content with the reasonably concise syntax we saw in chapter 5, the language designers have made delegates even easier to create than in C#2. Lambdas are capable of more—they can be converted into expression trees: a powerful way of representing code as data.

In chapter 10 we examine extension methods, which provide a way of fooling the compiler into believing that methods declared in one type actually belong to another. At first glance this appears to be a readability nightmare, but with careful consideration it can be an extremely powerful feature—and one which is vital to LINQ.

Chapter 11 combines the previous three chapters in the form of query expressions, a concise but powerful way of querying data. Initially we concentrate on LINQ to Objects, but see how the query expression pattern is applied in a way which allows other data providers to plug in seamlessly.

Chapter 12 reaps the rewards of query expressions combined with expression trees: it shows how LINQ to SQL is able to convert what appears to be normal C# into SQL statements. We also take a speedy look at some other LINQ providers—those in the .NET Framework, some third-party ones which are gradually appearing, and a sneak peek at what Microsoft has in store.

We wind down in chapter 13 with a speculative glance at what the future might hold. We examine how the changes to C# will affect the flavor and texture of the code we write in it, as well as looking at some future trends in technology.

The appendix is a straightforward piece of reference material: the LINQ standard query operators, with some examples. Strictly speaking, this is not part of C#3—it’s library material—but I believe it’s so useful that readers will welcome its inclusion.

Terminology and typography

Most of the terminology of the book is explained as it goes along, but there are a few definitions that are worth highlighting here. I use C#1, C#2, and C#3 in a reasonably obvious manner—but you may see other books and websites referring to C#1.0, C#2.0, and C#3.0. The extra “.0” seems redundant to me, which is why I’ve omitted it—I hope the meaning is clear.

I’ve appropriated a pair of terms from a C# book by Mark Michaelis. To avoid the confusion between “runtime” being an execution environment (as in “the Common Language Runtime”) and a point in time (as in “overriding occurs at runtime”), Mark uses “execution time” for the latter concept, usually in comparison with “compile time.” This seems to me to be a thoroughly sensible idea, and one that I hope catches on in the wider community. I’m doing my bit by following his example in this book.

I frequently refer to “the language specification” or just “the specification”—unless I indicate otherwise, this means “the C# language specification.” However, multiple versions of the specification are available, partly due to different versions of the language itself and partly due to the standardization process. Rather than clutter up the book with specific “chapter and verse” references, there’s a page on the book’s website that allows you to pick which version of the specification you’re interested in and then see which part of the book refers to which area of the specification.

This book contains numerous pieces of code, which appear in a fixed-width font; output from the listings appears in the same way. Code annotations accompany some listings, and at other times particular sections of the code are highlighted in bold.

Almost all of the code appears in “snippet” form, allowing it to stay compact but still runnable—within the right environment. That environment is Snippy, a custom tool that is introduced in section 1.4. Snippy is available for download, along with all of the code from the book (in the form of snippets, full Visual Studio solutions, or more often both) from the book’s website at www.manning.com/CSharpInDepth.

Author Online and the C# in Depth website

Purchase of C# in Depth includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and other users. To access the forum and subscribe to it, point your web browser to www.manning.com/CSharpInDepth or www.manning.com/skeet. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

In addition to Manning’s own website, I have set up a companion website for the book at www.csharpindepth.com, containing information that didn’t quite fit into the book, as well as downloadable source code for all the listings in the book and further examples.

About the author

In many books, you will find a very impressive list of business and technical achievements accomplished by the author(s). Sadly, I have little to boast of on that front. Microsoft has been kind enough to award me MVP (Most Valuable Professional) status since 2003 for my “work” in the C# newsgroups, but I have to put “work” in quotes as it’s been such a fun ride. Beyond that, I run a modest website with some articles about C# and .NET, and a blog with some random thoughts about software development. I’m not the CTO of a wildly successful startup. I haven’t given sell-out lecture tours across multiple continents with webcasts that brought the Internet to its knees. Instead, I’ve spent my time working as a developer, listening to the problems of other developers, and trying to gradually learn the best way to write code and design solutions.

I’d like to think that in some ways that makes me the right person to write a book about C#—because it’s what I live and breathe from day to day, and it’s what I love helping people with. I’m passionate about C# in a way which my wife has learned to tolerate, and I hope that passion comes through in this book. I thought I was mad about it before I started writing, and my appreciation has only grown as I’ve become more intimately familiar with the details.

I’m not so much in love with C# that I can’t see any flaws—again, I hope that comes across in my writing. I’ve never met a language yet that didn’t have its hidden traps: C# is better than most in that respect, but it’s not perfect. When I see areas that have caused problems, either for me or for other developers who have posted in newsgroups or emailed me, I’m more than willing to point them out. I hope the designers will forgive the implied criticisms, and understand that I hold them in the highest regard for the beautiful and elegant language they created.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.34.146