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

Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition phần 1 doc

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 (5.41 MB, 140 trang )

this print for content only—size & color not accurate
7" x 9-1/4" / CASEBOUND / MALLOY
(2.125 INCH BULK 1,400 pages 40# Thor)
The eXPeRT’s VOIce
®
In .neT
Andrew Troelsen
Pro
C# 2008
.NET 3.5 Platform
Exploring the .NET universe using curly brackets
BOOks fOR PROfessIOnals By PROfessIOnals
®
Pro C# 2008 and the .NET 3.5 Platform, FOURTH EDITION
Dear Reader,
The first edition of this book was released at the 2001 Tech·Ed conference in
Atlanta, Georgia. (I honestly can’t believe it was that long ago!) Since that time,
this text has been revised, tweaked, and enhanced to account for the changes
found within each release of the .NET platform (1.1, 2.0, 3.0, and now 3.5).
.NET 3.0 was more of an augmentative release, essentially providing three new
APIs: Windows Presentation Foundation (WPF), Windows Communication
Foundation (WCF), and Windows Workflow Foundation (WF). As you would
expect, coverage of the “W’s” has been expanded upon a great deal from the
previous Special Edition of this text.
Unlike .NET 3.0, .NET 3.5 provides dozens of new C# language features and .NET
APIs. This edition of the book will walk you through all of this new material using
the same readable approach (at least that is what I have been told!) as found
in the prior editions. Rest assured, you’ll find detailed coverage of Language
Integrated Query (LINQ), the C# 2008 language changes (automatic properties,
extension methods, anonymous types, etc.), and the numerous bells and whistles
of Visual Studio 2008.


If you’re checking out this book for the first time, understand that it targets
experienced software professionals and/or students of computer science (so
please don’t expect three chapters devoted to “for loops”). The mission of this
text is to provide you with a rock-solid foundation in the C# 2008 programming
language and the core aspects of the .NET platform (OOP, assemblies, file IO,
Windows Forms/WPF, ASP.NET, ADO.NET, WCF, WF, etc.). Once you digest the
information presented in these 33 chapters, you’ll be in a perfect position to
apply this knowledge to your specific programming assignments, and you’ll be
well equipped to explore the .NET universe on your own terms.
Take care and enjoy,
Andrew Troelsen
Microsoft MVP, Visual Developer—Visual C#
THE APRESS ROADMAP
Pro ASP.NET 3.5
in C# 2008
Pro VB 2008 and the
.NET 3.5 Platform
Accelerated C# 2008
Beginning C# 2008
Beginning C# 2008
Databases
Pro C# 2008 and the
.NET 3.5 Platform
Pro LINQ
Pro WPF in C# 2008
C#
Troelsen
cyan
MaGenTa
yellOW

Black
PanTOne 123 c
Author of
Pro VB 2008 and the
.NET 3.5 Platform
COM and .NET
Interoperability
Expert ASP.NET 2.0
Advanced Application
Design
Developer’s Workshop
to COM and ATL 3.0
ISBN-13: 978-1-59059-884-9
ISBN-10: 1-59059-884-9
9 781590 598849
5 5 9 9 9
US $59.99
Shelve in
Programming/
Microsoft/.NET
User level:
Intermediate–Advanced
www.apress.com
SOURCE CODE ONLINE
Free Companion eBook

For a limited time only.
See last page for details.
FOURTH EDITION
FOURTH EDITION

Free Companion eBook Available
Pro
2008
and the
.NET 3.5
Platform
and
the
For a limited time,
get the free, fully searchable
eBook—a $30 value!
See last page for details.
Offer ends June 30, 2008.
For a limited time,
get the free, fully searchable
eBook—a $30 value!
See last page for details.
Offer ends June 30, 2008.
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Andrew Troelsen
Pro C# 2008 and the
.NET 3.5 Platform
Fourth Edition
8849FM.qxd 10/19/07 9:45 AM Page i
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition
Copyright © 2007 by Andrew Troelsen
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: 978-1-59059-884-9
ISBN-10: 1-59059-884-9
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: Ewan Buckingham
Technical Reviewer: Gavin Smyth
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
Production Director | Project Manager: Grace Wong
Senior Copy Editors: Ami Knox, Nicole Flores
Associate Production Director: Kari Brooks-Copony
Production Editor: Ellie Fountain
Compositor: Dina Quan
Proofreaders: April Eddy and Liz Welch
Indexer: Broccoli Information Management
Artist: Kinetic Publishing Services, LLC
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 in the Source Code/
Download section. You will need to answer questions pertaining to this book in order to successfully
download the code.
8849FM.qxd 10/19/07 9:45 AM Page ii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
This edition of the text is dedicated to Mikko the wonder cat, life at 412, and my wonderful
wife, Amanda, who patiently waited for me to finish yet another book.
8849FM.qxd 10/19/07 9:45 AM Page iii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Contents
About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Part 1
■ ■ ■
Introducing C# and the .NET Platform
■CHAPTER 1 The Philosophy of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Understanding the Previous State of Affairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The .NET Solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Introducing the Building Blocks of the .NET Platform (the CLR, CTS,
and CLS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Additional .NET-Aware Programming Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . 9
An Overview of .NET Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Understanding the Common Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Understanding the Common Language Specification . . . . . . . . . . . . . . . . . . . . . . 20
Understanding the Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The Assembly/Namespace/Type Distinction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Exploring an Assembly Using ildasm.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Exploring an Assembly Using Lutz Roeder’s Reflector. . . . . . . . . . . . . . . . . . . . . . 31
Deploying the .NET Runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
The Platform-Independent Nature of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
■CHAPTER 2 Building C# Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Role of the .NET Framework 3.5 SDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Building C# Applications Using csc.exe. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Building .NET Applications Using TextPad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Building .NET Applications Using Notepad++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Building .NET Applications Using SharpDevelop. . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Building .NET Applications Using Visual C# 2008 Express . . . . . . . . . . . . . . . . . . 50
Building .NET Applications Using Visual Studio 2008 . . . . . . . . . . . . . . . . . . . . . . 52
A Partial Catalog of Additional .NET Development Tools . . . . . . . . . . . . . . . . . . . . 64
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
iv
8849FM.qxd 10/19/07 9:45 AM Page iv
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Part 2
■ ■ ■
Core C# Programming Constructs
■CHAPTER 3 Core C# Programming Constructs, Part I . . . . . . . . . . . . . . . . . . . . . . . . . 69
The Anatomy of a Simple C# Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
An Interesting Aside: Some Additional Members of the
System.Environment Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

The System.Console Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
System Data Types and C# Shorthand Notation. . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Understanding the System.String Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Narrowing and Widening Data Type Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . 95
C# Iteration Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Decision Constructs and the Relational/Equality Operators. . . . . . . . . . . . . . . . . 102
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
■CHAPTER 4 Core C# Programming Constructs, Part II. . . . . . . . . . . . . . . . . . . . . . . . 107
Methods and Parameter Modifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Understanding Member Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Array Manipulation in C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Understanding the Enum Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Understanding the Structure Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Understanding Value Types and Reference Types . . . . . . . . . . . . . . . . . . . . . . . . 129
Value and Reference Types: Final Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Understanding C# Nullable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
■CHAPTER 5 Defining Encapsulated Class Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Introducing the C# Class Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Understanding Class Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
The Role of the this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Understanding the static Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Defining the Pillars of OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
C# Access Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The First Pillar: C#’s Encapsulation Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Understanding Constant Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Understanding Read-Only Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Understanding Partial Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Documenting C# Source Code via XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Visualizing the Fruits of Our Labor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
■CHAPTER 6 Understanding Inheritance and Polymorphism . . . . . . . . . . . . . . . . . . 185
The Basic Mechanics of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Revising Visual Studio Class Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
The Second Pillar: The Details of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Programming for Containment/Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
■CONTENTS v
8849FM.qxd 10/19/07 9:45 AM Page v
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
■CONTENTSvi
The Third Pillar: C#’s Polymorphic Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Understanding Base Class/Derived Class Casting Rules . . . . . . . . . . . . . . . . . . . 210
The Master Parent Class: System.Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
■CHAPTER 7 Understanding Structured Exception Handling. . . . . . . . . . . . . . . . . . . 219
Ode to Errors, Bugs, and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
The Role of .NET Exception Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
The Simplest Possible Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Configuring the State of an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
System-Level Exceptions (System.SystemException) . . . . . . . . . . . . . . . . . . . . . 230
Application-Level Exceptions (System.ApplicationException) . . . . . . . . . . . . . . . 231
Processing Multiple Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
The Finally Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Who Is Throwing What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
The Result of Unhandled Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Debugging Unhandled Exceptions Using Visual Studio . . . . . . . . . . . . . . . . . . . . 242
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
■CHAPTER 8 Understanding Object Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Classes, Objects, and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

The Basics of Object Lifetime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
The Role of Application Roots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Understanding Object Generations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The System.GC Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Building Finalizable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Building Disposable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Building Finalizable and Disposable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Part 3
■ ■ ■
Advanced C# Programming Constructs
■CHAPTER 9 Working with Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Understanding Interface Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Defining Custom Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Implementing an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Invoking Interface Members at the Object Level . . . . . . . . . . . . . . . . . . . . . . . . . 276
Interfaces As Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Interfaces As Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Arrays of Interface Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Implementing Interfaces Using Visual Studio 2008 . . . . . . . . . . . . . . . . . . . . . . . 282
Resolving Name Clashes via Explicit Interface Implementation . . . . . . . . . . . . . 283
Designing Interface Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Building Enumerable Types (IEnumerable and IEnumerator) . . . . . . . . . . . . . . . 289
Building Cloneable Objects (ICloneable) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
8849FM.qxd 10/19/07 9:45 AM Page vi
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Building Comparable Objects (IComparable). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Understanding Callback Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308

■CHAPTER 10 Collections and Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
The Interfaces of the System.Collections Namespace. . . . . . . . . . . . . . . . . . . . . 309
The Class Types of System.Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
System.Collections.Specialized Namespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
The Boxing, Unboxing, and System.Object Relationship . . . . . . . . . . . . . . . . . . . 316
The Issue of Type Safety and Strongly Typed Collections . . . . . . . . . . . . . . . . . . 319
The System.Collections.Generic Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Creating Custom Generic Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Creating Generic Structures and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Creating a Custom Generic Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Creating Generic Base Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Creating Generic Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
■CHAPTER 11 Delegates, Events, and Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Understanding the .NET Delegate Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Defining a Delegate in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
The System.MulticastDelegate and System.Delegate Base Classes . . . . . . . . . 344
The Simplest Possible Delegate Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Retrofitting the Car Type with Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
A More Elaborate Delegate Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Understanding Delegate Covariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Creating Generic Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Understanding C# Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
The Generic EventHandler<T> Delegate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Understanding C# Anonymous Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Understanding Method Group Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
The C# 2008 Lambda Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
■CHAPTER 12 Indexers, Operators, and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Understanding Indexer Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Understanding Operator Overloading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Understanding Custom Type Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Working with Pointer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
C# Preprocessor Directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
■CHAPTER 13 C# 2008 Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Understanding Implicitly Typed Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Understanding Automatic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Understanding Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
■CONTENTS vii
8849FM.qxd 10/19/07 9:45 AM Page vii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Understanding Partial Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Understanding Object Initializer Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Understanding Anonymous Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
■CHAPTER 14 An Introduction to LINQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Understanding the Role of LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
A First Look at LINQ Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
LINQ and Generic Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
LINQ and Nongeneric Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
The Internal Representation of LINQ Query Operators . . . . . . . . . . . . . . . . . . . . . 459
Investigating the C# LINQ Query Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
LINQ Queries: An Island unto Themselves?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
PART 4
■ ■ ■
Programming with .NET Assemblies
■CHAPTER 15 Introducing .NET Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475

Defining Custom Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
The Role of .NET Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Understanding the Format of a .NET Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Building and Consuming a Single-File Assembly . . . . . . . . . . . . . . . . . . . . . . . . . 486
Building and Consuming a Multifile Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Understanding Private Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Understanding Shared Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Consuming a Shared Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Configuring Shared Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Investigating the Internal Composition of the GAC . . . . . . . . . . . . . . . . . . . . . . . . 516
Understanding Publisher Policy Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Understanding the <codeBase> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
The System.Configuration Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
The Machine Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
■CHAPTER 16 Type Reflection, Late Binding, and Attribute-Based
Programming
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
The Necessity of Type Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Understanding Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Building a Custom Metadata Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Dynamically Loading Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Reflecting on Shared Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Understanding Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
■CONTENTSviii
8849FM.qxd 10/19/07 9:45 AM Page viii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Understanding Attributed Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Building Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546

Assembly-Level (and Module-Level) Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Reflecting on Attributes Using Early Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Reflecting on Attributes Using Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Putting Reflection, Late Binding, and Custom Attributes in Perspective . . . . . . 553
Building an Extendable Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
■CHAPTER 17 Processes, AppDomains, and Object Contexts. . . . . . . . . . . . . . . . . . . 561
Reviewing Traditional Win32 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Interacting with Processes Under the .NET Platform . . . . . . . . . . . . . . . . . . . . . . 563
Understanding .NET Application Domains. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Understanding Object Context Boundaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
Summarizing Processes, AppDomains, and Context . . . . . . . . . . . . . . . . . . . . . . 581
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
■CHAPTER 18 Building Multithreaded Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
The Process/AppDomain/Context/Thread Relationship . . . . . . . . . . . . . . . . . . . . 583
A Brief Review of the .NET Delegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
The Asynchronous Nature of Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Invoking a Method Asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
The System.Threading Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
The System.Threading.Thread Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Programmatically Creating Secondary Threads . . . . . . . . . . . . . . . . . . . . . . . . . . 597
The Issue of Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Programming with Timer Callbacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Understanding the CLR ThreadPool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
The Role of the BackgroundWorker Component. . . . . . . . . . . . . . . . . . . . . . . . . . 612
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
■CHAPTER 19 Understanding CIL and the Role of Dynamic Assemblies . . . . . . . . 617
Reflecting on the Nature of CIL Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Examining CIL Directives, Attributes, and Opcodes . . . . . . . . . . . . . . . . . . . . . . . 618
Pushing and Popping: The Stack-Based Nature of CIL . . . . . . . . . . . . . . . . . . . . 620

Understanding Round-Trip Engineering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Understanding CIL Directives and Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
.NET Base Class Library, C#, and CIL Data Type Mappings . . . . . . . . . . . . . . . . 635
Defining Type Members in CIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
Examining CIL Opcodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Building a .NET Assembly with CIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Understanding Dynamic Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
■CONTENTS ix
8849FM.qxd 10/19/07 9:45 AM Page ix
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Part 5
■ ■ ■
Introducing the .NET Base Class Libraries
■CHAPTER 20 File I/O and Isolated Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Exploring the System.IO Namespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
The Directory(Info) and File(Info) Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
Working with the DirectoryInfo Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Working with the Directory Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Working with the DriveInfo Class Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Working with the FileInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Working with the File Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
The Abstract Stream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Working with StreamWriters and StreamReaders . . . . . . . . . . . . . . . . . . . . . . . . 677
Working with StringWriters and StringReaders . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Working with BinaryWriters and BinaryReaders. . . . . . . . . . . . . . . . . . . . . . . . . . 682
Programmatically “Watching” Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Performing Asynchronous File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
Understanding the Role of Isolated Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687

A Primer on Code Access Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
An Overview of Isolated Storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
Obtaining a Store Using IsolatedStorageFile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Isolated Storage in Action: ClickOnce Deployment. . . . . . . . . . . . . . . . . . . . . . . . 707
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
■CHAPTER 21 Introducing Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Understanding Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Configuring Objects for Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
Choosing a Serialization Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Serializing Objects Using the BinaryFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Serializing Objects Using the SoapFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Serializing Objects Using the XmlSerializer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Serializing Collections of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Customizing the Serialization Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
■CHAPTER 22 ADO.NET Part I: The Connected Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
A High-Level Definition of ADO.NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
Understanding ADO.NET Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
Additional ADO.NET Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
The Types of the System.Data Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
Abstracting Data Providers Using Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Creating the AutoLot Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
The ADO.NET Data Provider Factory Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Understanding the Connected Layer of ADO.NET. . . . . . . . . . . . . . . . . . . . . . . . . 755
Working with Data Readers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Building a Reusable Data Access Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
■CONTENTSx
8849FM.qxd 10/19/07 9:45 AM Page x
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -

Creating a Console UI–Based Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
Asynchronous Data Access Using SqlCommand . . . . . . . . . . . . . . . . . . . . . . . . . 775
Understanding Database Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
■CHAPTER 23 ADO.NET Part II: The Disconnected Layer. . . . . . . . . . . . . . . . . . . . . . . . 783
Understanding the Disconnected Layer of ADO.NET . . . . . . . . . . . . . . . . . . . . . . 783
Understanding the Role of the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Working with DataColumns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Working with DataRows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
Working with DataTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Binding DataTable Objects to User Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
Filling DataSet/DataTable Objects Using Data Adapters . . . . . . . . . . . . . . . . . . . 808
Revisiting AutoLotDAL.dll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Navigating Multitabled DataSet Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
The Data Access Tools of Visual Studio 2008. . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Decoupling Autogenerated Code from the UI Layer . . . . . . . . . . . . . . . . . . . . . . . 831
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
■CHAPTER 24 Programming with the LINQ APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837
The Role of LINQ to ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837
Programming with LINQ to DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838
Programming with LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
Generating Entity Classes Using SqlMetal.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
Building Entity Classes Using Visual Studio 2008. . . . . . . . . . . . . . . . . . . . . . . . . 854
Manipulating XML Documents Using LINQ to XML. . . . . . . . . . . . . . . . . . . . . . . . 857
Navigating an In-Memory Document. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865
■CHAPTER 25 Introducing Windows Communication Foundation. . . . . . . . . . . . . . . 867
A Potpourri of Distributed Computing APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867
The Role of WCF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
Investigating the Core WCF Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876

The Visual Studio WCF Project Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877
The Basic Composition of a WCF Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
The ABCs of WCF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Building a WCF Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Hosting the WCF Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
Building the WCF Client Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896
Using the WCF Service Library Project Template . . . . . . . . . . . . . . . . . . . . . . . . . 900
Hosting the WCF Service As a Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . 903
Invoking a Service Asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908
Designing WCF Data Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
■CONTENTS xi
8849FM.qxd 10/19/07 9:45 AM Page xi
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
■CHAPTER 26 Introducing Windows Workflow Foundation . . . . . . . . . . . . . . . . . . . . . 917
Defining a Business Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917
The Building Blocks of WF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
WF Assemblies, Namespaces, and Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Building a Simple Workflow-Enabled Application. . . . . . . . . . . . . . . . . . . . . . . . . 926
Examining the WF Engine Hosting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Invoking Web Services Within Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
Building a Reusable WF Code Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
A Brief Word Regarding Custom Activities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
Part 6
■ ■ ■
Desktop User Interfaces
■CHAPTER 27 Programming with Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
The Windows Forms Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955

Building a Simple Windows Forms Application (IDE-Free). . . . . . . . . . . . . . . . . . 956
The Visual Studio Windows Forms Project Template. . . . . . . . . . . . . . . . . . . . . . 961
The Anatomy of a Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
Responding to Mouse Activity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
Responding to Keyboard Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
Designing Dialog Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Rendering Graphical Data Using GDI+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
Building a Complete Windows Forms Application . . . . . . . . . . . . . . . . . . . . . . . . 990
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
■CHAPTER 28 Introducing Windows Presentation Foundation and XAML. . . . . . . 999
The Motivation Behind WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999
The Various Flavors of WPF Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
Investigating the WPF Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004
Building a (XAML-Free) WPF Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011
Additional Details of the Application Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015
Additional Details of the Window Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017
Building a (XAML-Centric) WPF Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021
Transforming Markup into a .NET Assembly. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
Separation of Concerns Using Code-Behind Files . . . . . . . . . . . . . . . . . . . . . . . 1029
The Syntax of XAML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
Building WPF Applications Using Visual Studio 2008. . . . . . . . . . . . . . . . . . . . . 1044
Processing XAML at Runtime: SimpleXamlPad.exe . . . . . . . . . . . . . . . . . . . . . . 1048
The Role of Microsoft Expression Blend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
■CONTENTSxii
8849FM.qxd 10/19/07 9:45 AM Page xii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
■CHAPTER 29 Programming with WPF Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055
A Survey of the WPF Control Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055

Declaring Controls in XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
Understanding the Role of Dependency Properties . . . . . . . . . . . . . . . . . . . . . . 1060
Understanding Routed Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
Working with Button Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
Working with CheckBoxes and RadioButtons . . . . . . . . . . . . . . . . . . . . . . . . . . . 1072
Working with the ListBox and ComboBox Types . . . . . . . . . . . . . . . . . . . . . . . . 1075
Working with Text Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
Controlling Content Layout Using Panels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083
Building a Window’s Frame Using Nested Panels . . . . . . . . . . . . . . . . . . . . . . . 1093
Understanding WPF Control Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1099
Understanding the WPF Data-Binding Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 1102
Data Conversion Using IValueConverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106
Binding to Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1108
Binding UI Elements to XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1112
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1116
■CHAPTER 30 WPF 2D Graphical Rendering, Resources, and Themes . . . . . . . . . 1117
The Philosophy of WPF Graphical Rendering Services. . . . . . . . . . . . . . . . . . . . 1117
Exploring the Shape-Derived Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124
Working with WPF Brushes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1127
Working with WPF Pens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1130
Exploring the Drawing-Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1130
The Role of UI Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1135
Understanding WPF’s Animation Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136
Understanding the WPF Resource System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1145
Defining and Applying Styles for WPF Controls . . . . . . . . . . . . . . . . . . . . . . . . . 1147
Altering a Control’s UI Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1156
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1160
Part 7
■ ■ ■
Building Web Applications with ASP.NET

■CHAPTER 31 Building ASP.NET Web Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163
The Role of HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163
Understanding Web Applications and Web Servers . . . . . . . . . . . . . . . . . . . . . . 1164
The Role of HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167
The Role of Client-Side Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1172
Submitting the Form Data (GET and POST). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1174
Building a Classic ASP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175
Problems with Classic ASP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177
The ASP.NET Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178
The ASP.NET Web Page Code Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179
Details of an ASP.NET Website Directory Structure . . . . . . . . . . . . . . . . . . . . . . 1190
The ASP.NET Page Compilation Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1192
■CONTENTS xiii
8849FM.qxd 10/19/07 9:45 AM Page xiii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
The Inheritance Chain of the Page Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194
Interacting with the Incoming HTTP Request . . . . . . . . . . . . . . . . . . . . . . . . . . . 1195
Interacting with the Outgoing HTTP Response . . . . . . . . . . . . . . . . . . . . . . . . . . 1198
The Life Cycle of an ASP.NET Web Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1200
The Role of the Web.config File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1203
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206
■CHAPTER 32 ASP.NET Web Controls, Themes, and Master Pages . . . . . . . . . . . . 1207
Understanding the Nature of Web Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1207
The System.Web.UI.Control Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1209
The System.Web.UI.WebControls.WebControl Type. . . . . . . . . . . . . . . . . . . . . . 1213
Major Categories of ASP.NET Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
Building a Feature-Rich ASP.NET Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1215
The Role of the Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1231
Working with Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237

Positioning Controls Using HTML Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1244
■CHAPTER 33 ASP.NET State Management Techniques . . . . . . . . . . . . . . . . . . . . . . . 1245
The Issue of State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1245
ASP.NET State Management Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1247
Understanding the Role of ASP.NET View State . . . . . . . . . . . . . . . . . . . . . . . . . 1248
The Role of the Global.asax File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1251
Understanding the Application/Session Distinction . . . . . . . . . . . . . . . . . . . . . . 1254
Working with the Application Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1259
Maintaining Session Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1263
Understanding Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1267
The Role of the <sessionState> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1269
Understanding the ASP.NET Profile API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1272
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1279
Part 8
■ ■ ■
Appendixes
■APPENDIX A COM and .NET Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1283
The Scope of .NET Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1283
A Simple Example of .NET to COM Interop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1284
Investigating a .NET Interop Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1287
Understanding the Runtime Callable Wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . 1289
The Role of COM IDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1292
Using a Type Library to Build an Interop Assembly . . . . . . . . . . . . . . . . . . . . . . 1296
Building a More Elaborate COM Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1299
Examining the Interop Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1301
Understanding COM to .NET Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1305
The Role of the CCW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1306
The Role of the .NET Class Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1307
■CONTENTSxiv

8849FM.qxd 10/19/07 9:45 AM Page xiv
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Building Your .NET Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1308
Generating the Type Library and Registering the .NET Types. . . . . . . . . . . . . . 1310
Examining the Exported Type Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1311
Building a Visual Basic 6.0 Test Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1312
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1313
■APPENDIX B Platform-Independent .NET Development with Mono . . . . . . . . . . . 1315
The Platform-Independent Nature of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1315
Obtaining and Installing Mono . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1318
The Mono Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1321
Building .NET Applications with Mono. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1323
Suggestions for Further Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1330
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1331
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1333
■CONTENTS xv
8849FM.qxd 10/19/07 9:45 AM Page xv
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
About the Author
■ANDREW TROELSEN is a Microsoft MVP (Visual C#) and a partner, trainer, and
consultant with Intertech Training (
), a .NET and
J2EE developer education center. He is the author of numerous books, includ-
ing
Developer’s Workshop to COM and ATL 3.0 (Wordware Publishing, 2000),
COM and .NET Interoperability (Apress, 2002), Visual Basic .NET and the .NET
Platform: An Advanced Guide
(Apress, 2001), and the award-winning C# and

the .NET Platform
(Apress, 2003). Andrew has also authored numerous articles
on .NET for MSDN online, DevX, and
MacTech, and is frequently a speaker at
various .NET conferences and user groups.
Andrew lives in Minneapolis, Minnesota, with his wife, Amanda. He spends his free time wait-
ing for the Wild to win the Stanley Cup, but has given up all hope of the Vikings winning a Super
Bowl and feels quite strongly that the Timberwolves will never get back to the playoffs until current
management is replaced.
xvi
8849FM.qxd 10/19/07 9:45 AM Page xvi
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
About the Technical Reviewer
■GAVIN SMYTH is a professional software engineer with more years of experience in development
than he cares to admit on projects ranging from device drivers to distributed web applications;
under platforms as diverse as 8-bit “bare metal,” embedded real-time operating systems, Unix, and
Windows; and in languages including assembler, C++, Ada, and C#, among a good many others. He
has worked for clients such as BT and Nortel, and is currently employed by Microsoft. Gavin has
published a few pieces of technical prose in the past (
EXE, where are you now?) but finds criticizing
other people’s work much more fulfilling. Beyond that, when he’s not battling weeds and ants in the
garden, he tries to persuade LEGO robots to do what he wants them to do (it’s for the kids’ benefit—
honest).
xvii
8849FM.qxd 10/19/07 9:45 AM Page xvii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Acknowledgments
While I might be the only name seen on the front of this book, this text would never be printed

without the aid of numerous talented people. Allow me to offer some heartfelt words of thanks to
the many, many people who made this book possible.
First and foremost, thanks to all of the people at Apress, whom I have had the pleasure of
working with for many years now. You are all extremely talented people who do a wonderful job of
transforming my original Word documents into polished prose. Thank you so much. Looking for-
ward to working with you all on the next book (well, after I take a sanity break from this book).
Special thanks to my technical editor, Gavin, who has offered me many words of wisdom that
I feel make this edition of the book better than ever. As always, any remaining typos or technical
errors are my responsibility alone.
Last but not least, thanks to my family, friends, and coworkers who put up with my occasional
grumpy demeanor, which sadly presented itself once or twice during the final phases of this
manuscript.
xviii
8849FM.qxd 10/19/07 9:45 AM Page xviii
8bbe9bd62dce7511c66cccbd759f8afe
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Introduction
This book has existed (in one form or another) since the first edition of C# and the .NET Platform
was published in conjunction with the release of .NET 1.0 Beta 2, circa the summer of 2001. Since
that point, I have been extremely happy and grateful to see that this text was very well received by
the press and, most important, by readers. Over the years it was nominated as a Jolt Award finalist
(I lost . . . crap!) and for the 2003 Referenceware Excellence Award in the programming book cate-
gory (I won? Cool!).
Since that point, I have worked to keep the book current with each release of the .NET platform,
including a limited printing of a Special Edition, which introduced the technologies of .NET 3.0
(Windows Presentation Foundation, Windows Communication Foundation, and Windows Workflow
Foundation) as well as offered previews of several forthcoming technologies, which we now know as
LINQ.
The fourth edition of this text, which you hold in your hands, is a massive retelling of the previ-

ous manuscript to account for all of the major changes that are found within .NET 3.5. Not only will
you find numerous brand-new chapters, you will find many of the previous chapters have been
expanded in great detail.
As with the earlier editions, this edition presents the C# programming language and .NET base
class libraries using a friendly and approachable tone. I have never understood the need some
technical authors have to spit out prose that reads more like a GRE vocabulary study guide than a
readable book. As well, this new edition remains focused on providing you with the information you
need to build software solutions today, rather than spending too much time examining esoteric
details that few individuals will ever actually care about.
We’re a Team, You and I
Technology authors write for a demanding group of people (I should know—I’m one of them).
You know that building software solutions using any platform (.NET, J2EE, COM, etc.) is extremely
detailed and is very specific to your department, company, client base, and subject matter. Perhaps
you work in the electronic publishing industry, develop systems for the state or local government,
or work at NASA or a branch of the military. Speaking for myself, I have developed children’s educa-
tional software, various n-tier systems, and projects within the medical and financial industries. The
chances are almost 100 percent that the code you write at your place of employment has little to do
with the code I write at mine (unless we happened to work together previously!).
Therefore, in this book, I have deliberately chosen to avoid creating examples that tie the
example code to a specific industry or vein of programming. Given this, I explain C#, OOP, the CLR,
and the .NET 3.5 base class libraries using industry-agnostic examples. Rather than having every
blessed example fill a grid with data, calculate payroll, or whatnot, I’ll stick to subject matter we can
all relate to: automobiles (with some geometric structures and employees thrown in for good meas-
ure). And that’s where you come in.
My job is to explain the C# programming language and the core aspects of the .NET platform
the best I possibly can. As well, I will do everything I can to equip you with the tools and strategies
you need to continue your studies at this book’s conclusion.
Your job is to take this information and apply it to your specific programming assignments.
I obviously understand that your projects most likely don’t revolve around automobiles with pet
xix

8849FM.qxd 10/19/07 9:45 AM Page xix
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
names, but that’s what applied knowledge is all about! Rest assured, once you understand the con-
cepts presented within this text, you will be in a perfect position to build .NET solutions that map to
your own unique programming environment.
An Overview of This Book
Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition is logically divided into eight distinct parts,
each of which contains a number of related chapters. If you have read the earlier editions of this
text, you will quickly notice a number of changes. For example, several topics (such as core C# con-
structs, object-oriented programming, and platform-independent .NET development) have been
expanded into several dedicated chapters. Furthermore, this edition of the text contains numerous
new chapters to account for .NET 3.0–3.5 programming features (LINQ, WCF, WPF, WF, etc.). Here is
a part-by-part and chapter-by-chapter breakdown of the text.
Part 1: Introducing C# and the .NET Platform
The purpose of Part 1 is to acclimate you to the nature of the .NET platform and various develop-
ment tools (many of which are open source) used during the construction of .NET applications.
Along the way, you will also check out some basic details of the C# programming language and the
.NET type system.
Chapter 1: The Philosophy of .NET
This first chapter functions as the backbone for the remainder of the text. We begin by examining
the world of traditional Windows development and uncover the shortcomings with the previous
state of affairs. The primary goal of this chapter, however, is to acquaint you with a number of .NET-
centric building blocks, such as the common language runtime (CLR), Common Type System (CTS),
Common Language Specification (CLS), and base class libraries. Here, you will take an initial look at
the C# programming language and the .NET assembly format, and get an overview the platform-
independent nature of the .NET platform (Appendix B will examine this topic in greater detail).
Chapter 2: Building C# Applications
The goal of this chapter is to introduce you to the process of compiling C# source code files using
various tools and techniques. First, you will learn how to make use of the command-line compiler

(
csc.exe) and C# response files. Over the remainder of the chapter, you will examine numerous
code editors and integrated development environments (IDEs), including TextPad, Notepad++,
SharpDevelop, Visual C# 2008 Express, and Visual Studio 2008. As well, you will be exposed to a
number of additional programming tools that any .NET developer should have in their back pocket.
Part 2: Core C# Programming Constructs
The topics presented in this part of the book are quite important, as they will be used regardless of
which type of .NET software you intend to develop (web applications, desktop GUI applications,
code libraries, Windows services, etc.). Here, you will come to understand the core constructs of the
C# language, including the details of object-oriented programming (OOP). As well, this part will
examine how to process runtime exceptions and dive into the details of .NET’s garbage collection
services.
■INTRODUCTIONxx
8849FM.qxd 10/19/07 9:45 AM Page xx
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Chapter 3: Core C# Programming Constructs, Part I
This chapter begins your formal investigation of the C# programming language. Here you will learn
about the role of the
Main() method and numerous details regarding the intrinsic data types of the
.NET platform, including the manipulation of textual data using
System.String and System.Text.
StringBuilder
. You will also examine iteration and decision constructs, narrowing and widening
operations, and use of the
unchecked keyword.
Chapter 4: Core C# Programming Constructs, Part II
This chapter completes your examination of the core aspects of C#, beginning with the construc-
tion of overloaded type methods and defining parameters via the
out, ref, and params keywords.

You will also learn how to create and manipulate arrays of data, define nullable data types (with the
? and ?? operators), and understand the distinction between value types (including enumerations
and custom structures) and
reference types.
Chapter 5: Defining Encapsulated Class Types
This chapter begins your examination of object-oriented programming (OOP) using the C# pro-
gramming language. Once we qualify the pillars of OOP (encapsulation, inheritance, and
polymorphism), the remainder of this chapter will examine how to build robust class types using
constructors, properties, static members, constants, and read-only fields. We wrap up with an
examination of partial type definitions and C#’s XML code documentation syntax.
Chapter 6: Understanding Inheritance and Polymorphism
Here, you will examine the remaining pillars of OOP (inheritance and polymorphism), which allow
you to build families of related class types. During this time, you will examine the role of virtual
methods, abstract methods (and abstract base classes), and the nature of the
polymorphic interface.
Last but not least, this chapter will explain the role of the supreme base class of the .NET platform,
System.Object.
Chapter 7: Understanding Structured Exception Handling
The point of this chapter is to discuss how to handle runtime anomalies in your code base through
the use of structured exception handling. Not only will you learn about the C# keywords that allow
you to handle such problems (
try, catch, throw, and finally), but you will also come to understand
the distinction between application-level and system-level exceptions. In addition, this chapter
examines various tools within Visual Studio 2008 that allow you to debug the exceptions that have
escaped your view.
Chapter 8: Understanding Object Lifetime
The final chapter of this part examines how the CLR manages memory using the .NET garbage col-
lector. Here you will come to understand the role of application roots, object generations, and the
System.GC type. Once you understand the basics, the remainder of this chapter covers the topics of
disposable objects (via the IDisposable interface) and the finalization process (via the System.

Object.Finalize()
method).
■INTRODUCTION xxi
8849FM.qxd 10/19/07 9:45 AM Page xxi
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Part 3: Advanced C# Programming Constructs
This section of the book will deepen your understanding of the C# language, by examining a num-
ber of more advanced (but very important) concepts. Here, you will complete your examination of
the .NET type system by examining interfaces and delegates. As well, you will learn about the role of
generics and the numerous new language features of C# 2008, and take an initial look at Language
Integrated Query (LINQ).
Chapter 9: Working with Interfaces
The material in this chapter builds upon your understanding of object-based development by
covering the topic of interface-based programming. Here, you will learn how to define types that
support multiple behaviors, how to discover these behaviors at runtime, and how to selectively hide
particular behaviors using
explicit interface implementation. In addition to examining a number of
predefined .NET interface types, you will also learn how to make use of custom interfaces to build
an ad hoc event architecture.
Chapter 10: Collections and Generics
This chapter begins by examining the collection types of the System.Collections namespace, which
has been part of the .NET platform since its initial release. However, since the release of .NET 2.0,
the C# programming language offers support for
generics. As you will see, generic programming
greatly enhances application performance and type safety. Not only will you explore various generic
types within the
System.Collections.Generic namespace, but you will also learn how to build your
own generic methods and types (with and without constraints).
Chapter 11: Delegates, Events, and Lambdas

The purpose of Chapter 11 is to demystify the delegate type. Simply put, a .NET delegate is an object
that “points” to other methods in your application. Using this pattern, you are able to build systems
that allow multiple objects to engage in a two-way conversation. After you have examined the use of
.NET delegates, you will then be introduced to the C#
event keyword, which is used to simplify the
manipulation of raw delegate programming. You wrap up by investigating the role of the C# 2008
lambda operator (
=>) and exploring the connection between delegates, anonymous methods, and
lambda expressions.
Chapter 12: Indexers, Operators, and Pointers
This chapter deepens your understanding of the C# programming language by introducing a num-
ber of advanced programming techniques. Here, you will learn how to overload operators and
create custom conversion routines (both implicit and explicit) for your types. As well, you will learn
how to build and interact with
type indexers, and manipulate C-style pointers using an “unsafe”
code context.
Chapter 13: C# 2008 Language Features
With the release of .NET 3.5, the C# language has been enhanced to support a great number of new
programming constructs, many of which are used to enable the LINQ API (which you will begin to
examine in Chapter 14). Here, you will learn the role of implicit typing of local variables, partial
methods, automatic properties, extension methods, anonymous types, and object initialization
syntax.
■INTRODUCTIONxxii
8849FM.qxd 10/19/07 9:45 AM Page xxii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Chapter 14: An Introduction to LINQ
This chapter will begin your examination of Language Integrated Query (LINQ), which could easily
be considered the most intriguing aspect of .NET 3.5. As you will see in this chapter, LINQ allows
you to build strongly typed

query expressions, which can be applied to a number of LINQ targets to
manipulate “data” in the broadest sense of the word. Here, you will learn about LINQ to Objects,
which allows you to apply LINQ expressions to containers of data (arrays, collections, custom
types). This information will serve you well when we examine how to apply LINQ expressions to
relational databases (via LINQ to ADO) and XML documents (à la LINQ to XML) later in Chapter 24.
Part 4: Programming with .NET Assemblies
Part 4 dives into the details of the .NET assembly format. Not only will you learn how to deploy and
configure .NET code libraries, but you will also come to understand the internal composition of a
.NET binary image. This part also explains the role of .NET attributes and the construction of multi-
threaded applications. Later chapters examine some fairly advanced topics such as object context,
CIL code, and dynamic assemblies.
Chapter 15: Introducing .NET Assemblies
From a very high level, assembly is the term used to describe a managed *.dll or *.exe binary file.
However, the true story of .NET assemblies is far richer than that. Here you will learn the distinction
between single-file and multifile assemblies, and how to build and deploy each entity. You’ll exam-
ine how private and shared assemblies may be configured using XML-based
*.config files and
publisher policy assemblies. Along the way, you will investigate the internal structure of the global
assembly cache (GAC) and the role of the .NET Framework configuration utility.
Chapter 16: Type Reflection, Late Binding, and Attribute-Based Programming
Chapter 16 continues our examination of .NET assemblies by checking out the process of runtime
type discovery via the
System.Reflection namespace. Using these types, you are able to build appli-
cations that can read an assembly’s metadata on the fly. You will learn how to dynamically load and
create types at runtime using
late binding. The final topic of this chapter explores the role of .NET
attributes (both standard and custom). To illustrate the usefulness of each of these topics, the chap-
ter concludes with the construction of an extendable Windows Forms application.
Chapter 17: Processes, AppDomains, and Object Contexts
Now that you have a solid understanding of assemblies, this chapter dives deeper into the composi-

tion of a loaded .NET executable. The goal of this chapter is to illustrate the relationship between
processes, application domains, and contextual boundaries. These topics provide the proper foun-
dation for the topic of the following chapter, where we examine the construction of multithreaded
applications.
Chapter 18: Building Multithreaded Applications
This chapter examines how to build multithreaded applications and illustrates a number of tech-
niques you can use to author thread-safe code. The chapter opens by revisiting the .NET delegate
type in order to understand a delegate’s intrinsic support for asynchronous method invocations.
Next, you will investigate the types within the
System.Threading namespace. You will look at numer-
ous types (
Thread, ThreadStart, etc.) that allow you to easily create additional threads of execution.
We wrap up by examining the
BackgroundWorker type, which greatly simplifies the creation of
threads from within a desktop user interface.
■INTRODUCTION xxiii
8849FM.qxd 10/19/07 9:45 AM Page xxiii
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -
Chapter 19: Understanding CIL and the Role of Dynamic Assemblies
The goal of the final chapter of this part is twofold. In the first half (more or less), you will examine
the syntax and semantics of CIL in much greater detail than in previous chapters. The remainder of
this chapter covers the role of the
System.Reflection.Emit namespace. Using these types, you are
able to build software that is capable of generating .NET assemblies in memory at runtime. For-
mally speaking, assemblies defined and executed in memory are termed
dynamic assemblies.
Part 5: Introducing the .NET Base Class Libraries
By this point in the text, you have a solid handle on the C# language and the details of the .NET
assembly format. Part 5 leverages your newfound knowledge by exploring a number of commonly

used services found within the base class libraries, including file I/O and database access using
ADO.NET. This part also covers the construction of distributed applications using Windows Com-
munication Foundation (WCF) and workflow-enabled applications that make use of the Windows
Workflow Foundation (WF) API.
Chapter 20: File I/O and Isolated Storage
The System.IO namespace allows you to interact with a machine’s file and directory structure. Over
the course of this chapter, you will learn how to programmatically create (and destroy) a directory
system as well as move data into and out of various streams (file based, string based, memory
based, etc.). The latter part of this chapter examines the role of
isolated storage, which allows you to
persist per-user data into a safe sandbox, regardless of the security settings of a target machine. To
understand certain aspects of the
System.IO.IsolatedStorage API, you will also receive an overview
of Code Access Security (CAS).
Chapter 21: Introducing Object Serialization
This chapter examines the object serialization services of the .NET platform. Simply put, serializa-
tion
allows you to persist the state of an object (or a set of related objects) into a stream for later use.
Deserialization (as you might expect) is the process of plucking an object from the stream into
memory for consumption by your application. Once you understand the basics, you will then learn
how to customize the serialization process via the
ISerializable interface and a set of .NET
attributes.
Chapter 22: ADO.NET Part I: The Connected Layer
In this first of two database-centric chapters, you will learn about the ADO.NET programming API.
Specifically, this chapter will introduce the role of .NET data providers and how to communicate
with a relational database using the
connected layer of ADO.NET, represented by connection
objects, command objects, transaction objects, and data reader objects. Be aware that this chapter
will also walk you through the creation of a custom database and a data access library that will be

used throughout the remainder of this text.
Chapter 23: ADO.NET Part II: The Disconnected Layer
This chapter continues your study of database manipulation by examining the disconnected layer
of ADO.NET. Here, you will learn the role of the DataSet type, data adapter objects, and numerous
tools of Visual Studio 2008 that can greatly simplify the creation of data-driven applications. Along
the way, you will learn how to bind
DataTable objects to user interface elements, such as the
GridView type of the Windows Forms API.
■INTRODUCTIONxxiv
8849FM.qxd 10/19/07 9:45 AM Page xxiv
www.free-ebooks-download.org
Simpo PDF Merge and Split Unregistered Version -

×