Tải bản đầy đủ (.pdf) (540 trang)

Accelerated C# 2008 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.02 MB, 540 trang )

this print for content only—size & color not accurate spine = 1.028" 544 page count
Books for professionals By professionals
®
Accelerated C# 2008
Dear Reader,
In your hands is a guide to creating effective C# 3.0 code. This book focuses
precisely on the C# 3.0 language, showing you how to write programs that are
robust, fault-tolerant, and ready to be put into widely available libraries.
Accelerated C# 2008 won’t burden your time with endless discussions of
libraries. Instead, it presents you with a well organized, focused and easy-to-
read text about C# 3.0 and all of the tried and true idioms, patterns, and design
principles developed in the object oriented arena during the lifetime of the
.NET Framework. With many short examples, I will show you how common
design patterns are used routinely in the .NET Framework and how you should
employ them in your own designs.
In this book I’ll explore with you all the new features of the C# 3.0 language
in detail, including extension methods, lambda expressions, and Language
Integrated Query (LINQ) among others. These are all exciting additions to C# 3.0
that foster a functional programming model within what has previously been
almost exclusively an imperative programming language. As you put these new
features of C# 3.0 to use, you’ll find that they expand your horizons and open
up new possibilities of solutions using functional programming techniques!
Another area of particular interest for C# developers is how we can write
exception-safe and fault-tolerant code. The .NET Framework includes several
facilities, including Critical Execution Regions, to help protect the state of your
application in the event of an asynchronous exception. In this book, I explain
how to use these new facilities to your advantage.
Have fun programming and remember, always defining a contract before
implementation, striving to be exception-neutral, and being frugal with your
resource usage are all keys to entering the halls of C# guru code-dom.
Trey Nash


Author of
Accelerated C# 2005
Coauthor of
Accelerated VB 2005
Accelerated VB 2008
US $39.99
Shelve in
Programming Languages/C#
User level:
Intermediate–Advanced
Nash
Accelerated C# 2008
The eXperT’s Voice
®
in .neT
Accelerated
C# 2008
cyan
MaGenTa
yelloW
Black
panTone 123 c
Trey Nash
Foreword by Wes Dyer
C# Language Design Team, Microsoft
Free Companion
eBook Available
THE APRESS ROADMAP
Pro LINQ:
Language Integrated

Query in C# 2008
Pro C# 2008 and the
.NET 3.5 Framework, 4e
Illustrated C# 2008
Pro WCF: Practical Microsoft
SOA Implementation
Beginning C# 2008:
From Novice to Professional
Beginning
C# 2008 Databases:
From Novice to Professional
Accelerated C# 2008
Pro WPF in C# 2008:
Windows Presentation
Foundation with .NET 3.5, 2e
www.apress.com
SOURCE CODE ONLINE
Free Companion eBook

For a limited time only.
See last page for details.
ISBN-13: 978-1-59059-873-3
ISBN-10: 1-59059-873-3
9 781590 598733
5 3 9 9 9
For a limited time,
get the free, fully searchable
eBook—a $20 value!
See last page for details.
Offer ends June 30, 2008.

For a limited time,
get the free, fully searchable
eBook—a $20 value!
See last page for details.
Offer ends June 30, 2008.
Accelerated C# 2008
Trey Nash
8733fmCMP4.qrk 10/16/07 11:04 AM Page i
Accelerated C# 2008
Copyright © 2007 by Weldon W. Nash, III
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-873-3
ISBN-10 (pbk): 1-59059-873-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Dominic Shakeshaft
Technical Reviewer: Shawn Wildermuth
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Sofia Marchant
Copy Editor: Jim Compton
Assistant Production Director: Kari Brooks-Copony
Production Editor: Laura Cheu
Compositor: Jimmie Young

Proofreader: April Eddy
Indexer: Beth Palmer
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit
.
The information in this book is distributed on an “as is” basis, without warranty. Although every precau-
tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in this work.
The source code for this book is available to readers at .
8733fmCMP4.qrk 10/16/07 11:04 AM Page ii
This book is dedicated to the memory of my grandfather
Weldon W. Nash, Sr.
December 19, 1912 – April 29, 2007
To Svetlana
for believing in me
8733fmCMP4.qrk 10/16/07 11:04 AM Page iii
8733fmCMP4.qrk 10/16/07 11:04 AM Page iv
Contents at a Glance
Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

■CHAPTER 1 C# Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 C# and the CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
■CHAPTER 3 C# Syntax Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
■CHAPTER 4 Classes, Structs, and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
■CHAPTER 5 Interfaces and Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
■CHAPTER 6 Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
■CHAPTER 7 Exception Handling and Exception Safety. . . . . . . . . . . . . . . . . . . . . . . . . . . 163
■CHAPTER 8 Working with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
■CHAPTER 9 Arrays, Collection Types, and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
■CHAPTER 10 Delegates, Anonymous Functions, and Events. . . . . . . . . . . . . . . . . . . . . . . 253
■CHAPTER 11 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
■CHAPTER 12 Threading in C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
■CHAPTER 13 In Search of C# Canonical Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
■CHAPTER 14 Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
■CHAPTER 15 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
■CHAPTER 16 LINQ: Language Integrated Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
■APPENDIX References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
v
8733fmCMP4.qrk 10/16/07 11:04 AM Page v
8733fmCMP4.qrk 10/16/07 11:04 AM Page vi
Contents
Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■CHAPTER 1 C# Preview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Differences Between C# and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
C# 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
CLR Garbage Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Example of a C# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Overview of Features Added in C# 2.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Overview of What’s New in C# 3.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
■CHAPTER 2 C# and the CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The JIT Compiler in the CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Assemblies and the Assembly Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Minimizing the Working Set of the Application . . . . . . . . . . . . . . . . . . . . . . . 11
Naming Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Loading Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Cross-Language Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
■CHAPTER 3 C# Syntax Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
C# Is a Strongly Typed Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
vii
8733fmCMP4.qrk 10/16/07 11:04 AM Page vii
Types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Value Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Reference Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Default Variable Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Implicitly Typed Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Type Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
as and is Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Defining Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Using Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
if-else, while, do-while, and for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
foreach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
break, continue, goto, return, and throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
■CHAPTER 4 Classes, Structs, and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Accessibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
sealed Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
abstract Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Indexers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
partial Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
partial Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Static Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Reserved Member Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Value Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The Meaning of this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Anonymous Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Object Initializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
■CONTENTSviii
8733fmCMP4.qrk 10/16/07 11:04 AM Page viii
Boxing and Unboxing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
When Boxing Occurs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Efficiency and Confusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
System.Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Equality and What It Means . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The IComparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The new Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Field Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Static (Class) Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Instance Constructor and Creation Ordering . . . . . . . . . . . . . . . . . . . . . 101
Destroying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Deterministic Destruction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Exception Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Disposable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The IDisposable Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The using Keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Method Parameter Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Value Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
ref Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
out Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
param Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Inheritance and Virtual Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

Virtual and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
override and new Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
sealed Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
A Final Few Words on C# Virtual Methods . . . . . . . . . . . . . . . . . . . . . . . 118
Inheritance, Containment, and Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Choosing Between Interface and Class Inheritance. . . . . . . . . . . . . . . . 119
Delegation and Composition vs. Inheritance . . . . . . . . . . . . . . . . . . . . . 120
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
■CHAPTER 5 Interfaces and Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Interfaces Define Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Defining Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
What Can Be in an Interface? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Interface Inheritance and Member Hiding. . . . . . . . . . . . . . . . . . . . . . . 126
■CONTENTS ix
8733fmCMP4.qrk 10/16/07 11:04 AM Page ix
Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Implicit Interface Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Explicit Interface Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Overriding Interface Implementations in Derived Classes . . . . . . . . . . . 131
Beware of Side Effects of Value Types Implementing Interfaces . . . . . . 134
Interface Member Matching Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Explicit Interface Implementation with Value Types . . . . . . . . . . . . . . . . . . . . 139
Versioning Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Contracts Implemented with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Interface Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Choosing Between Interfaces and Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
■CHAPTER 6 Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Just Because You Can Doesn’t Mean You Should . . . . . . . . . . . . . . . . . . . . . . 149

Types and Formats of Overloaded Operators . . . . . . . . . . . . . . . . . . . . . . . . . 149
Operators Shouldn’t Mutate Their Operands . . . . . . . . . . . . . . . . . . . . . . . . . 150
Does Parameter Order Matter? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Overloading the Addition Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Operators That Can Be Overloaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Conversion Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
■CHAPTER 7 Exception Handling and Exception Safety. . . . . . . . . . . . . . . . . . . 163
How the CLR Treats Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Mechanics of Handling Exceptions in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Changes with Unhandled Exceptions Starting with .NET 2.0 . . . . . . . . . 164
Syntax Overview of the try Statement. . . . . . . . . . . . . . . . . . . . . . . . . . 165
Rethrowing Exceptions and Translating Exceptions. . . . . . . . . . . . . . . . 167
Exceptions Thrown in finally Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Exceptions Thrown in Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Exceptions Thrown in Static Constructors. . . . . . . . . . . . . . . . . . . . . . . 171
Who Should Handle Exceptions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Avoid Using Exceptions to Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Achieving Exception Neutrality. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Basic Structure of Exception-Neutral Code . . . . . . . . . . . . . . . . . . . . . . 174
Constrained Execution Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Critical Finalizers and SafeHandle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
■CONTENTSx
8733fmCMP4.qrk 10/16/07 11:04 AM Page x
Creating Custom Exception Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Working with Allocated Resources and Exceptions. . . . . . . . . . . . . . . . . . . . . 186
Providing Rollback Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
■CHAPTER 8 Working with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
String Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Format Specifiers and Globalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Object.ToString, IFormattable, and CultureInfo . . . . . . . . . . . . . . . . . . . 198
Creating and Registering Custom CultureInfo Types . . . . . . . . . . . . . . . 199
Format Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Console.WriteLine and String.Format . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Examples of String Formatting in Custom Types . . . . . . . . . . . . . . . . . . 203
ICustomFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Working with Strings from Outside Sources. . . . . . . . . . . . . . . . . . . . . . . . . . 208
StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Searching Strings with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 211
Searching with Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Searching and Grouping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Replacing Text with Regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Regex Creation Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
■CHAPTER 9 Arrays, Collection Types, and Iterators . . . . . . . . . . . . . . . . . . . . . . 221
Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Implicitly Typed Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Type Convertibility and Covariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Sortability and Searchability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Vectors vs. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Multidimensional Rectangular Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Multidimensional Jagged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Collection Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

Comparing ICollection with ICollection. . . . . . . . . . . . . . . . . . . . . . . . . 231
Collection Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
System.Collections.ObjectModel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
■CONTENTS xi
8733fmCMP4.qrk 10/16/07 11:04 AM Page xi
IEnumerable <T>, IEnumerator <T>, IEnumerable, and IEnumerator. . . . . . . 239
Types That Produce Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Forward, Reverse, and Bidirectional Iterators . . . . . . . . . . . . . . . . . . . . 247
Collection Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
■CHAPTER 10 Delegates, Anonymous Functions, and Events. . . . . . . . . . . . . . 253
Overview of Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Delegate Creation and Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Single Delegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Delegate Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Iterating Through Delegate Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Unbound (Open Instance) Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Anonymous Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Beware the Captured Variable Surprise. . . . . . . . . . . . . . . . . . . . . . . . . 270
Anonymous Methods as Delegate Parameter Binders. . . . . . . . . . . . . . 272
The Strategy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
■CHAPTER 11 Generics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Difference Between Generics and C++ Templates. . . . . . . . . . . . . . . . . . . . . 280

Efficiency and Type Safety of Generics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Generic Type Definitions and Constructed Types . . . . . . . . . . . . . . . . . . . . . . 282
Generic Classes and Structs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Generic Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Generic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Generic Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Generic Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Default Value Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Nullable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Constructed Types Control Accessibility . . . . . . . . . . . . . . . . . . . . . . . . 295
Generics and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Constraints on Nonclass Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Generic System Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Generic System Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Select Problems and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Conversion and Operators Within Generic Types . . . . . . . . . . . . . . . . . . 305
Creating Constructed Types Dynamically . . . . . . . . . . . . . . . . . . . . . . . 314
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
■CONTENTSxii
8733fmCMP4.qrk 10/16/07 11:04 AM Page xii
■CHAPTER 12 Threading in C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Threading in C# and .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Starting Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The IOU Pattern and Asynchronous Method Calls . . . . . . . . . . . . . . . . . 320
States of a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Terminating Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Halting Threads and Waking Sleeping Threads . . . . . . . . . . . . . . . . . . . 324
Waiting for a Thread to Exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Foreground and Background Threads. . . . . . . . . . . . . . . . . . . . . . . . . . 326

Thread-Local Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
How Unmanaged Threads and COM Apartments Fit In . . . . . . . . . . . . . 330
Synchronizing Work Between Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Lightweight Synchronization with the Interlocked Class . . . . . . . . . . . . 332
Monitor Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Locking Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Win32 Synchronization Objects and WaitHandle . . . . . . . . . . . . . . . . . . 351
Using ThreadPool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Asynchronous Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
■CHAPTER 13 In Search of C# Canonical Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Reference Type Canonical Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Default to sealed Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Use the Non-Virtual Interface (NVI) Pattern . . . . . . . . . . . . . . . . . . . . . . 365
Is the Object Cloneable?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Is the Object Disposable?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Does the Object Need a Finalizer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
What Does Equality Mean for This Object? . . . . . . . . . . . . . . . . . . . . . . 382
If You Override Equals, Override GetHashCode Too . . . . . . . . . . . . . . . . 388
Does the Object Support Ordering? . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Is the Object Formattable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Is the Object Convertible?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Prefer Type Safety at All Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Using Immutable Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Value Type Canonical Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Override Equals for Better Performance . . . . . . . . . . . . . . . . . . . . . . . . 406
Do Values of This Type Support Any Interfaces? . . . . . . . . . . . . . . . . . . 410

Implement Type-Safe Forms of Interface Members and
Derived Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
■CONTENTS xiii
8733fmCMP4.qrk 10/16/07 11:04 AM Page xiii
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Checklist for Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Checklist for Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
■CHAPTER 14 Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Introduction to Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
How Does the Compiler Find Extension Methods? . . . . . . . . . . . . . . . . 418
Under the Covers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Code Readability vs. Code Understandability. . . . . . . . . . . . . . . . . . . . . 422
Recommendations for Use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Consider Extension Methods Over Inheritance . . . . . . . . . . . . . . . . . . . 423
Isolate Extension Methods in Separate Namespace . . . . . . . . . . . . . . . 424
Changing a Type’s Contract Can Break Extension Methods . . . . . . . . . . 425
Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Operation Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Custom Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Borrowing from Functional Programming . . . . . . . . . . . . . . . . . . . . . . . 432
The Visitor Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
■CHAPTER 15 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Introduction to Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Lambda Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Expression Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Operating on Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Functions As Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Useful Applications of Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 453

Iterators and Generators Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Closures (Variable Capture) and Memoization. . . . . . . . . . . . . . . . . . . . 456
Currying. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Anonymous Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
■CHAPTER 16 LINQ: Language Integrated Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
A Bridge to Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Extension Methods and Lambda Expressions Revisited . . . . . . . . . . . . 468
Standard Query Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
■CONTENTSxiv
8733fmCMP4.qrk 10/16/07 11:04 AM Page xiv
C# Query Keywords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
The from Clause and Range Variables . . . . . . . . . . . . . . . . . . . . . . . . . 470
The join Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
The where Clause and Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
The orderby Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
The select Clause and Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
The let Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
The group Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
The into Clause and Continuations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
The Virtues of Being Lazy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
C# Iterators Foster Laziness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Subverting Laziness. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Executing Queries Immediately . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Expression Trees Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Techniques from Functional Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Custom Standard Query Operators and Lazy Evaluation . . . . . . . . . . . . 486
Replacing foreach Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494

■APPENDIX References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Blogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
■CONTENTS xv
8733fmCMP4.qrk 10/16/07 11:04 AM Page xv
8733fmCMP4.qrk 10/16/07 11:04 AM Page xvi
Foreword
Programming is exhilarating. It is a breathtaking venture through problems of the mind accented
by beautiful vistas as solutions are discovered. Throughout the process, a programmer’s view of the
problems he solves and the solutions to these problems is formed through the lens of the language
that he uses and thinks in. So, it is important for a programmer to become intimately familiar with
that language.
The problems that developers work on are as varied as the developers themselves. They range
from medical applications to insurance software, from multimedia applications to data mining
tools. The evolution of C# really is about the evolution of the problems that developers face. As the
problems become ever more complex, the language becomes simpler and more powerful to deal
with the complexity.
C# began its life as a way to describe reusable components that run in a variety of execution
environments. During this phase, it solidified its role as a great language for describing the architec-
ture of components and systems while embracing and extending its roots as a C-like language. One
of the major contributions it added was a fully object-oriented type system that unifies the con-
cepts of primitive and complex types and garbage collection.
While the first version of C# was a major accomplishment, the second release became the
defining moment for the language. The type system became much richer with the inclusion of
generics. The language also began to provide features like iterators and anonymous methods that
encourage simpler and more elegant designs. These features enabled more flexible and powerful
frameworks to be developed.
The third version of C# really breaks new ground. It blurs the line between code and data. It
introduces declarative query syntax. It empowers programmers through functional features. All of
these additions enable programmers to deal with the difficulties of data whether in-memory, in a

database, or from a web server. Programmers will find that these features put the fun back in pro-
gramming.
In this book, Trey Nash provides a refreshingly clear explanation of the C# language. He not
only understands C#, but he is also able to guide the reader through the process of learning to excel
in the language. He makes the journey to understanding enjoyable through thought-provoking
examples while taking the time to motivate each feature and demonstrate common and best prac-
tices. I am confident that the reader will write better code by reading this book.
Wes Dyer
C# Compiler and Language Design Teams
Microsoft Corporation
xvii
8733fmCMP4.qrk 10/16/07 11:04 AM Page xvii
8733fmCMP4.qrk 10/16/07 11:04 AM Page xviii
About the Author
■TREY NASH currently develops software at a market-leading security soft-
ware company. Prior to that, he called Macromedia Inc. home for five
years. At Macromedia, he worked on a cross-product engineering team for
several years, designing solutions for a wide range of products throughout
the company, including Flash and Fireworks. He specialized in
COM/DCOM using C/C++/ATL until the .NET revolution. He’s been glued
to computers ever since he scored his first, a TI-99/4A, when he was a mere
13 years old. He astounded his parents by turning a childhood obsession
into a decent paying career, much to their dismay. Trey received his bache-
lor of science and his master of engineering degrees in electrical
engineering from Texas A&M University. When he’s not sitting in front of a
computer, you can find him working in his garage, honing his skills in card magic (strange but true),
playing his piano, brushing up on a foreign language (Russian and Icelandic are the current
favorites), or playing ice hockey.
xix
8733fmCMP4.qrk 10/16/07 11:04 AM Page xix

8733fmCMP4.qrk 10/16/07 11:04 AM Page xx
a2226ea6064d65fa709ebdc214c2fb2c
About the Technical Reviewer
■SHAWN WILDERMUTH is a Microsoft MVP (C#), MCSD.NET, MCT and is the
founder of Wildermuth Consulting Services, LLC, a company that is dedi-
cated to delivering architecture, mentoring, and software solutions in the
Atlanta, Georgia area. He is also a speaker on the INETA Speaker’s Bureau
and has spoken at several national conferences on a variety of subjects. He is
currently teaching Silverlight across the country during his Silverlight Tour
().
Shawn is also the author of several books, including Pragmatic
ADO.NET for Addison-Wesley, and is also the co-author of four Microsoft
Certification Training Kits for Microsoft Press, as well as the upcoming book,
Prescriptive Data Architectures.
He has been writing articles for a number of years for a variety of magazines and web sites,
including MSDN, MSDN Online, DevSource, InformIT, Windows IT Pro, The ServerSide, ONDotNet
and Intel’s Rich Client series. Shawn has enjoyed building data-driven software for more than 20
years. He can be reached at his web site at .
xxi
8733fmCMP4.qrk 10/16/07 11:04 AM Page xxi
8733fmCMP4.qrk 10/16/07 11:04 AM Page xxii
Acknowledgments
Writing a book is a long and arduous process, during which I have received tons of great support,
which I greatly appreciate, from friends and family. The process would have been much more diffi-
cult, and arguably much less fruitful, without their support.
I would like to specifically call out the following individuals for their contribution to the first
edition of this work. I would like to thank (in no particular order) David Weller, Stephen Toub, Rex
Jaeschke, Vladimir Levin, Jerry Maresca, Chris Pels, Christopher T. McNabb, Brad Wilson, Peter
Partch, Paul Stubbs, Rufus Littlefield, Tomas Restrepo, John Lambert, Joan Murray, Sheri Cain,
Jessica D’Amico, Karen Gettman, Jim Huddleston, Richard Dal Porto, Gary Cornell, Brad Abrams,

Ellie Fountain, Nicole Abramowitz and the entire Apress crew, and finally, Shelley Nash.
During the development of the second edition, I would like to call out the following individuals
for their help and support (again in no particular order): Shawn Wildermuth, Sofia Marchant, Jim
Compton, Dominic Shakeshaft, Wes Dyer, Kelly Winquist, and Laura Cheu.
If I have left anyone out, it is purely my mistake and not one I intended. I could not have done
it without all of your support. Thank you all!
xxiii
8733fmCMP4.qrk 10/16/07 11:04 AM Page xxiii

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×