Libro apress - accelerated c sharp 2010

3,090 views
3,012 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,090
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
74
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Libro apress - accelerated c sharp 2010

  1. 1.   CYAN   YELLOW   MAGENTA   BLACK   PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® THE EXPERT’S VOICE ® IN C# Companion eBook Available Accelerated C# 2010 Accelerated C# Dear Reader, In your hands is a guide to creating effective C# 4.0 code. This book focuses precisely on the C# language, showing you how to write programs that are robust, fault-toler- Accelerated ant, and ready to be put into widely available libraries. I won’t burden your time with endless discussions of libraries: Accelerated C# 2010 instead presents you with a well organized, focused and easy to read text aboutTrey Nash, author of C# and all of the tried and true idioms, patterns, and design principles accrued in the C# 2010Accelerated C# 2008 object oriented arena and during the lifetime of the .NET Framework. With many short examples, I will show you how common design patterns are used routinely inAccelerated C# 2005 the .NET Framework and how you should employ them in your own designs.Accelerated VB 2005 I cover with you all the newer features of the C# language in detail - including(with Guy Fouche) the dynamic type, co- and contra-variance, extension methods, lambda expres-Accelerated VB 2008 sions, and Language Integrated Query (LINQ) among others. The new dynamic(with Guy Fouche) type facilitates interoperability possibilities that were previously cumbersome to implement. Extension methods, lambda expressions, and LINQ foster a functional programming model within a traditionally imperative programming language and you’ll find that they expand your horizons and open up new possibilities of solu- tions using functional programming techniques! Another area of particular interest for C# developers is how we can write excep- tion-safe and fault-tolerant code. The .NET Framework includes several facilities, including Critical Execution Regions, to help protect the state of your application 2010 in the event of an asynchronous exception. In this book, I explain how to use these facilities to your advantage. Have fun programming and remember, defining contract before implementa- tion, striving to be exception neutral, applying performance analysis and optimiza- tion, and being frugal with your resource usage are all keys to a successful product. The fast way to the latest and greatest in C# Trey Nash programming and .NET 4.0 Companion eBook THE APRESS ROADMAP Pro C# 2010 and the .NET 4.0 Platform See last page for details on $10 eBook version Accelerated C# 2010 Pro LINQ: Language Integrated Query in C# 2008SOURCE CODE ONLINEwww.apress.com ISBN 978-1-4302-2537-9 Nash Trey Nash 5 39 9 9US $39.99Shelve in:.NET Pro Android GamesUser level: 9 781430 225379Intermediate–Advanced Beginning Android Pro Android this print for content only—size & color not accurate 7.5 x 9.25 spine = 1.21875" 656 page count
  2. 2. Accelerated C# 2010■ ■ ■Trey Nash
  3. 3. Accelerated C# 2010Copyright © 2010 by Trey NashAll 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 retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-4302-2537-9ISBN-13 (electronic): 978-1-4302-2538-6Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names may appear in this book. Rather than use a trademark symbol with everyoccurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit ofthe trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: Damien Foggon Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editors: Katie Stence and Nancy Sixsmith Compositor: Bob Cooper Indexer: Julie Grady Artist: April Milne Cover Designer: Anna IshchenkoDistributed 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 orders-ny@springer-sbm.com, orvisit http://www.springeronline.com.For information on translations, please e-mail info@apress.com, or visit http://www.apress.com.Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.eBook versions and licenses are also available for most titles. For more information, reference ourSpecial Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales.The information in this book is distributed on an “as is” basis, without warranty. Although everyprecaution has been taken in the preparation of this work, neither the author(s) nor Apress shall haveany liability to any person or entity with respect to any loss or damage caused or alleged to be causeddirectly or indirectly by the information contained in this work.The source code for this book is available to readers at http://www.apress.com. You will need to answerquestions pertaining to this book in order to successfully download the code.
  4. 4. Dedicated to the memory of a special friend Thor January 1999 – October 2008 For Maite for the love and support
  5. 5. Contents at a Glance Contents ................................................................................................................... vii About the Author ..................................................................................................... xxi About the Technical Reviewer ................................................................................ xxii Acknowledgments ................................................................................................. xxiii Preface ...................................................................................................................xxiv ■Chapter 1: C# Preview ............................................................................................. 1 ■Chapter 2: C# and the CLR ..................................................................................... 10 ■Chapter 3: C# Syntax Overview.............................................................................. 17 ■Chapter 4: Classes, Structs, and Objects............................................................... 43 ■ Chapter 5: Interfaces and Contracts................................................................... 137 ■Chapter 6: Overloading Operators........................................................................ 165 ■Chapter 7: Exception Handling and Exception Safety.......................................... 181 ■Chapter 8: Working with Strings ......................................................................... 215 ■Chapter 9: Arrays, Collection Types, and Iterators .............................................. 243 ■Chapter 10: Delegates, Anonymous Functions, and Events................................. 279 ■Chapter 11: Generics ........................................................................................... 307 ■Chapter 12: Threading in C# ................................................................................ 361 ■Chapter 13: In Search of C# Canonical Forms ..................................................... 429 ■Chaper 14: Extension Methods ............................................................................ 489 ■Chaper 15: Lambda Expressions ......................................................................... 517 ■Chaper 16: LINQ: Language Integrated Query ...................................................... 543 v
  6. 6. ■Chaper 17: Dynamic Types .................................................................................. 577 Index....................................................................................................................... 609vi
  7. 7. ■ CONTENTSContentsContents at a Glance .................................................................................................. vContents ................................................................................................................... viiAbout the Author ..................................................................................................... xxiAbout the Technical Reviewer ................................................................................ xxiiAcknowledgments ................................................................................................. xxiiiPreface ...................................................................................................................xxiv■Chapter 1: C# Preview ............................................................................................. 1 Differences Between C# and C++ .....................................................................................1 C#..............................................................................................................................................................1 C++...........................................................................................................................................................2 CLR Garbage Collection.............................................................................................................................3 Example of a C# Program...................................................................................................3 Overview of Features Added in C# 2.0 ...............................................................................5 Overview of Features Added in C# 3.0 ...............................................................................6 Overview of New C# 4.0 Features......................................................................................7 Summary............................................................................................................................7■Chapter 2: C# and the CLR ..................................................................................... 10 The JIT Compiler in the CLR.............................................................................................10 Assemblies and the Assembly Loader .............................................................................11 Minimizing the Working Set of the Application .......................................................................................12 Naming Assemblies ................................................................................................................................12 Loading Assemblies ................................................................................................................................13 Metadata ..........................................................................................................................13 vii
  8. 8. ■ CONTENTS Cross-Language Compatibility .........................................................................................15 Summary..........................................................................................................................15 ■Chapter 3: C# Syntax Overview.............................................................................. 17 C# Is a Strongly Typed Language.....................................................................................17 Expressions......................................................................................................................18 Statements and Expressions............................................................................................20 Types and Variables .........................................................................................................21 Value Types.............................................................................................................................................23 Enumerations.....................................................................................................................................24 Flags Enumerations ...........................................................................................................................25 Reference Types .....................................................................................................................................26 Default Variable Initialization ..................................................................................................................27 Implicitly Typed Local Variables..............................................................................................................28 Type Conversion......................................................................................................................................30 Array Covariance................................................................................................................................31 Boxing Conversion .............................................................................................................................31 as and is Operators .................................................................................................................................32 Generics ..................................................................................................................................................34 Namespaces ....................................................................................................................35 Defining Namespaces .............................................................................................................................36 Using Namespaces .................................................................................................................................37 Control Flow .....................................................................................................................39 if-else, while, do-while, and for..............................................................................................................39 switch .....................................................................................................................................................39 foreach....................................................................................................................................................40 break, continue, goto, return, and throw ................................................................................................41 Summary..........................................................................................................................41viii
  9. 9. ■ CONTENTS■Chapter 4: Classes, Structs, and Objects............................................................... 43 Class Definitions ..............................................................................................................45 Fields.......................................................................................................................................................46 Constructors............................................................................................................................................49 Methods ..................................................................................................................................................49 Static Methods...................................................................................................................................50 Instance Methods...............................................................................................................................50 Properties................................................................................................................................................51 Declaring Properties ..........................................................................................................................51 Accessors...........................................................................................................................................53 Read-Only and Write-Only Properties ................................................................................................53 Auto-Implemented Properties............................................................................................................54 Encapsulation..........................................................................................................................................56 Accessibility ............................................................................................................................................59 Interfaces ................................................................................................................................................61 Inheritance ..............................................................................................................................................62 Accessibility of Members...................................................................................................................63 Implicit Conversion and a Taste of Polymorphism.............................................................................63 Member Hiding ..................................................................................................................................65 The base Keyword .............................................................................................................................68 sealed Classes ........................................................................................................................................69 abstract Classes......................................................................................................................................70 Nested Classes........................................................................................................................................71 Indexers ..................................................................................................................................................74 partial Classes.........................................................................................................................................76 partial Methods .......................................................................................................................................77 Static Classes..........................................................................................................................................79 Reserved Member Names.......................................................................................................................81 Reserved Names for Properties .........................................................................................................81 Reserved Names for Indexers............................................................................................................81 Reserved Names for Destructors .......................................................................................................82 ix
  10. 10. ■ CONTENTS Reserved Names for Events...............................................................................................................82 Value Type Definitions......................................................................................................82 Constructors............................................................................................................................................82 The Meaning of this ................................................................................................................................85 Finalizers.................................................................................................................................................87 Interfaces ................................................................................................................................................87 Anonymous Types ............................................................................................................88 Object Initializers .............................................................................................................91 Boxing and Unboxing .......................................................................................................94 When Boxing Occurs ...............................................................................................................................98 Efficiency and Confusion.......................................................................................................................100 System.Object ................................................................................................................101 Equality and What It Means ..................................................................................................................103 The IComparable Interface....................................................................................................................103 Creating Objects.............................................................................................................103 The new Keyword .................................................................................................................................103 Using new with Value Types ............................................................................................................103 Using new with Class Types ............................................................................................................103 Field Initialization ..................................................................................................................................104 Static (Class) Constructors....................................................................................................................106 Instance Constructor and Creation Ordering.........................................................................................109 Destroying Objects.........................................................................................................113 Finalizers...............................................................................................................................................113 Deterministic Destruction .....................................................................................................................115 Exception Handling ...............................................................................................................................115 Disposable Objects.........................................................................................................116 The IDisposable Interface......................................................................................................................116 The using Keyword ...............................................................................................................................118 Method Parameter Types...............................................................................................119 Value Arguments...................................................................................................................................120x
  11. 11. ■ CONTENTS ref Arguments .......................................................................................................................................120 out Parameters .....................................................................................................................................122 param Arrays ........................................................................................................................................123 Method Overloading..............................................................................................................................123 Optional Arguments ..............................................................................................................................124 Named Arguments ................................................................................................................................125 Inheritance and Virtual Methods ....................................................................................128 Virtual and Abstract Methods................................................................................................................129 override and new Methods ...................................................................................................................129 sealed Methods.....................................................................................................................................131 A Final Few Words on C# Virtual Methods ............................................................................................132 Inheritance, Containment, and Delegation.....................................................................132 Choosing Between Interface and Class Inheritance..............................................................................132 Delegation and Composition vs. Inheritance.........................................................................................134 Summary........................................................................................................................136■Chapter 5: Interfaces and Contracts.................................................................... 137 Interfaces Define Types .................................................................................................138 Defining Interfaces.........................................................................................................139 What Can Be in an Interface?................................................................................................................139 Interface Inheritance and Member Hiding ............................................................................................140 Implementing Interfaces ................................................................................................143 Implicit Interface Implementation .........................................................................................................143 Explicit Interface Implementation .........................................................................................................143 Overriding Interface Implementations in Derived Classes ....................................................................145 Beware of Side Effects of Value Types Implementing Interfaces .........................................................150 Interface Member Matching Rules.................................................................................150 Explicit Interface Implementation with Value Types ......................................................154 Versioning Considerations .............................................................................................156 Contracts........................................................................................................................157 xi
  12. 12. ■ CONTENTS Contracts Implemented with Classes....................................................................................................157 Interface Contracts................................................................................................................................159 Choosing Between Interfaces and Classes ....................................................................160 Summary........................................................................................................................164 ■Chapter 6: Overloading Operators........................................................................ 165 Just Because You Can Doesn’t Mean You Should..........................................................165 Types and Formats of Overloaded Operators.................................................................165 Operators Shouldn’t Mutate Their Operands..................................................................167 Does Parameter Order Matter? ......................................................................................167 Overloading the Addition Operator.................................................................................168 Operators That Can Be Overloaded ................................................................................169 Comparison Operators ..........................................................................................................................170 Conversion Operators............................................................................................................................173 Boolean Operators ................................................................................................................................176 Summary........................................................................................................................179 ■Chapter 7: Exception Handling and Exception Safety.......................................... 181 How the CLR Treats Exceptions .....................................................................................181 Mechanics of Handling Exceptions in C# .......................................................................182 Throwing Exceptions.............................................................................................................................182 Changes with Unhandled Exceptions Starting with .NET 2.0 ................................................................182 Syntax Overview of the try, catch, and finally Statements ...................................................................183 Rethrowing Exceptions and Translating Exceptions .............................................................................186 Exceptions Thrown in finally Blocks .....................................................................................................189 Exceptions Thrown in Finalizers ...........................................................................................................189 Exceptions Thrown in Static Constructors ............................................................................................191 Who Should Handle Exceptions?....................................................................................192 Avoid Using Exceptions to Control Flow.........................................................................193 Achieving Exception Neutrality ......................................................................................193 Basic Structure of Exception-Neutral Code...........................................................................................194xii
  13. 13. ■ CONTENTS Constrained Execution Regions ............................................................................................................199 Critical Finalizers and SafeHandle ........................................................................................................201 Creating Custom Exception Classes...............................................................................206 Working with Allocated Resources and Exceptions .......................................................207 Providing Rollback Behavior ..........................................................................................211 Summary........................................................................................................................214■Chapter 8: Working with Strings ......................................................................... 215 String Overview..............................................................................................................215 String Literals.................................................................................................................216 Format Specifiers and Globalization ..............................................................................217 Object.ToString, IFormattable, and CultureInfo.....................................................................................218 Creating and Registering Custom CultureInfo Types ............................................................................219 Format Strings ......................................................................................................................................221 Console.WriteLine and String.Format ...................................................................................................222 Examples of String Formatting in Custom Types ..................................................................................223 ICustomFormatter .................................................................................................................................224 Comparing Strings ................................................................................................................................227 Working with Strings from Outside Sources ..................................................................228 StringBuilder ..................................................................................................................230 Searching Strings with Regular Expressions .................................................................232 Searching with Regular Expressions ....................................................................................................232 Searching and Grouping........................................................................................................................234 Replacing Text with Regex....................................................................................................................238 Regex Creation Options.........................................................................................................................240 Summary........................................................................................................................242■Chapter 9: Arrays, Collection Types, and Iterators .............................................. 243 Introduction to Arrays ....................................................................................................243 Implicitly Typed Arrays..........................................................................................................................244 Type Convertibility and Covariance.......................................................................................................247 xiii
  14. 14. ■ CONTENTS Sortability and Searchability .................................................................................................................248 Synchronization ....................................................................................................................................249 Vectors vs. Arrays .................................................................................................................................249 Multidimensional Rectangular Arrays ............................................................................251 Multidimensional Jagged Arrays....................................................................................253 Collection Types.............................................................................................................255 Comparing ICollection<T> with ICollection ..........................................................................................255 Collection Synchronization....................................................................................................................257 Lists.......................................................................................................................................................258 Dictionaries ...........................................................................................................................................259 Sets .......................................................................................................................................................259 System.Collections.ObjectModel...........................................................................................................260 Efficiency ..............................................................................................................................................262 IEnumerable<T>, IEnumerator<T>, IEnumerable, and IEnumerator .............................264 Types That Produce Collections ............................................................................................................267 Iterators..........................................................................................................................268 Forward, Reverse, and Bidirectional Iterators ......................................................................................273 Collection Initializers......................................................................................................277 Summary........................................................................................................................278 ■Chapter 10: Delegates, Anonymous Functions, and Events................................. 279 Overview of Delegates ...................................................................................................279 Delegate Creation and Use.............................................................................................280 Single Delegate .....................................................................................................................................281 Delegate Chaining .................................................................................................................................282 Iterating Through Delegate Chains........................................................................................................284 Unbound (Open Instance) Delegates .....................................................................................................285 Events ............................................................................................................................288 Anonymous Methods......................................................................................................292 Captured Variables and Closures ..........................................................................................................295xiv
  15. 15. ■ CONTENTS Beware the Captured Variable Surprise................................................................................................297 Anonymous Methods as Delegate Parameter Binders..........................................................................300 The Strategy Pattern ......................................................................................................304 Summary........................................................................................................................305■Chapter 11: Generics ........................................................................................... 307 Difference Between Generics and C++ Templates........................................................308 Efficiency and Type Safety of Generics..........................................................................309 Generic Type Definitions and Constructed Types ..........................................................311 Generic Classes and Structs .................................................................................................................311 Generic Interfaces.................................................................................................................................314 Generic Methods ...................................................................................................................................315 Generic Delegates.................................................................................................................................317 Generic Type Conversion ......................................................................................................................320 Default Value Expression ......................................................................................................................321 Nullable Types.......................................................................................................................................323 Constructed Types Control Accessibility...............................................................................................325 Generics and Inheritance ......................................................................................................................325 Constraints.....................................................................................................................327 Constraints on Nonclass Types .............................................................................................................332 Co- and Contravariance .................................................................................................332 Covariance ............................................................................................................................................334 Contravariance......................................................................................................................................337 Invariance .............................................................................................................................................339 Variance and Delegates ........................................................................................................................340 Generic System Collections ...........................................................................................344 Generic System Interfaces .............................................................................................345 Select Problems and Solutions ......................................................................................347 Conversion and Operators within Generic Types ..................................................................................347 Creating Constructed Types Dynamically .............................................................................................357 xv
  16. 16. ■ CONTENTS Summary........................................................................................................................358 ■Chapter 12: Threading in C# ................................................................................ 361 Threading in C# and .NET...............................................................................................361 Starting Threads ...................................................................................................................................362 Passing Data to New Threads ..........................................................................................................363 Using ParameterizedThreadStart.....................................................................................................365 The IOU Pattern and Asynchronous Method Calls.................................................................................366 States of a Thread.................................................................................................................................366 Terminating Threads.............................................................................................................................369 Halting Threads and Waking Sleeping Threads ....................................................................................371 Waiting for a Thread to Exit...................................................................................................................372 Foreground and Background Threads...................................................................................................372 Thread-Local Storage............................................................................................................................373 How Unmanaged Threads and COM Apartments Fit In .........................................................................377 Synchronizing Work Between Threads ..........................................................................378 Lightweight Synchronization with the Interlocked Class ......................................................................379 SpinLock Class......................................................................................................................................385 Monitor Class ........................................................................................................................................387 Beware of Boxing.............................................................................................................................391 Pulse and Wait .................................................................................................................................392 Locking Objects.....................................................................................................................................396 ReaderWriterLock ............................................................................................................................397 ReaderWriterLockSlim .....................................................................................................................400 Mutex ...............................................................................................................................................401 Semaphore............................................................................................................................................402 Events ...................................................................................................................................................404 Win32 Synchronization Objects and WaitHandle ..................................................................................405 Using ThreadPool ...........................................................................................................407 Asynchronous Method Calls..................................................................................................................408 Timers ...................................................................................................................................................416xvi
  17. 17. ■ CONTENTS Concurrent Programming...............................................................................................417 Task Class.............................................................................................................................................418 Parallel Class ........................................................................................................................................420 Easy Entry to the Thread Pool ...............................................................................................................425 Thread-Safe Collection Classes .....................................................................................426 Summary........................................................................................................................426■Chapter 13: In Search of C# Canonical Forms ..................................................... 429 Reference Type Canonical Forms ..................................................................................429 Default to sealed Classes......................................................................................................................430 Use the Non-Virtual Interface (NVI) Pattern...........................................................................................431 Is the Object Cloneable? .......................................................................................................................434 Is the Object Disposable?......................................................................................................................440 Does the Object Need a Finalizer? ........................................................................................................443 What Does Equality Mean for This Object? ...........................................................................................450 Reference Types and Identity Equality.............................................................................................451 Value Equality ..................................................................................................................................454 Overriding Object.Equals for Reference Types ................................................................................454 If You Override Equals, Override GetHashCode Too ..............................................................................457 Does the Object Support Ordering? ......................................................................................................461 Is the Object Formattable?....................................................................................................................463 Is the Object Convertible? .....................................................................................................................467 Prefer Type Safety at All Times.............................................................................................................469 Using Immutable Reference Types .......................................................................................................473 Value Type Canonical Forms..........................................................................................476 Override Equals for Better Performance ...............................................................................................477 Do Values of This Type Support Any Interfaces? ..................................................................................481 Implement Type-Safe Forms of Interface Members and Derived Methods ..........................................482 Summary........................................................................................................................484 Checklist for Reference Types ..............................................................................................................485 Checklist for Value Types......................................................................................................................486 xvii
  18. 18. ■ CONTENTS ■Chaper 14: Extension Methods ............................................................................ 489 Introduction to Extension Methods ................................................................................489 How Does the Compiler Find Extension Methods? ...............................................................................490 Under the Covers...................................................................................................................................493 Code Readability versus Code Understandability..................................................................................494 Recommendations for Use.............................................................................................495 Consider Extension Methods Over Inheritance .....................................................................................495 Isolate Extension Methods in Separate Namespace .............................................................................496 Changing a Type’s Contract Can Break Extension Methods .................................................................497 Transforms.....................................................................................................................497 Operation Chaining.........................................................................................................502 Custom Iterators ............................................................................................................503 Borrowing from Functional Programming.............................................................................................505 The Visitor Pattern..........................................................................................................511 Summary........................................................................................................................515 ■Chaper 15: Lambda Expressions ......................................................................... 517 Introduction to Lambda Expressions..............................................................................517 Lambda Expressions and Closures .......................................................................................................518 Closures in C# 1.0 ............................................................................................................................521 Closures in C# 2.0 ............................................................................................................................523 Lambda Statements..............................................................................................................................524 Expression Trees............................................................................................................524 Operating on Expressions .....................................................................................................................527 Functions as Data .................................................................................................................................528 Useful Applications of Lambda Expressions ..................................................................529 Iterators and Generators Revisited .......................................................................................................529 More on Closures (Variable Capture) and Memoization ........................................................................533 Currying ................................................................................................................................................538 Anonymous Recursion ..........................................................................................................................540xviii
  19. 19. ■ CONTENTS Summary........................................................................................................................541■Chaper 16: LINQ: Language Integrated Query ...................................................... 543 A Bridge to Data .............................................................................................................544 Query Expressions ................................................................................................................................544 Extension Methods and Lambda Expressions Revisited .......................................................................546 Standard Query Operators .............................................................................................547 C# Query Keywords........................................................................................................549 The from Clause and Range Variables ..................................................................................................549 The join Clause......................................................................................................................................550 The where Clause and Filters ...............................................................................................................552 The orderby Clause ...............................................................................................................................553 The select Clause and Projection..........................................................................................................554 The let Clause .......................................................................................................................................556 The group Clause ..................................................................................................................................557 The into Clause and Continuations........................................................................................................560 The Virtues of Being Lazy...............................................................................................562 C# Iterators Foster Laziness .................................................................................................................562 Subverting Laziness..............................................................................................................................563 Executing Queries Immediately ............................................................................................................565 Expression Trees Revisited ...................................................................................................................566 Techniques from Functional Programming....................................................................566 Custom Standard Query Operators and Lazy Evaluation.......................................................................566 Replacing foreach Statements..............................................................................................................575 Summary........................................................................................................................576■Chaper 17: Dynamic Types .................................................................................. 577 What does dynamic Mean?............................................................................................577 How Does dynamic Work? .............................................................................................580 The Great Unification.............................................................................................................................582 Call Sites ...............................................................................................................................................582 xix
  20. 20. ■ CONTENTS Objects with Custom Dynamic Behavior ...............................................................................................585 Efficiency ..............................................................................................................................................587 Boxing with Dynamic ............................................................................................................................589 Dynamic Conversions.....................................................................................................590 Implicit Dynamic Expressions Conversion ............................................................................................591 Dynamic Overload Resolution ........................................................................................592 Dynamic Inheritance ......................................................................................................594 You Cannot Derive from dynamic..........................................................................................................595 You Cannot Implement dynamic Interfaces ..........................................................................................595 You Can Derive From Dynamic Base Types ..........................................................................................597 Duck Typing in C# ..........................................................................................................599 Limitations of dynamic Types ........................................................................................602 ExpandoObject: Creating Objects Dynamically ..............................................................602 Summary........................................................................................................................607 Index....................................................................................................................... 609xx
  21. 21. ■ CONTENTSAbout the Author ■ Trey Nash is an Escalation Engineer at Microsoft on the Platforms Global Escalation Services team working on the Windows operating systems as well as various other products. When he is not working feverishly within the bowels of the operating system, he is delivering training on .NET Platform debugging as well as user mode and kernel mode debugging on the Windows platform. Prior to working at Microsoft, he was a Principal Software Engineer working on security solutions at Credant Technologies, a market-leading security software company. He also enjoyed a stint at a large Bluetooth company developing Bluetooth solutions for the release of Microsoft Vista. And before that he called Macromedia Inc. home for five years. At Macromedia, he worked on a cross-product engineering team for several years, designing solutions for a wide range of products throughout the company, including Flash, Fireworks, and Dreamweaver. He specialized inCOM/DCOM using C/C++/ATL until the .NET revolution. He’s been glued to computers ever since hescored his first, a TI-99/4A, when he was a mere 13 years old. He astounded his parents by turning achildhood obsession into a decent paying career, much to their dismay. Trey received his bachelor ofscience and his master of engineering degrees in electrical engineering from Texas A&M University.When he’s not sitting in front of a computer, you can find him working in his garage, playing his piano,brushing up on a foreign language (Russian and Icelandic are the current favorites), or playing icehockey. xxi
  22. 22. ■ CONTENTSAbout the Technical Reviewer■ Damien Foggon is a developer, writer, and technical reviewer in cutting-edge technologies and hascontributed to more than 50 books on .NET, C#, Visual Basic and ASP.NET. He is a multiple MCPD in.NET 2.0 and .NET 3.5 and can be found online at http://blog.littlepond.co.uk.xxii
  23. 23. ■ CONTENTSAcknowledgmentsWriting a book is a long and arduous process, during which I have received tons of great support, which Igreatly appreciate, from friends and family. The process would have been much more difficult, andarguably much less fruitful, without their support. I would like to specifically call out the following individuals for their contribution to the first twoeditions of this work. I would like to thank (in no particular order) David Weller, Stephen Toub, RexJaeschke, Vladimir Levin, Jerry Maresca, Chris Pels, Christopher T. McNabb, Brad Wilson, Peter Partch,Paul Stubbs, Rufus Littlefield, Tomas Restrepo, John Lambert, Joan Murray, Sheri Cain, Jessica D’Amico,Karen Gettman, Jim Huddleston, Richard Dal Porto, Gary Cornell, Brad Abrams, Ellie Fountain, NicoleAbramowitz and the entire Apress crew, and finally, Shelley Nash, Michael Pulk, Shawn Wildermuth,Sofia Marchant, Jim Compton, Dominic Shakeshaft, Wes Dyer, Kelly Winquist, and Laura Cheu. During the development of the third edition, I would like to call out the following individuals fortheir help and support (again in no particular order): Jonathan Hassell, Mary Tobin, Damien Foggon,Maite Cervera. If I have left anyone out, it is purely my mistake and not one I intended. I could not have done itwithout all of your support. Thank you all! xxiii
  24. 24. PrefaceVisual C# .NET (C#) is relatively easy to learn for anyone familiar with another object-oriented language.Even someone familiar with Visual Basic 6.0, who is looking for an object-oriented language, will find C#easy to pick up. However, though C#, coupled with the .NET Framework, provides a quick path forcreating simple applications, you still must know a wealth of information and understand how to use itcorrectly in order to produce sophisticated, robust, fault-tolerant C# applications. I teach you what youneed to know and explain how best to use your knowledge so that you can quickly develop true C#expertise. Idioms and design patterns are invaluable for developing and applying expertise, and I show youhow to use many of them to create applications that are efficient, robust, fault-tolerant, and exception-safe. Although many are familiar to C++ and Java programmers, some are unique to .NET and itsCommon Language Runtime (CLR). I show you how to apply these indispensable idioms and designtechniques to seamlessly integrate your C# applications with the .NET runtime, focusing on the newcapabilities of C# 3.0 Design patterns document best practices in application design that many different programmershave discovered and rediscovered over time. In fact, the .NET Framework itself implements many well-known design patterns. Similarly, over the past three versions of the .NET Framework and the past twoversions of C#, many new idioms and best practices have come to light. You will see these practicesdetailed throughout this book. Also, it is important to note that the invaluable tool chest of techniques isevolving constantly. With the arrival of C# 3.0, you can easily incorporate functional programming techniques usinglambda expressions, extension methods, and Language Integrated Query (LINQ). Lambda expressionsmake is easy to declare and instantiate function delegates at one point. Additionally, with lambdaexpressions, it’s trivial to create functionals, which are functions that accept functions as arguments andtypically return another function. Even though you could implement functional programmingtechniques in C# (albeit with some difficulty), the new language features in C# 3.0 foster an environmentwhere functional programming can flourish interweaved with the typical imperative programming styleof C#. LINQ allows you to express data query operations (which are typically functional in nature) usinga syntax that is native to the language. Once you see how LINQ works, you realize you can do much morethan simple data query and use it to implement complex functional programs. .NET and the CLR provide a unique and stable cross-platform execution environment. C# is onlyone of the languages that target this powerful runtime. You will find that many of the techniquesexplored in this book are also applicable to any language that targets the .NET runtime. For those of you who have significant C++ experience and are familiar with such concepts as C++canonical forms, exception safety, Resource Acquisition Is Initialization (RAII), and const correctness,this book explains how to apply these concepts in C#. If you’re a Java or Visual Basic programmer whohas spent years developing your toolbox of techniques and you want to know how to apply themeffectively in C#, you’ll find out how to here. As you’ll see, it doesn’t take years of trial-and-error experience to become a C# expert. You simplyneed to learn the right things and the right ways to use them. That’s why I wrote this book for you.xxiv
  25. 25. ■ PREFACEAbout This BookI assume that you already have a working knowledge of some object-oriented programming language,such as C++, Java, or Visual Basic .NET. Since C# derives its syntax from both C++ and Java, I don’t spendmuch time covering C# syntax, except where it differs starkly from C++ or Java. If you already know someC#, you may find yourself skimming or even skipping Chapters 1 through 3. Chapter 1, “C# Preview,” gives a quick glimpse of what a simple C# application looks like, and itdescribes some basic differences between the C# programming environment and the native C++environment. Chapter 2, “C# and the CLR,” expands on Chapter 1 and quickly explores the managed environmentwithin which C# applications run. I introduce you to assemblies, the basic building blocks ofapplications, into which C# code files are compiled. Additionally, you’ll see how metadata makesassemblies self-describing. Chapter 3, “C# Syntax Overview,” surveys the C# language syntax. I introduce you to the twofundamental kinds of types within the CLR: value types and reference types. I also describe namespacesand how you can use them to logically partition types and functionality within your applications. Chapters 4 through 13 provide in-depth descriptions on how to employ useful idioms, designpatterns, and best practices in your C# programs and designs. I’ve tried hard to put these chapters inlogical order, but occasionally one chapter may reference a technique or topic covered in a later chapter.It is nearly impossible to avoid this situation, but I tried to minimize it as much as possible. Chapter 4, “Classes, Structs, and Objects,” provides details about defining types in C#. You’ll learnmore about value types and reference types in the CLR. I also touch upon the native support forinterfaces within the CLR and C#. You’ll see how type inheritance works in C#, as well as how everyobject derives from the System.Object type. This chapter also contains a wealth of information regardingthe managed environment and what you must know in order to define types that are useful in it. Iintroduce many of these topics in this chapter and discuss them in much more detail in later chapters. Chapter 5, “Interfaces and Contracts,” details interfaces and the role they play in the C# language.Interfaces provide a functionality contract that types may choose to implement. You’ll learn the variousways that a type may implement an interface, as well as how the runtime chooses which methods to callwhen an interface method is called. Chapter 6, “Overloading Operators,” details how you may provide custom functionality for thebuilt-in operators of the C# language when applied to your own defined types. You’ll see how tooverload operators responsibly, because not all managed languages that compile code for the CLR areable to use overloaded operators. Chapter 7, “Exception Handling and Exception Safety,” shows you the exception-handlingcapabilities of the C# language and the CLR. Although the syntax is similar to that of C++, creatingexception-safe and exception-neutral code is tricky—even more tricky than creating exception-safe codein native C++. You’ll see that creating fault-tolerant, exception-safe code doesn’t require the use of try,catch, or finally constructs at all. I also describe some of the new capabilities added with the .NET 2.0runtime that allow you to create more fault-tolerant code. Chapter 8, “Working with Strings,” describes how strings are a first-class type in the CLR and how touse them effectively in C#. A large portion of the chapter covers the string-formatting capabilities ofvarious types in the .NET Framework and how to make your defined types behave similarly byimplementing IFormattable. Additionally, I introduce you to the globalization capabilities of theframework and how to create custom CultureInfo for cultures and regions that the .NET Frameworkdoesn’t already know about. Chapter 9, “Arrays, Collection Types, and Iterators,” covers the various array and collection typesavailable in C#. You can create two types of multidimensional arrays, as well as your own collectiontypes while utilizing collection-utility classes. You’ll see how to define forward, reverse, and bidirectionaliterators using the new iterator syntax introduced in C# 2.0, so that your collection types will work wellwith foreach statements. xxv
  26. 26. ■ PREFACE Chapter 10, “Delegates, Anonymous Functions, and Events,” shows you the mechanisms usedwithin C# to provide callbacks. Historically, all viable frameworks have always provided a mechanism toimplement callbacks. C# goes one step further and encapsulates callbacks into callable objects calleddelegates. Additionally, C# 2.0 allows you to create delegates with an abbreviated syntax calledanonymous functions. Anonymous functions are similar to lambda functions in functionalprogramming. Also, you’ll see how the framework builds upon delegates to provide a publish/subscribeevent notification mechanism, allowing your design to decouple the source of the event from theconsumer of the event. Chapter 11, “Generics,” introduces you to probably the most exciting feature added to C# 2.0 andthe CLR. Those familiar with C++ templates will find generics somewhat familiar, though manyfundamental differences exist. Using generics, you can provide a shell of functionality within which todefine more specific types at run time. Generics are most useful with collection types and provide greatefficiency compared to the collections of previous .NET versions. Starting with C# 4.0, generic type usagebecame even more intuitive with the support of co- and contravariance. Assigning from one generic typeto another when it makes intuitive type-sense is now possible, thus reducing the clutter of conversionmethods needed prior to that. Chapter 12, “Threading in C#,” covers the tasks required in creating multithreaded applications inthe C# managed virtual execution environment. If you’re familiar with threading in the native Win32environment, you’ll notice the significant differences. Moreover, the managed environment providesmuch more infrastructure for making the job easier. You’ll see how delegates, through use of the I OweYou (IOU) pattern, provide an excellent gateway into the process thread pool. Arguably, synchronizationis the most important concept when getting multiple threads to run concurrently. This chapter coversthe various synchronization facilities available to your applications. In today’s world, concurrency is atthe forefront because, rather than spending exorbitant amount of time and money to create fasterprocessors, the industry has gravitated to creating processors with multiple cores. Therefore, I introducethe new Parallel Extensions and the Task Parallel Library (TPL) added to .NET 4.0. Chapter 13, “In Search of C# Canonical Forms,” is a dissertation on the best design practices fordefining new types and how to make them so you can use them naturally and so consumers won’t abusethem inadvertently. I touch upon some of these topics in other chapters, but I discuss them in detail inthis chapter. This chapter concludes with a checklist of items to consider when defining new types usingC#. Chapter 14, “Extension Methods,” are new since C# 3.0. Because you can invoke them like instancemethods on a type they extend, they can appear to augment the contract of types. But they are muchmore than that. In this chapter, I show you how extension methods can begin to open up the world offunctional programming in C#. Chapter 15, “Lambda Expressions,” are another feature added to C# 3.0. You can declare andinstantiate delegates using lambda expressions using a syntax that is brief and visually descriptive.Although anonymous functions can serve the same purpose just mentioned, they are much moreverbose and less syntactically elegant. However, in C# 3.0 and beyond, you can convert lambdaexpressions into expression trees. That is, the language has a built-in capability to convert code into datastructures. By itself, this capability is useful, but not nearly as useful as when coupled with LanguageIntegrated Query (LINQ). Lambda expressions, coupled with extension methods, really bring functionalprogramming full circle in C#. Chapter 16, “LINQ: Language Integrated Query,” is the culmination of all of the new features addedto C# 3.0. Using LINQ expressions via the LINQ-oriented keywords, you can seamlessly integrate dataqueries into your code. LINQ forms a bridge between the typically imperative programming world of C#programming and the functional programming world of data query. LINQ expressions can be used tomanipulate normal objects as well as data originating from SQL databases, Datasets, and XML just toname a few. Chapter 17, “Dynamic Types,” covers the new dynamic type added in C# 4.0. Along with thedynamic type comes easier integration with dynamic .NET languages, including COM Automationobjects. Gone are the days of coding unnatural-looking and hard-to-read code purely in efforts tointegrate with these components because the dynamic type implementation handles all of that rotexxvi
  27. 27. ■ PREFACEwork for you. The implementation of the dynamic type utilizes the Dynamic Language Runtime (DLR)which is the same foundation for dynamic languages such as IronRuby and IronPython, among others.And while using the dynamic type in conjunction with DLR types such as ExpandoObject, you can createand implement truly dynamic types in C#. xxvii
  28. 28. CHAPTER 1■■■C# PreviewThis is a book for experienced object-oriented developers; therefore, I assume that you already havesome familiarity with the .NET runtime. Essential .NET Volume 1: The Common Language Runtime byDon Box (Boston, MA: Addison-Wesley, 2002) is an excellent book specifically covering the .NETruntime. Additionally, it’s important to look at some of the similarities and differences between C# andC++, and then go through an elementary “Hello World!” example for good measure. If you already haveexperience building .NET applications, you may want to skip this chapter. However, you may want toread the section “Overview of New C# 4.0 Features.”Differences Between C# and C++C# is a strongly typed object-oriented language whose code visually resembles C++ (and Java). Thisdecision by the C# language designers allows C++ developers to easily leverage their knowledge toquickly become productive in C#. C# syntax differs from C++ in some ways, but most of the differencesbetween these languages are semantic and behavioral, stemming from differences in the runtimeenvironments in which they execute.C#C# source code compiles into managed code. Managed code, as you may already know, is anintermediate language (IL) because it is halfway between the high-level language (C#) and the lowest-level language (assembly/machine code). At runtime, the Common Language Runtime (CLR) compilesthe code on the fly by using Just In Time (JIT) compiling. As with just about anything in engineering, thistechnique comes with its pros and cons. It may seem that an obvious con is the inefficiency of compilingthe code at runtime. This process is different from interpreting, which is typically used by scriptinglanguages such as Perl and JScript. The JIT compiler doesn’t compile a function or method each andevery time it’s called; it does so only the first time, and when it does, it produces machine code native tothe platform on which it’s running. An obvious pro of JIT compiling is that the working set of theapplication is reduced, because the memory footprint of intermediate code is smaller. During theexecution of the application, only the needed code is JIT-compiled. If your application contains printingcode, for example, that code is not needed if the user never prints a document, and therefore the JITcompiler never compiles it. Moreover, the CLR can optimize the program’s execution on the fly atruntime. For example, the CLR may determine a way to reduce page faults in the memory manager byrearranging compiled code in memory, and it could do all this at runtime. Once you weigh all the prostogether, you find that they outweigh the cons for most applications. 1
  29. 29. CHAPTER 1 ■ C# PREVIEW ■ Note Actually, you can choose to code your programs in raw IL while building them with the IL Assembler (ILASM). However, it will likely be an inefficient use of your time. High-level languages can nearly always provide any capability that you can achieve with raw IL code. C++ Unlike C#, C++ code traditionally compiles into native code. Native code is the machine code that’s native to the processor for which the program was compiled. For the sake of discussion, assume that we’re talking about natively compiled C++ code rather than managed C++ which can be achieved by using C++/CLI. If you want your native C++ application to run on different platforms, such as on both a 32-bit platform and a 64-bit platform, you must compile it separately for each. The native binary output is generally not compatible across platforms. IL, on the other hand, is compatible across platforms, because it, along with the Common Language Infrastructure (CLI) upon which the CLR is built, is a defined international standard.1 This standard is rapidly gaining traction and being implemented beyond the Microsoft Windows platform. ■ Note I recommend you check out the work the Mono team has accomplished toward creating alternate, open source Virtual Execution Systems (VESs) on other platforms.2 Included in the CLI standard is the Portable Executable (PE) file format for managed modules. Therefore, you can actually compile a C# program on a Windows platform and execute the output on both Windows and Linux without having to recompile, because even the file format is standardized.3 This degree of portability is extremely convenient and was in the hearts and minds of the COM/DCOM designers back in the day, but for various reasons, it failed to succeed across disparate platforms at this level.4 One of the major reasons for that failure is that COM lacked a sufficiently expressive and extensible mechanism for describing types and their dependencies. The CLI specification solves this nicely by introducing metadata, which I’ll describe in Chapter 2. 1 You can find the CLI standard document Ecma-335 at http://www.ecma-international.org. Additionally, Ecma- 334 is the standard document for the C# language. 2 You can find the Mono project on the Internet at http://www.mono-project.com. 3 Of course, the target platform must also have all of the dependent libraries installed. This is quickly becoming a reality, considering the breadth of the .NET Standard Library. For example, check out http://www.go- mono.com/docs/ to see how much coverage the Mono project libraries have. 4 For all the gory details, I recommend reading Essential .NET, Volume I: The Common Language Runtime by Don Box and Chris Sells (Boston, MA: Addison-Wesley Professional, 2002). (The title leads one to believe that Volume II is due out any time now, so let’s hope it’s not titled in the same vein as Mel Brooks’ History of the World: Part I.)2
  30. 30. CHAPTER 1 ■ C# PREVIEWCLR Garbage CollectionOne of the key facilities in the CLR is the garbage collector (GC). The GC frees you from the burden ofhandling memory allocation and deallocation, which is where many software errors can occur. However,the GC doesn’t remove all resource-handling burdens from your plate, as you’ll see in Chapter 4. Forexample, a file handle is a resource that must be freed when the consumer is finished with it, just asmemory must be freed in the same way. The GC handles only memory resources directly. To handleresources other than memory, such as database connections and file handles, you can use a finalizer (asI’ll show you in Chapter 13) to free your resources when the GC notifies you that your object is beingdestroyed. However, an even better way is to use the Disposable pattern for this task, which I’lldemonstrate in Chapters 4 and 13.■ Note The CLR references all objects of reference type indirectly, similar to the way you use pointers and referencesin C++, except without the pointer syntax. When you declare a variable of a reference type in C#, you actually reservea storage location that has a type associated with it, either on the heap or on the stack, which stores the reference tothe object. So when you copy an object reference in one variable into another variable, you end up with two variablesreferencing the same object. All reference type instances live on the managed heap. The CLR manages the location ofthese objects, and if it needs to move them around, it updates all the outstanding references to the moved objects topoint to the new location. Also, value types exist in the CLR, and instances of them live on the stack or as a field of anobject on the managed heap. Their usage comes with many restrictions and nuances. You normally use them whenyou need a lightweight structure to manage some related data. Value types are also useful when modeling animmutable chunk of data. I cover this topic in much more detail in Chapter 4. C# allows you to develop applications rapidly while dealing with fewer mundane details than in aC++ environment. At the same time, C# provides a language that feels familiar to either C++ or Javadevelopers.Example of a C# ProgramLet’s take a close look at a very simple C# program. Consider the venerable “Hello World!” program thateveryone knows and loves. A console version of it looks like this in C#:class EntryPoint { static void Main() { System.Console.WriteLine( "Hello World!" ); }} Note the structure of this C# program. It declares a type (a class named EntryPoint) and a memberof that type (a method named Main). This differs from C++, where you declare a type in a header anddefine it in a separate compilation unit, usually a .cpp file. Also, metadata (which describes all of thetypes in a module and is generated transparently by the C# compiler) removes the need for the forwarddeclarations and inclusions as required in C++. In fact, forward declarations don’t even exist in C#. 3
  31. 31. CHAPTER 1 ■ C# PREVIEW C++ programmers will find the static Main method familiar, except for the fact that its name begins with a capital letter. Every program requires an entry point, and in the case of C#, it is the static Main method. There are some further differences. For example, the Main method is declared within a class (in this case, named EntryPoint). In C#, you must declare all methods within a type definition. There is no such thing as a static, free function as there is in C++. The return type for the Main method may be either of type int or void, depending on your needs. In my example, Main has no parameters, but if you need access to the command-line parameters, your Main method can declare a parameter (an array of strings) to access them. ■ Note If your application contains multiple types with a static Main method, you can select which one to use via the /main compiler switch. You may notice that the call to WriteLine seems verbose. I had to qualify the method name with the class name Console, and I also had to specify the namespace that the Console class lives in (in this case, System). .NET (and therefore C#) supports namespaces to avoid name collisions in the vast global namespace. However, instead of having to type the fully qualified name, including the namespace, every time, C# provides the using directive, which is analogous to Java’s import and C++’s using namespace. So you could rewrite the previous program slightly, as Listing 1-1 shows. Listing 1-1. hello_world.cs using System; class EntryPoint { static void Main() { Console.WriteLine( "Hello World!" ); } } With the using System; directive, you can omit the System namespace when calling Console.WriteLine. To compile this example, execute the following command from a Windows command prompt: csc.exe /r:mscorlib.dll /target:exe hello_world.cs Let’s take a look at exactly what this command line does: • csc.exe is the Microsoft C# compiler. • The /r option specifies the assembly dependencies this program has. Assemblies are similar in concept to DLLs in the native world. mscorlib.dll is where the System.Console object is defined. In reality, you don’t need to reference the mscorlib assembly because the compiler will reference it automatically, unless you use the /nostdlib option.4

×