C# 2008
Programmer's Reference
Wei-Meng Lee
To my family and wife, Shihua, for their support and love.
Wei-Meng Lee, Microsoft MVP, is a technologist and founder of Developer Learning Solutions (www.learn2develop.net), a technology company specializing in hands-on training in the latest Microsoft and Apple technologies.
Wei-Meng writes extensively for online publications such as DevX.com and the O'Reilly Network and magazines such as
You can contact Wei-Meng at [email protected].
Andrew Moore is a graduate of Purdue University-Calumet in Hammond, Indiana, and has been developing software since 1998 for radar systems, air traffic management, discrete-event simulation, and business communications applications using C, C++, C#, and Java on the Windows, UNIX, and Linux platforms. Andrew is the author of Wrox Blox articles titled 'Create Amazing Custom User Interfaces with WPF, C#, and XAML in .NET 3.0,', '.NET 3.5 CD Audio Player,' and 'Power Programming with ReSharper.' He is currently a senior software engineer at Interactive Intelligence, Inc. in Indianapolis developing Microsoft-based applications for business communications. Andrew lives in Indiana with his wife, Barbara, and children, Sophia and Andrew.
Acquisitions Editor
Katie Mohr
Development Editor
Maryann Steinhart
Technical Editor
Andrew Moore
Production Editor
Christine O'Connor
Copy Editor
Foxxe Editorial Services
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Project Coordinator, Cover
Lynsey Stanford
Proofreader
C.M. Jones
Indexer
Robert Swanson
Writing a book is like running a long race — you need stamina, training, and perseverance. But these are not the most important factors that ensure you reach the finishing line. The most important factor is the motivation that keeps you going, on and on, even though you may be physically exhausted and don't think you can move any farther. While this is not the first book I have written, I am always very excited when embarking on a new book project. After the excitement comes a lot of hard work — coping with missed deadlines, changing work schedules, writer's block, and so forth.
For this book, I am extremely lucky to work with two very important people — my acquisitions editor, Katie Mohr, and development editor, Maryann Steinhart. Both Katie and Maryann have been very patient with me when the going gets tough. Katie has always egged me on, and offered many suggestions to scope the book to what it is today. Maryann has been the studious timekeeper, with a gentle but firm voice telling me to press forward when I missed the deadline. Maryann has also painstakingly read and reread every sentence I wrote, and I am always surprised with her attention to detail which has definitely made this book a better one. With heart-felt sincerity, I want to say a big thank you to both of them!
I would also like to thank the technical editor, Andrew Moore, for giving me many suggestions for improving the book. Writing this book has made me learn a lot of things I never knew. Thanks, Andrew!
Last but not least, I want to take this opportunity to thank my parents and my wife, Shihua, for their understanding and support when I have had to take time away to work on this book. Thanks!
Introduction
Since the release of the Microsoft .NET Framework in July 2000, the C# programming language has gone through a few iterations to its latest version, 3.0. Over the years, the C# language has gained a lot of followers, partly due to its syntax, which is familiar to Java and C programmers. The clear syntax of the language made it easy to learn, and it's a popular choice for beginning programmers. In addition, the C# language is gaining a lot of traction in the Visual Basic camp, especially among VB6 programmers, who needed to move to .NET and did not want to learn a totally new variant of the Visual Basic language — Visual Basic .NET.
The latest version of C# 3.0 comes with .NET Framework 3.5. It contains many new features that makes the language more intuitive and powerful. Coupled with Visual Studio 2008, Microsoft's flagship development environment, developing applications using C# is now available to a wide audience.
In writing this book, I used the approach I believe is the easiest way to learn a new language — by examples. Often, books and articles get into too much of the theory without showing the readers what the concept looks like in code. For each topic, I try to provide numerous examples to illustrate the concept, and I would encourage you to make changes to the program to explore further. If you are an experienced programmer, you can jump directly to a particular chapter, as each chapter comes with independent examples.
This book is for programmers of all levels. Beginning programmers should find the C# language easy to learn through the many code examples provided in each chapter. Experienced programmers can jump directly to individual chapters covering the topics of interest to them.
A conscious effort is made to illustrate each topic with independent code examples so that readers who want clarification on a topic do not need to wade through the entire chapter.
This book is ideal for the working programmer as well as students taking a semester course in C# programming. The sample projects covered in chapters 16 to 20 provide numerous project ideas as well as motivation for readers to get started working on bigger projects.