cyan yellOW MaGenTa Black PanTOne 123 c BOOks fOR PROfessIOnals By PROfessIOnals® The eXPeRT’s VOIce® In .neT Free Companion eBook Available FFoorr aa lliimmiitteedd ttiimmee,, Pro C# 2008 and the .NET 3.5 Platform, FourTh EdiTioN ggeett tthhee ffrreeee,, ffuullllyy sseeaarrcchhaabbllee Pro eeBBooookk——aa $$3300 vvaalluuee!! Dear Reader, SSeeee llaasstt ppaaggee ffoorr ddeettaaiillss.. The first edition of this book was released at the 2001 Tech·Ed conference in OOffffeerr eennddss JJuunnee 3300,, 22000088.. Atlanta, Georgia. (I honestly can’t believe it was that long ago!) Since that time, C this text has been revised, tweaked, and enhanced to account for the changes Pro 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 Author of Foundation (WCF), and Windows Workflow Foundation (WF). As you would Pro VB 2008 and the expect, coverage of the “W’s” has been expanded upon a great deal from the # C# 2008 .NET 3.5 Platform previous Special Edition of this text. COM and .NET Unlike .NET 3.0, .NET 3.5 provides dozens of new C# language features and .NET and Interoperability 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 Expert ASP.NET 2.0 Advanced Application in the prior editions. Rest assured, you’ll find detailed coverage of Language the Design Integrated Query (LINQ), the C# 2008 language changes (automatic properties, 2008 extension methods, anonymous types, etc.), and the numerous bells and whistles Developer’s Workshop to COM and ATL 3.0 of Visual Studio 2008. If you’re checking out this book for the first time, understand that it targets .NET 3.5 Platform experienced software professionals and/or students of computer science (so and the 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 P. information presented in these 33 chapters, you’ll be in a perfect position to N apply this knowledge to your specific programming assignments, and you’ll be l well equipped to explore the .NET universe on your own terms. a E Take care and enjoy, Andrew Troelsen t Exploring the .NET universe using curly brackets Microsoft MVP, Visual Developer—Visual C# fT o Free Companion eBook THE APRESS ROADMAP 3 r Pro ASP.NET 3.5 FourTh EdiTioN Beginning C# 2008 in C# 2008 m . Pro LINQ For a limited time only. Pro C# 2008 and the 5 Accelerated C# 2008 See last page for details. .NET 3.5 Platform Pro WPF in C# 2008 Beginning C# 2008 Pro VB 2008 and the Databases .NET 3.5 Platform SOURCE CODE ONLINE FourTh EdiTioN www.apress.com ISBN-13: 978-1-59059-884-9 Andrew Troelsen ISBN-10: 1-59059-884-9 US $59.99 55999 Troelsen Shelve in Programming/ Microsoft/.NET User level: Intermediate–Advanced 9 781590 598849 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) 8849FM.qxd 10/19/07 9:45 AM Page i Pro C# 2008 and the .NET 3.5 Platform Fourth Edition Andrew Troelsen 8849FM.qxd 10/19/07 9:45 AM Page ii 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, JasonGilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, JeffreyPepper, 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 [email protected], or visit http://www.springeronline.com. 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 [email protected], or visit http://www.apress.com. 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 http://www.apress.comin 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 iii 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 iv Contents About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Part 1 n n nIntroducing C# and the .NET Platform nCHAPTER 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 nCHAPTER 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 v nCONTENTS v Part 2 n n nCore C# Programming Constructs nCHAPTER 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 nCHAPTER 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 nCHAPTER 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 nCHAPTER 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 8849FM.qxd 10/19/07 9:45 AM Page vi vi nCONTENTS 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 nCHAPTER 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 nCHAPTER 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 n n nAdvanced C# Programming Constructs nCHAPTER 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 vii nCONTENTS vii Building Comparable Objects (IComparable). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Understanding Callback Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 nCHAPTER 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 nCHAPTER 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 nCHAPTER 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 nCHAPTER 13 C# 2008 Language Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Understanding Implicitly Typed Local Variables. . . . . . . . . . . . . . . . . . . . . . . . . . 415 Understanding Automatic Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 Understanding Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424 8849FM.qxd 10/19/07 9:45 AM Page viii viii nCONTENTS Understanding Partial Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Understanding Object Initializer Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 Understanding Anonymous Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 nCHAPTER 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 n n nProgramming with .NET Assemblies nCHAPTER 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 nCHAPTER 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
Description: