Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596009090/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
If youve picked up this book, youre probably among the many programmers who have come to appreciate how powerful the Microsoft .NET Framework can be as a platform for development. But youve also learned that, as with any platform, .NET has quirks that can trip up even the savviest programmer. Have you been surprised by feature behavior in the .NET language or the .NET Frameworkincluding the CLR and Framework Class Libraryor in Visual Studio .NET? If youve worked with .NET for any length of time, I suspect you are nodding your head with a sigh of agreement. Experience teaches us what to do and what to avoid, but in programming, we commonly acquire our knowledge the hard way: by making costly, hard-to-debug and time-consuming mistakes. My goal in writing this book is to save my fellow developers from some of that pain, by sharing the experiences (and bad code) that brought the pitfalls of .NET to light for me, and showing you how to avoid them (and write code that works).
I started working with .NET when it was in pre-release Beta 2. The following years have been a lot of fun. However, there have been days (and nights) when things were not so obvious, when the code would behave, shall I say, strangely, given my expectations as an experienced developer.
The gotchas and guidelines presented in this book have come from three sources: First, the insights I have gained from developing .NET applications. Second, the questions raised by the hundreds of inquisitive software developers Ive had the privilege of training. Third, the thought-provoking articles and discussions I have come across at symposiums, user-group meetings, the Internet, as well as the MSDN documentation itself.
Whats a GOTCHA ? According to Merriam-Webster, gotcha is defined as follows:
"Main Entry: got:cha Pronunciation: 'g-ch& Function: noun Etymology: alteration of got you : an unexpected usually disconcerting challenge, revelation, or catch."
For me, .NET gotchas are those things that pop up unexpectedly when youre programming in .NET. Things that are buried, just waiting for you! It is as if the environment is there, aware of its own idiosyncrasies, watching you as you work, but unable (or unwilling) to warn you as you fall into one of its traps.
Im not talking about those little annoying quirks in Visual Studio that make it misbehave occasionally, nor the transient flaws you see while working with the debugger or editor. In this book, I focus on the .NET Framework and language features that have consistently exhibited behavior that was not obvious to me. Why should you be interested in learning about these? Because knowing these little gotchas will help you avoid mistakes. Youll develop applications more quickly. Some solutions will improve the performance of your applications. Others will help you stay clear of problems that will make your code incorrect. Yet others will help you with hidden problems such as proper garbage collection.
Most books on .NET start by assuming that the reader knows little about it. Those books are intended to bring readers up to speed with .NET or one of its associated languages. As more and more people learn to use any technology, a need arises not just to learn how use it, but how to use it well, and do things right . Because experience is our best teacher, it always takes a few years for books that address those needs to appear.
Consider my own case, some years ago. I had taken the time to learn the C# language and was a serious developer (or so I thought), working hard to put the features and facilities of the language to prudent use. One day in the early 90s, I walked into my colleagues office and found a book entitled C++ FAQs by Marshal Cline on her table. Picking it up, I asked Why would anyone who knows C++ read a book that lists questions and answers? She smiled. I flipped through a few pages, and before I knew it, I found I had been standing there for several minutes reading the book, page after page. She finally interrupted and said, Why would anyone who knows C++ be reading that book for so long? I found it to be invaluable and bought a copy for myself (and I still have that book and its next edition in my library!) Other great books that I have enjoyed reading are Effective C++ and More Effective C++ by Scott Meyers, Essential COM by Don Box et al., and Effective Jav a by Joshua Bloch.
Programming in .NET has entered a stage where a large number of developers have learnt how to program what was a brand new platform only a few years ago. The time has arrived for books that teach good practices. I hope this book will serve, like the classics on COM and C++ that have preceded it, to educate software developers on the dos and donts of programming the .NET platform.
Who This Book Is For
This book is intended for the .NET programmer in the trenches. This includes both C# and VB.NET programmers. I assume that you are fairly familiar with .NET programming using either C# or VB.NET. I also assume that you are acquainted with the terms and vocabulary used by Microsoft in describing the .NET platform. In addition to programmers, project leads and senior programmers may find this book helpful in setting standards and guidelines for their development teams.
How This Book Is Organized
.NET Gotchas is organized into eight chapters, each focusing on a different area of interest in .NET. Each item has a name, title, and includes code examples, discussions, and guidelines In a Nutshell. The items are grouped into the area that I find to be the most appropriate. At the end of each item, related gotchas are referenced for your convenience. The examples are presented in both C# and VB.NET (clearly marked so you can easily identify these without having to examine the code). The code is labeled with the directory where you can find the example when you download the source code from the online resource. The output shown is from the C# code, unless the output from C# differs from the VB.NET output, as is the case in a few gotchas. Where they are different, both the C# output and the VB.NET output are shown and discussed.
CLR/FRAMEWORK GOTCHAS
The Common Language Runtime (CLR) and the .NET framework provide programmers with a high level of abstraction and ease of use. They pave the way for writing code, more or less consistently, in any of the different languages supported by .NET. However, there are aspects of both that are confusing, misleading, or prone to misuse. Also, certain features and choices can impact the behavior or performance of your code. In this chapter I focus on those issues.