pro c# 5.0 and the .net 4.5 framework


this print for content only—size & color not accurate 7.5 x 9.25 spine = 2.08“ 1560 pages 750PPI CYAN MAGENTA YELLOW BLACK PANTONE 123 C Fourteen leading lights in Oracle Application Express share their favorite insights from the field BOOKS FOR PROFESSIONALS BY PROFESSIONALS®BOOKS FOR PROFESSIONALS BY PROFESSIONALS® US $59.99 Shelve in .NET User level: Intermediate–Advanced www.apress.com SOURCE CODE ONLINE RELATED Pro C# 5.0 and the .NET 4.5 Framework Explore the .NET universe on your own terms with Pro C# 5.0 and the .NET 4.5 Framework. From the very latest features to essential concepts, this book teaches you the ins and outs of the leading .NET technology. Led by an award-winning author who has been teaching the .NET world since version 1.0, you’ll gain a deep understanding of the core aspects of the C# programming language together with the new features introduced with the release of .NET 4.5. With Pro C# 5.0 and the .NET 4.5 Framework, you'll learn: • Object-Oriented Programming with C# • The relationship between delegates, events and lambda expressions • Programming with the LINQ technology set • Multithreaded, parallel and asynchronous programming • Interacting with legacy code using the Dynamic Language Runtime • Communicating with relational databases using ADO.NET • Creating distributed systems using WCF • Modeling business processes using WF • Building desktop and web based user interfaces using WPF and ASP.NET Whether you’re an experienced coder moving to .NET for the first time or are already writing applications using previous .NET versions, Pro C# 5.0 and the .NET 4.5 Framework will provide you with a comprehensive grounding in the new technology and serve as a complete reference throughout your coding career. ISBN 978-1-4302-4233-8 9 781430 242338 55999 BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN .NET Troelsen SIXTH EDITIONFor your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Pro C# 5.0 and the .NET 4.5 Framework Sixth Edition    Andrew Troelsen Pro C# and the .NET 4.5 Framework, Sixth Edition Copyright © 2012 by Andrew Troelsen This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4302-4233-8 ISBN-13 (electronic): 978-1-4302-4234-5 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Andy Olsen Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editors: Jessica Belanger, Christine Ricketts Copy Editors: Ralph and Vanessa Moore Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit 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 our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source- code. This edition of the text is dedicated to the two most important people in my life. First to my wife Mandy, who was crazy enough to bring up the idea of adoption. Second, to my son Soren Wade Troelsen. Words can’t express how much I love you. However, I will say one thing: Grrrrrrawwwwhhhh! You can ask me about this when you get older. iv Contents at a Glance  About the Author...................................................................................................liii  About the Technical Reviewer ..............................................................................liv  Acknowledgments .................................................................................................lv  Introduction ..........................................................................................................lvi  Part I: Introducing C# and .NET Platform................................................................1  Chapter 1: The Philosophy of .NET..........................................................................3  Chapter 2: Building C# Applications .....................................................................39  Part II: Core C# Programming...............................................................................71  Chapter 3: Core C# Programming Constructs, Part I ............................................73  Chapter 4: Core C# Programming Constructs, Part II.........................................121  Part III: Object-Oriented Programming with C#..................................................161  Chapter 5: Understanding Encapsulation ...........................................................163  Chapter 6: Understanding Inheritance and Polymorphism.................................213  Chapter 7: Understanding Structured Exception Handling .................................253  Chapter 8: Working with Interfaces....................................................................281  Part IV: Advanced C# Programming ...................................................................319  Chapter 9: Collections and Generics...................................................................321  Chapter 10: Delegates, Events, and Lambda Expressions ..................................359  Chapter 11: Advanced C# Language Features ....................................................399  Chapter 12: LINQ to Objects................................................................................439  Chapter 13: Understanding Object Lifetime........................................................473  Part V: Programming with .NET Assemblies.......................................................501  Chapter 14: Building and Configuring Class Libraries........................................503  CONTENTS v  Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming.555  Chapter 16: Dynamic Types and the Dynamic Language Runtime .....................599  Chapter 17: Processes, AppDomains, and Object Contexts................................623  Chapter 18: Understanding CIL and the Role of Dynamic Assemblies................651  Part VI: Introducing the .NET Base Class Libraries.............................................695  Chapter 19: Multithreaded, Parallel, and Async Programming ..........................697  Chapter 20: File I/O and Object Serialization......................................................753  Chapter 21: ADO.NET Part I: The Connected Layer..............................................801  Chapter 22: ADO.NET Part II: The Disconnected Layer........................................859  Chapter 23: ADO.NET Part III: The Entity Framework..........................................927  Chapter 24: Introducing LINQ to XML .................................................................967  Chapter 25: Introducing Windows Communication Foundation .........................985  Chapter 26: Introducing Windows Workflow Foundation .................................1047  Part VII: Windows Presentation Foundation .....................................................1089  Chapter 27: Introducing Windows Presentation Foundation and XAML ...........1091  Chapter 28: Programming with WPF Controls ..................................................1157  Chapter 29: WPF Graphics Rendering Services ................................................1223  Chapter 30: WPF Resources, Animations, and Styles.......................................1267  Chapter 31: Dependency Properties, Routed Events, and Templates ..............1301  Part VIII: ASP.NET Web Forms ..........................................................................1335  Chapter 32: Introducing ASP.NET Web Forms...................................................1337  Chapter 33: ASP.NET Web Controls, Master Pages, and Themes......................1383  Chapter 34: ASP.NET State Management Techniques.......................................1429  Index .................................................................................................................1463 vi Contents  About the Author...................................................................................................liii  About the Technical Reviewer ..............................................................................liv  Acknowledgments .................................................................................................lv  Introduction ..........................................................................................................lvi  Part I: Introducing C# and .NET Platform................................................................1  Chapter 1: The Philosophy of .NET..........................................................................3 An Initial Look at the .NET Platform...................................................................................3 Some Key Benefits of the .NET Platform .................................................................................................. 4 Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS).................4 The Role of the Base Class Libraries ........................................................................................................ 5 What C# Brings to the Table..................................................................................................................... 5 Managed vs. Unmanaged Code................................................................................................................ 7 Additional .NET-Aware Programming Languages..............................................................7 Life in a Multilanguage World................................................................................................................... 8 An Overview of .NET Assemblies.......................................................................................9 The Role of the Common Intermediate Language .................................................................................. 10 The Role of .NET Type Metadata............................................................................................................. 13 The Role of the Assembly Manifest ........................................................................................................ 14 Understanding the Common Type System.......................................................................15 CTS Class Types ..................................................................................................................................... 15 CTS Interface Types................................................................................................................................ 16  CONTENTS vii CTS Structure Types............................................................................................................................... 16 CTS Enumeration Types.......................................................................................................................... 17 CTS Delegate Types................................................................................................................................ 17 CTS Type Members................................................................................................................................. 17 Intrinsic CTS Data Types......................................................................................................................... 18 Understanding the Common Language Specification......................................................19 Ensuring CLS Compliance....................................................................................................................... 20 Understanding the Common Language Runtime .............................................................21 The Assembly/Namespace/Type Distinction ...................................................................22 The Role of the Microsoft Root Namespace ........................................................................................... 25 Accessing a Namespace Programmatically ........................................................................................... 26 Referencing External Assemblies........................................................................................................... 27 Exploring an Assembly Using ildasm.exe ........................................................................28 Viewing CIL Code.................................................................................................................................... 29 Viewing Type Metadata .......................................................................................................................... 30 Viewing Assembly Metadata (a.k.a. the Manifest) .................................................................................31 The Platform-Independent Nature of .NET.......................................................................31 A Brief Word Regarding Windows 8 Applications ............................................................33 Building Windows 8 Applications ........................................................................................................... 34 The Role of .NET Under Windows 8 ........................................................................................................ 35 Summary .........................................................................................................................37  Chapter 2: Building C# Applications .....................................................................39 The Role of the .NET Framework 4.5 SDK .......................................................................39 The Developer Command Prompt........................................................................................................... 40 Building C# Applications Using csc.exe...........................................................................40 Specifying Input and Output Targets ...................................................................................................... 41 Referencing External Assemblies........................................................................................................... 43  CONTENTS viii Referencing Multiple External Assemblies............................................................................................. 44 Compiling Multiple Source Files............................................................................................................. 44 Working with C# Response Files ............................................................................................................ 45 Building .NET Applications Using Notepad++ .................................................................47 Building .NET Applications Using SharpDevelop..............................................................48 Building a Simple Test Project................................................................................................................ 48 Building .NET Applications Using Visual C# Express .......................................................51 Some Unique Features of Visual C# Express.......................................................................................... 52 Building .NET Applications Using Visual Studio...............................................................52 Some Unique Features of Visual Studio.................................................................................................. 53 Targeting the .NET Framework Using the New Project Dialog Box ........................................................ 54 Using the Solution Explorer Utility .......................................................................................................... 54 The Class View Utility ............................................................................................................................. 57 The Object Browser Utility...................................................................................................................... 58 Integrated Support for Code Refactoring................................................................................................ 59 Code Snippets and Surround with Technology....................................................................................... 62 The Visual Class Designer ...................................................................................................................... 63 The Integrated .NET Framework 4.5 SDK Documentation System......................................................... 67 Summary .........................................................................................................................70  Part II: Core C# Programming...............................................................................71  Chapter 3: Core C# Programming Constructs, Part I ............................................73 The Anatomy of a Simple C# Program.............................................................................73 Variations on the Main() Method............................................................................................................. 75 Specifying an Application Error Code ..................................................................................................... 76 Processing Command-Line Arguments .................................................................................................. 77 Specifying Command-Line Arguments with Visual Studio ..................................................................... 78 An Interesting Aside: Some Additional Members of the System.Environment Class.......79  CONTENTS ix The System.Console Class...............................................................................................81 Basic Input and Output with the Console Class...................................................................................... 82 Formatting Console Output..................................................................................................................... 83 Formatting Numerical Data .................................................................................................................... 84 Formatting Numerical Data Beyond Console Applications ..................................................................... 85 System Data Types and Corresponding C# Keywords.....................................................86 Variable Declaration and Initialization .................................................................................................... 87 Intrinsic Data Types and the new Operator ............................................................................................ 89 The Data Type Class Hierarchy............................................................................................................... 89 Members of Numerical Data Types ........................................................................................................ 91 Members of System.Boolean.................................................................................................................. 92 Members of System.Char ....................................................................................................................... 92 Parsing Values from String Data............................................................................................................. 92 System.DateTime and System.TimeSpan............................................................................................... 93 The System.Numerics.dll Assembly ....................................................................................................... 93 Working with String Data.................................................................................................95 Basic String Manipulation ...................................................................................................................... 96 String Concatenation .............................................................................................................................. 97 Escape Characters.................................................................................................................................. 97 Defining Verbatim Strings....................................................................................................................... 98 Strings and Equality................................................................................................................................ 99 Strings Are Immutable.......................................................................................................................... 100 The System.Text.StringBuilder Type .................................................................................................... 101 Narrowing and Widening Data Type Conversions..........................................................102 The checked Keyword .......................................................................................................................... 105 Setting Project-Wide Overflow Checking.............................................................................................. 106 The unchecked Keyword ...................................................................................................................... 107 Understanding Implicitly Typed Local Variables............................................................108  CONTENTS x Restrictions on Implicitly Typed Variables............................................................................................ 109 Implicit Typed Data Is Strongly Typed Data.......................................................................................... 110 Usefulness of Implicitly Typed Local Variables..................................................................................... 111 C# Iteration Constructs ..................................................................................................112 The for Loop.......................................................................................................................................... 112 The foreach Loop.................................................................................................................................. 113 The while and do/while Looping Constructs ........................................................................................ 114 Decision Constructs and the Relational/Equality Operators ..........................................114 The if/else Statement ........................................................................................................................... 115 Equality and Relational Operators ........................................................................................................ 115 Conditional Operators........................................................................................................................... 116 The switch Statement........................................................................................................................... 116 Summary .......................................................................................................................118  Chapter 4: Core C# Programming Constructs, Part II.........................................121 Methods and Parameter Modifiers ................................................................................121 The Default by Value Parameter-Passing Behavior.............................................................................. 122 The out Modifier ................................................................................................................................... 123 The ref Modifier .................................................................................................................................... 125 The params Modifier ............................................................................................................................ 126 Defining Optional Parameters............................................................................................................... 127 Invoking Methods Using Named Parameters ....................................................................................... 129 Understanding Method Overloading ..................................................................................................... 130 Understanding C# Arrays...............................................................................................133 C# Array Initialization Syntax................................................................................................................ 134 Implicitly Typed Local Arrays................................................................................................................ 134 Defining an Array of Objects................................................................................................................. 135 Working with Multidimensional Arrays................................................................................................. 136 Arrays As Arguments or Return Values ................................................................................................ 137  CONTENTS xi The System.Array Base Class............................................................................................................... 138 Understanding the enum Type.......................................................................................140 Controlling the Underlying Storage for an enum .................................................................................. 141 Declaring enum Variables..................................................................................................................... 142 The System.Enum Type........................................................................................................................ 143 Dynamically Discovering an enum’s Name/Value Pairs ....................................................................... 143 Understanding the Structure Type.................................................................................146 Creating Structure Variables ................................................................................................................ 147 Understanding Value Types and Reference Types.........................................................149 Value Types, References Types, and the Assignment Operator ........................................................... 150 Value Types Containing Reference Types............................................................................................. 151 Passing Reference Types by Value....................................................................................................... 153 Passing Reference Types by Reference ............................................................................................... 155 Final Details Regarding Value Types and Reference Types.................................................................. 156 Understanding C# Nullable Types..................................................................................157 Working with Nullable Types................................................................................................................ 158 The ?? Operator.................................................................................................................................... 159 Summary .......................................................................................................................160  Part III: Object-Oriented Programming with C#..................................................161  Chapter 5: Understanding Encapsulation ...........................................................163 Introducing the C# Class Type .......................................................................................163 Allocating Objects with the new Keyword............................................................................................ 166 Understanding Constructors..........................................................................................166 The Role of the Default Constructor ..................................................................................................... 167 Defining Custom Constructors.............................................................................................................. 167 The Default Constructor Revisited........................................................................................................ 169 The Role of the this Keyword.........................................................................................170  CONTENTS xii Chaining Constructor Calls Using this .................................................................................................. 172 Observing Constructor Flow ................................................................................................................. 174 Revisiting Optional Arguments ............................................................................................................. 176 Understanding the static Keyword.................................................................................177 Defining Static Field Data..................................................................................................................... 178 Defining Static Methods ....................................................................................................................... 179 Defining Static Constructors................................................................................................................. 181 Defining Static Classes......................................................................................................................... 183 Defining the Pillars of OOP.............................................................................................184 The Role of Encapsulation .................................................................................................................... 185 The Role of Inheritance......................................................................................................................... 185 The Role of Polymorphism.................................................................................................................... 187 C# Access Modifiers ......................................................................................................188 The Default Access Modifiers............................................................................................................... 189 Access Modifiers and Nested Types..................................................................................................... 189 The First Pillar: C#’s Encapsulation Services.................................................................190 Encapsulation Using Traditional Accessors and Mutators.................................................................... 191 Encapsulation Using .NET Properties ................................................................................................... 193 Using Properties Within a Class Definition ........................................................................................... 196 Read-Only and Write-Only Properties................................................................................................... 198 Revisiting the static Keyword: Defining Static Properties .................................................................... 199 Understanding Automatic Properties.............................................................................199 Interacting with Automatic Properties.................................................................................................. 201 Regarding Automatic Properties and Default Values............................................................................ 201 Understanding Object Initialization Syntax....................................................................203 Calling Custom Constructors with Initialization Syntax ........................................................................ 204 Initializing Inner Types.......................................................................................................................... 206 Working with Constant Field Data .................................................................................207  CONTENTS xiii Understanding Read-Only Fields .......................................................................................................... 208 Static Read-Only Fields ........................................................................................................................ 209 Understanding Partial Types..........................................................................................210 Summary .......................................................................................................................211  Chapter 6: Understanding Inheritance and Polymorphism.................................213 The Basic Mechanics of Inheritance..............................................................................213 Specifying the Parent Class of an Existing Class.................................................................................. 214 Regarding Multiple Base Classes ......................................................................................................... 216 The sealed Keyword ............................................................................................................................. 216 Revising Visual Studio Class Diagrams .........................................................................218 The Second Pillar of OOP: The Details of Inheritance ....................................................220 Controlling Base Class Creation with the base Keyword...................................................................... 221 Keeping Family Secrets: The protected Keyword................................................................................. 223 Adding a Sealed Class.......................................................................................................................... 224 Programming for Containment/Delegation....................................................................225 Understanding Nested Type Definitions ............................................................................................... 226 The Third Pillar of OOP: C#’s Polymorphic Support .......................................................228 The virtual and override Keywords....................................................................................................... 229 Overriding Virtual Members Using the Visual Studio IDE...................................................................... 231 Sealing Virtual Members ...................................................................................................................... 232 Understanding Abstract Classes........................................................................................................... 233 Understanding the Polymorphic Interface ............................................................................................ 235 Understanding Member Shadowing ..................................................................................................... 239 Understanding Base Class/Derived Class Casting Rules ...............................................240 The C# as Keyword............................................................................................................................... 242 The C# is Keyword................................................................................................................................ 243 The Master Parent Class: System.Object.......................................................................243  CONTENTS xiv Overriding System.Object.ToString() .................................................................................................... 247 Overriding System.Object.Equals() ....................................................................................................... 247 Overriding System.Object.GetHashCode() ............................................................................................ 248 Testing Your Modified Person Class ..................................................................................................... 249 The Static Members of System.Object ................................................................................................. 250 Summary .......................................................................................................................250  Chapter 7: Understanding Structured Exception Handling .................................253 Ode to Errors, Bugs, and Exceptions .............................................................................253 The Role of .NET Exception Handling.............................................................................254 The Building Blocks of .NET Exception Handling.................................................................................. 255 The System.Exception Base Class........................................................................................................ 255 The Simplest Possible Example.....................................................................................257 Throwing a General Exception.............................................................................................................. 259 Catching Exceptions ............................................................................................................................. 260 Configuring the State of an Exception ...........................................................................261 The TargetSite Property........................................................................................................................ 261 The StackTrace Property ...................................................................................................................... 262 The HelpLink Property .......................................................................................................................... 263 The Data Property................................................................................................................................. 264 System-Level Exceptions (System.SystemException) ...................................................266 Application-Level Exceptions (System.ApplicationException) .......................................266 Building Custom Exceptions, Take One ................................................................................................ 267 Building Custom Exceptions, Take Two................................................................................................ 269 Building Custom Exceptions, Take Three ............................................................................................. 269 Processing Multiple Exceptions.....................................................................................271 General catch Statements .................................................................................................................... 274 Rethrowing Exceptions......................................................................................................................... 274  CONTENTS xv Inner Exceptions................................................................................................................................... 275 The finally Block ................................................................................................................................... 276 Who Is Throwing What?.................................................................................................277 The Result of Unhandled Exceptions .............................................................................277 Debugging Unhandled Exceptions Using Visual Studio .................................................278 Summary .......................................................................................................................280  Chapter 8: Working with Interfaces....................................................................281 Understanding Interface Types......................................................................................281 Interface Types vs. Abstract Base Classes........................................................................................... 282 Defining Custom Interfaces ...........................................................................................284 Implementing an Interface.............................................................................................286 Invoking Interface Members at the Object Level ...........................................................288 Obtaining Interface References: The as Keyword ................................................................................ 289 Obtaining Interface References: The is Keyword ................................................................................. 290 Interfaces As Parameters ..............................................................................................291 Interfaces As Return Values...........................................................................................293 Arrays of Interface Types...............................................................................................294 Implementing Interfaces Using Visual Studio ................................................................295 Explicit Interface Implementation..................................................................................296 Designing Interface Hierarchies ....................................................................................299 Multiple Inheritance with Interface Types ............................................................................................ 300 The IEnumerable and IEnumerator Interfaces ...............................................................302 Building Iterator Methods with the yield Keyword ............................................................................... 305 Building a Named Iterator..................................................................................................................... 306 The ICloneable Interface................................................................................................308 A More Elaborate Cloning Example ...................................................................................................... 310  CONTENTS xvi The IComparable Interface ............................................................................................313 Specifying Multiple Sort Orders with IComparer .................................................................................. 316 Custom Properties and Custom Sort Types.......................................................................................... 317 Summary .......................................................................................................................318  Part IV: Advanced C# Programming ...................................................................319  Chapter 9: Collections and Generics...................................................................321 The Motivation for Collection Classes............................................................................321 The System.Collections Namespace .................................................................................................... 323 A Survey of System.Collections.Specialized Namespace..................................................................... 325 The Problems of Nongeneric Collections.......................................................................326 The Issue of Performance..................................................................................................................... 326 The Issue of Type Safety ...................................................................................................................... 330 A First Look at Generic Collections....................................................................................................... 333 The Role of Generic Type Parameters ...........................................................................334 Specifying Type Parameters for Generic Classes/Structures............................................................... 335 Specifying Type Parameters for Generic Members .............................................................................. 336 Specifying Type Parameters for Generic Interfaces ............................................................................. 337 The System.Collections.Generic Namespace ................................................................338 Understanding Collection Initialization Syntax ..................................................................................... 340 Working with the List Class ........................................................................................................... 341 Working with the Stack Class........................................................................................................ 342 Working with the Queue Class....................................................................................................... 343 Working with the SortedSet Class................................................................................................. 345 The System.Collections.ObjectModel Namespace.........................................................346 Working with ObservableCollection............................................................................................... 347 Creating Custom Generic Methods ................................................................................349 Inference of Type Parameters .............................................................................................................. 351  CONTENTS xvii Creating Custom Generic Structures and Classes.........................................................353 The default Keyword in Generic Code .................................................................................................. 354 Constraining Type Parameters ......................................................................................355 Examples Using the where Keyword .................................................................................................... 356 The Lack of Operator Constraints......................................................................................................... 357 Summary .......................................................................................................................358  Chapter 10: Delegates, Events, and Lambda Expressions ..................................359 Understanding the .NET Delegate Type .........................................................................359 Defining a Delegate Type in C# ............................................................................................................ 360 The System.MulticastDelegate and System.Delegate Base Classes.................................................... 362 The Simplest Possible Delegate Example......................................................................364 Investigating a Delegate Object............................................................................................................ 365 Sending Object State Notifications Using Delegates .....................................................367 Enabling Multicasting ........................................................................................................................... 370 Removing Targets from a Delegate’s Invocation List ........................................................................... 371 Method Group Conversion Syntax ........................................................................................................ 372 Understanding Generic Delegates .................................................................................374 The Generic Action<> and Func<> Delegates..................................................................................... 375 Understanding C# Events...............................................................................................378 The C# event Keyword.......................................................................................................................... 379 Events Under the Hood ......................................................................................................................... 380 Listening to Incoming Events ............................................................................................................... 381 Simplifying Event Registration Using Visual Studio.............................................................................. 383 Creating Custom Event Arguments....................................................................................................... 384 The Generic EventHandler Delegate .............................................................................................. 386 Understanding C# Anonymous Methods........................................................................387 Accessing Local Variables.................................................................................................................... 389  CONTENTS xviii Understanding Lambda Expressions .............................................................................390 Dissecting a Lambda Expression.......................................................................................................... 393 Processing Arguments Within Multiple Statements ............................................................................. 394 Lambda Expressions with Multiple (or Zero) Parameters..................................................................... 395 Retrofitting the CarEvents Example Using Lambda Expressions.......................................................... 396 Summary .......................................................................................................................397  Chapter 11: Advanced C# Language Features ....................................................399 Understanding Indexer Methods....................................................................................399 Indexing Data Using String Values........................................................................................................ 401 Overloading Indexer Methods............................................................................................................... 403 Indexers with Multiple Dimensions ...................................................................................................... 403 Indexer Definitions on Interface Types ................................................................................................. 404 Understanding Operator Overloading ............................................................................404 Overloading Binary Operators............................................................................................................... 405 And What of the += and –+ Operators?............................................................................................... 408 Overloading Unary Operators................................................................................................................ 408 Overloading Equality Operators ............................................................................................................ 409 Overloading Comparison Operators...................................................................................................... 410 Final Thoughts Regarding Operator Overloading.................................................................................. 411 Understanding Custom Type Conversions .....................................................................412 Recall: Numerical Conversions............................................................................................................. 412 Recall: Conversions Among Related Class Types................................................................................. 412 Creating Custom Conversion Routines ................................................................................................. 413 Additional Explicit Conversions for the Square Type............................................................................ 416 Defining Implicit Conversion Routines.................................................................................................. 417 Understanding Extension Methods ................................................................................418 Defining Extension Methods................................................................................................................. 418 Invoking Extension Methods................................................................................................................. 420  CONTENTS xix Importing Extension Methods............................................................................................................... 421 The IntelliSense of Extension Methods................................................................................................. 421 Extending Types Implementing Specific Interfaces.............................................................................. 422 Understanding Anonymous Types .................................................................................423 Defining an Anonymous Type............................................................................................................... 424 The Internal Representation of Anonymous Types ............................................................................... 425 The Implementation of ToString() and GetHashCode().......................................................................... 426 The Semantics of Equality for Anonymous Types................................................................................. 427 Anonymous Types Containing Anonymous Types ................................................................................ 429 Working with Pointer Types...........................................................................................429 The unsafe Keyword............................................................................................................................. 431 Working with the * and & Operators..................................................................................................... 433 An Unsafe (and Safe) Swap Function ................................................................................................... 434 Field Access via Pointers (the -> Operator).......................................................................................... 435 The stackalloc Keyword ....................................................................................................................... 435 Pinning a Type via the fixed Keyword................................................................................................... 436 The sizeof Keyword .............................................................................................................................. 437 Summary .......................................................................................................................438  Chapter 12: LINQ to Objects................................................................................439 LINQ-Specific Programming Constructs ........................................................................439 Implicit Typing of Local Variables......................................................................................................... 440 Object and Collection Initialization Syntax............................................................................................ 441 Lambda Expressions............................................................................................................................. 441 Extension Methods ............................................................................................................................... 442 Anonymous Types ................................................................................................................................ 443 Understanding the Role of LINQ.....................................................................................443 LINQ Expressions Are Strongly Typed................................................................................................... 444 The Core LINQ Assemblies.................................................................................................................... 444  CONTENTS xx Applying LINQ Queries to Primitive Arrays.....................................................................445 Once Again, Without LINQ..................................................................................................................... 447 Reflecting over a LINQ Result Set......................................................................................................... 447 LINQ and Implicitly Typed Local Variables............................................................................................ 448 LINQ and Extension Methods................................................................................................................ 449 The Role of Deferred Execution ............................................................................................................ 450 The Role of Immediate Execution......................................................................................................... 452 Returning the Result of a LINQ Query ............................................................................452 Returning LINQ Results via Immediate Execution ................................................................................ 454 Applying LINQ Queries to Collection Objects .................................................................455 Accessing Contained Subobjects ......................................................................................................... 455 Applying LINQ Queries to Nongeneric Collections ................................................................................ 456 Filtering Data Using OfType()......................................................................................................... 457 Investigating the C# LINQ Query Operators ...................................................................457 Basic Selection Syntax ......................................................................................................................... 459 Obtaining Subsets of Data.................................................................................................................... 460 Projecting New Data Types .................................................................................................................. 461 Obtaining Counts Using Enumerable .................................................................................................... 462 Reversing Result Sets........................................................................................................................... 462 Sorting Expressions.............................................................................................................................. 463 LINQ As a Better Venn Diagramming Tool ............................................................................................ 463 Removing Duplicates............................................................................................................................ 464 LINQ Aggregation Operations ............................................................................................................... 465 The Internal Representation of LINQ Query Statements ................................................466 Building Query Expressions with Query Operators (Revisited) ............................................................. 466 Building Query Expressions Using the Enumerable Type and Lambda Expressions ............................ 467 Building Query Expressions Using the Enumerable Type and Anonymous Methods............................ 468 Building Query Expressions Using the Enumerable Type and Raw Delegates .................................... 469  CONTENTS xxi Summary .......................................................................................................................470  Chapter 13: Understanding Object Lifetime........................................................473 Classes, Objects, and References .................................................................................473 The Basics of Object Lifetime ........................................................................................475 The CIL of new...................................................................................................................................... 475 Setting Object References to null......................................................................................................... 477 The Role of Application Roots........................................................................................477 Understanding Object Generations................................................................................479 Concurrent Garbage Collection Under .NET 1.0–3.5......................................................480 Background Garbage Collection Under .NET 4.0 and Greater........................................481 The System.GC Type......................................................................................................481 Forcing a Garbage Collection................................................................................................................ 482 Building Finalizable Objects...........................................................................................485 Overriding System.Object.Finalize()...................................................................................................... 486 Detailing the Finalization Process ........................................................................................................ 488 Building Disposable Objects ..........................................................................................489 Reusing the C# using Keyword............................................................................................................. 491 Building Finalizable and Disposable Types....................................................................493 A Formalized Disposal Pattern.............................................................................................................. 493 Understanding Lazy Object Instantiation .......................................................................496 Customizing the Creation of the Lazy Data........................................................................................... 498 Summary .......................................................................................................................499  Part V: Programming with .NET Assemblies.......................................................501  Chapter 14: Building and Configuring Class Libraries........................................503 Defining Custom Namespaces.......................................................................................503 Resolving Name Clashes with Fully Qualified Names .......................................................................... 505  CONTENTS xxii Resolving Name Clashes with Aliases.................................................................................................. 506 Creating Nested Namespaces .............................................................................................................. 508 The Default Namespace of Visual Studio.............................................................................................. 509 The Role of .NET Assemblies .........................................................................................510 Assemblies Promote Code Reuse......................................................................................................... 510 Assemblies Establish a Type Boundary................................................................................................ 510 Assemblies Are Versionable Units ........................................................................................................ 510 Assemblies Are Self-Describing ........................................................................................................... 511 Assemblies Are Configurable................................................................................................................ 511 Understanding the Format of a .NET Assembly .............................................................511 The Windows File Header..................................................................................................................... 512 The CLR File Header ............................................................................................................................. 513 CIL Code, Type Metadata, and the Assembly Manifest......................................................................... 514 Optional Assembly Resources .............................................................................................................. 514 Building and Consuming Custom Class Library.............................................................514 Exploring the Manifest.......................................................................................................................... 518 Exploring the CIL................................................................................................................................... 520 Exploring the Type Metadata................................................................................................................ 521 Building a C# Client Application ........................................................................................................... 522 Building a Visual Basic Client Application ............................................................................................ 524 Cross-Language Inheritance in Action.................................................................................................. 525 Understanding Private Assemblies................................................................................526 The Identity of a Private Assembly ....................................................................................................... 526 Understanding the Probing Process ..................................................................................................... 526 Configuring Private Assemblies............................................................................................................ 527 The Role of the App.Config File ............................................................................................................ 529 Understanding Shared Assemblies................................................................................531 The Global Assembly Cache ................................................................................................................. 532  CONTENTS xxiii Understanding Strong Names............................................................................................................... 534 Generating Strong Names at the Command Line ................................................................................. 535 Generating Strong Names Using Visual Studio..................................................................................... 537 Installing Strongly Named Assemblies to the GAC ............................................................................... 539 Consuming a Shared Assembly .....................................................................................541 Exploring the Manifest of SharedCarLibClient...................................................................................... 543 Configuring Shared Assemblies.....................................................................................543 Freezing the Current Shared Assembly................................................................................................ 544 Building a Shared Assembly Version 2.0.0.0........................................................................................ 544 Dynamically Redirecting to Specific Versions of a Shared Assembly .................................................. 547 Understanding Publisher Policy Assemblies..................................................................548 Disabling Publisher Policy .................................................................................................................... 549 Understanding the Element......................................................................550 The System.Configuration Namespace..........................................................................552 The Configuration File Schema Documentation ............................................................553 Summary .......................................................................................................................554  Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming.555 The Necessity of Type Metadata....................................................................................555 Viewing (Partial) Metadata for the EngineState Enumeration .............................................................. 556 Viewing (Partial) Metadata for the Car Type......................................................................................... 557 Examining a TypeRef............................................................................................................................ 559 Documenting the Defining Assembly.................................................................................................... 559 Documenting Referenced Assemblies.................................................................................................. 559 Documenting String Literals................................................................................................................. 560 Understanding Reflection ..............................................................................................560 The System.Type Class......................................................................................................................... 561 Obtaining a Type Reference Using System.Object.GetType() ............................................................... 562  CONTENTS xxiv Obtaining a Type Reference Using typeof() .......................................................................................... 563 Obtaining a Type Reference Using System.Type.GetType().................................................................. 563 Building a Custom Metadata Viewer..............................................................................564 Reflecting on Methods.......................................................................................................................... 564 Reflecting on Fields and Properties...................................................................................................... 565 Reflecting on Implemented Interfaces.................................................................................................. 565 Displaying Various Odds and Ends ....................................................................................................... 566 Implementing Main()............................................................................................................................. 566 Reflecting on Generic Types................................................................................................................. 568 Reflecting on Method Parameters and Return Values.......................................................................... 568 Dynamically Loading Assemblies ..................................................................................569 Reflecting on Shared Assemblies..................................................................................572 Understanding Late Binding ..........................................................................................574 The System.Activator Class.................................................................................................................. 574 Invoking Methods with No Parameters................................................................................................. 576 Invoking Methods with Parameters...................................................................................................... 577 Understanding the Role of .NET Attributes ....................................................................578 Attribute Consumers............................................................................................................................. 579 Applying Attributes in C#...................................................................................................................... 579 C# Attribute Shorthand Notation........................................................................................................... 581 Specifying Constructor Parameters for Attributes................................................................................ 581 The Obsolete Attribute in Action........................................................................................................... 582 Building Custom Attributes............................................................................................583 Applying Custom Attributes.................................................................................................................. 583 Named Property Syntax........................................................................................................................ 584 Restricting Attribute Usage................................................................................................................... 584 Assembly-Level Attributes.............................................................................................585 The Visual Studio AssemblyInfo.cs File ................................................................................................ 586  CONTENTS xxv Reflecting on Attributes Using Early Binding .................................................................588 Reflecting on Attributes Using Late Binding ..................................................................589 Putting Reflection, Late Binding, and Custom Attributes in Perspective .......................591 Building an Extendable Application ...............................................................................591 Building CommonSnappableTypes.dll .................................................................................................. 592 Building the C# Snap-In........................................................................................................................ 593 Building the Visual Basic Snap-In......................................................................................................... 593 Building an Extendable Windows Forms Application ........................................................................... 594 Summary .......................................................................................................................598  Chapter 16: Dynamic Types and the Dynamic Language Runtime .....................599 The Role of the C# dynamic Keyword............................................................................599 Calling Members on Dynamically Declared Data.................................................................................. 601 The Role of the Microsoft.CSharp.dll Assembly.................................................................................... 603 The Scope of the dynamic Keyword ..................................................................................................... 604 Limitations of the dynamic Keyword .................................................................................................... 605 Practical Uses of the dynamic Keyword ............................................................................................... 605 The Role of the Dynamic Language Runtime (DLR) .......................................................606 The Role of Expression Trees ............................................................................................................... 606 The Role of the System.Dynamic Namespace...................................................................................... 607 Dynamic Runtime Lookup of Expression Trees .................................................................................... 607 Simplifying Late-Bound Calls Using Dynamic Types .....................................................608 Leveraging the dynamic Keyword to Pass Arguments ......................................................................... 609 Simplifying COM Interoperability Using Dynamic Data..................................................612 The Role of Primary Interop Assemblies (PIAs) .................................................................................... 613 Embedding Interop Metadata ............................................................................................................... 614 Common COM Interop Pain Points........................................................................................................ 615 COM Interop Using C# Dynamic Data.............................................................................616  CONTENTS xxvi COM interop Without C# Dynamic Data ................................................................................................ 620 Summary .......................................................................................................................621  Chapter 17: Processes, AppDomains, and Object Contexts................................623 The Role of a Windows Process.....................................................................................623 The Role of Threads.............................................................................................................................. 624 Interacting with Processes Under the .NET Platform.....................................................625 Enumerating Running Processes.......................................................................................................... 628 Investigating a Specific Process........................................................................................................... 629 Investigating a Process’s Thread Set ................................................................................................... 629 Investigating a Process’s Module Set................................................................................................... 632 Starting and Stopping Processes Programmatically ............................................................................ 633 Controlling Process Startup Using the ProcessStartInfo Class............................................................. 634 Understanding .NET Application Domains .....................................................................635 The System.AppDomain Class.............................................................................................................. 636 Interacting with the Default Application Domain...........................................................638 Enumerating Loaded Assemblies ......................................................................................................... 639 Receiving Assembly Load Notifications................................................................................................ 640 Creating New Application Domains ...............................................................................641 Loading Assemblies into Custom Application Domains........................................................................ 643 Programmatically Unloading AppDomains ........................................................................................... 644 Understanding Object Context Boundaries....................................................................646 Context-Agile and Context-Bound Types.............................................................................................. 646 Defining a Context-Bound Object ......................................................................................................... 647 Inspecting an Object’s Context............................................................................................................. 647 Summarizing Processes, AppDomains, and Context.....................................................649 Summary .......................................................................................................................649  CONTENTS xxvii  Chapter 18: Understanding CIL and the Role of Dynamic Assemblies................651 Reasons for Learning the Grammar of CIL.....................................................................651 Examining CIL Directives, Attributes, and Opcodes.......................................................652 The Role of CIL Directives..................................................................................................................... 653 The Role of CIL Attributes..................................................................................................................... 653 The Role of CIL Opcodes....................................................................................................................... 653 The CIL Opcode/CIL Mnemonic Distinction........................................................................................... 653 Pushing and Popping: The Stack-Based Nature of CIL..................................................654 Understanding Round-Trip Engineering.........................................................................656 The Role of CIL Code Labels ................................................................................................................. 659 Interacting with CIL: Modifying an *.il File............................................................................................ 660 Compiling CIL Code Using ilasm.exe .................................................................................................... 661 The Role of peverify.exe ....................................................................................................................... 662 Understanding CIL Directives and Attributes.................................................................663 Specifying Externally Referenced Assemblies in CIL............................................................................ 663 Defining the Current Assembly in CIL................................................................................................... 663 Defining Namespaces in CIL................................................................................................................. 664 Defining Class Types in CIL .................................................................................................................. 665 Defining and Implementing Interfaces in CIL........................................................................................ 666 Defining Structures in CIL..................................................................................................................... 667 Defining Enums in CIL........................................................................................................................... 667 Defining Generics in CIL ....................................................................................................................... 668 Compiling the CILTypes.il file ............................................................................................................... 668 .NET Base Class Library, C#, and CIL Data Type Mappings ...........................................669 Defining Type Members in CIL.......................................................................................670 Defining Field Data in CIL ..................................................................................................................... 670 Defining Type Constructors in CIL ........................................................................................................ 671 Defining Properties in CIL..................................................................................................................... 671  CONTENTS xxviii Defining Member Parameters............................................................................................................... 672 Examining CIL Opcodes .................................................................................................673 The .maxstack Directive ....................................................................................................................... 675 Declaring Local Variables in CIL ........................................................................................................... 676 Mapping Parameters to Local Variables in CIL..................................................................................... 677 The Hidden this Reference ................................................................................................................... 677 Representing Iteration Constructs in CIL.............................................................................................. 678 Building a .NET Assembly with CIL ................................................................................679 Building CILCars.dll............................................................................................................................... 679 Building CILCarClient.exe ..................................................................................................................... 681 Understanding Dynamic Assemblies .............................................................................683 Exploring the System.Reflection.Emit Namespace............................................................................... 684 The Role of the System.Reflection.Emit.ILGenerator............................................................................ 685 Emitting a Dynamic Assembly.............................................................................................................. 686 Emitting the Assembly and Module Set................................................................................................ 688 The Role of the ModuleBuilder Type..................................................................................................... 689 Emitting the HelloClass Type and the String Member Variable ............................................................ 690 Emitting the Constructors..................................................................................................................... 691 Emitting the SayHello() Method ............................................................................................................ 692 Using the Dynamically Generated Assembly ........................................................................................ 692 Summary .......................................................................................................................693  Part VI: Introducing the .NET Base Class Libraries.............................................695  Chapter 19: Multithreaded, Parallel, and Async Programming ..........................697 The Process/AppDomain/Context/Thread Relationship .................................................697 The Problem of Concurrency ................................................................................................................ 698 The Role of Thread Synchronization..................................................................................................... 699 A Brief Review of the .NET Delegate..............................................................................699  CONTENTS xxix The Asynchronous Nature of Delegates.........................................................................701 The BeginInvoke() and EndInvoke() Methods ....................................................................................... 701 The System.IAsyncResult Interface ...................................................................................................... 702 Invoking a Method Asynchronously...............................................................................703 Synchronizing the Calling Thread......................................................................................................... 703 The Role of the AsyncCallback Delegate .............................................................................................. 705 The Role of the AsyncResult Class ....................................................................................................... 708 Passing and Receiving Custom State Data........................................................................................... 708 The System.Threading Namespace...............................................................................709 The System.Threading.Thread Class .............................................................................711 Obtaining Statistics About the Current Thread of Execution ................................................................ 712 The Name Property............................................................................................................................... 713 The Priority Property............................................................................................................................. 713 Manually Creating Secondary Threads..........................................................................714 Working with the ThreadStart Delegate ............................................................................................... 715 Working with the ParameterizedThreadStart Delegate ........................................................................ 717 The AutoResetEvent Class.................................................................................................................... 718 Foreground Threads and Background Threads .................................................................................... 719 The Issue of Concurrency ..............................................................................................720 Synchronization Using the C# lock Keyword ........................................................................................ 723 Synchronization Using the System.Threading.Monitor Type ................................................................ 725 Synchronization Using the System.Threading.Interlocked Type .......................................................... 726 Synchronization Using the [Synchronization] Attribute ........................................................................ 727 Programming with Timer Callbacks ..............................................................................727 Understanding the CLR ThreadPool...............................................................................729 Parallel Programming Using the Task Parallel Library ..................................................731 The System.Threading.Tasks Namespace............................................................................................ 731  CONTENTS xxx The Role of the Parallel Class............................................................................................................... 732 Data Parallelism with the Parallel Class............................................................................................... 733 Accessing UI Elements on Secondary Threads..................................................................................... 735 The Task Class...................................................................................................................................... 736 Handling Cancellation Request............................................................................................................. 736 Task Parallelism Using the Parallel Class............................................................................................. 738 Parallel LINQ Queries (PLINQ) ........................................................................................741 Opting in to a PLINQ Query ................................................................................................................... 743 Cancelling a PLINQ Query..................................................................................................................... 743 Asynchronous Calls Under .NET 4.5...............................................................................744 A First Look at the C# async and await Keywords ............................................................................... 744 Naming Conventions for Async Methods.............................................................................................. 746 Async Methods Returning Void............................................................................................................. 748 Async Methods with Multiple Awaits.................................................................................................... 748 Retrofitting the AddWithThreads Example Using Asycn/Await............................................................. 749 Summary .......................................................................................................................751  Chapter 20: File I/O and Object Serialization......................................................753 Exploring the System.IO Namespace.............................................................................753 The Directory(Info) and File(Info) Types.........................................................................754 The Abstract FileSystemInfo Base Class .............................................................................................. 755 Working with the DirectoryInfo Type .............................................................................756 Enumerating Files with the DirectoryInfo Type .................................................................................... 758 Creating Subdirectories with the DirectoryInfo Type............................................................................ 758 Working with the Directory Type ...................................................................................760 Working with the DriveInfo Class Type..........................................................................760 Working with the FileInfo Class .....................................................................................762 The FileInfo.Create() Method ................................................................................................................ 763  CONTENTS xxxi The FileInfo.Open() Method .................................................................................................................. 764 The FileInfo.OpenRead() and FileInfo.OpenWrite() Methods ................................................................. 765 The FileInfo.OpenText() Method............................................................................................................ 766 The FileInfo.CreateText() and FileInfo.AppendText() Methods .............................................................. 766 Working with the File Type ............................................................................................766 Additional File-Centric Members.......................................................................................................... 767 The Abstract Stream Class ............................................................................................768 Working with FileStreams .................................................................................................................... 770 Working with StreamWriters and StreamReaders.........................................................771 Writing to a Text File............................................................................................................................. 772 Reading from a Text File....................................................................................................................... 773 Directly Creating StreamWriter/StreamReader Types .......................................................................... 774 Working with StringWriters and StringReaders.............................................................774 Working with BinaryWriters and BinaryReaders............................................................776 Watching Files Programmatically ..................................................................................778 Understanding Object Serialization ...............................................................................780 The Role of Object Graphs .................................................................................................................... 781 Configuring Objects for Serialization .............................................................................783 Defining Serializable Types .................................................................................................................. 783 Public Fields, Private Fields, and Public Properties.............................................................................. 784 Choosing a Serialization Formatter................................................................................784 The IFormatter and IRemotingFormatter Interfaces ............................................................................. 785 Type Fidelity Among the Formatters..................................................................................................... 786 Serializing Objects Using the BinaryFormatter..............................................................787 Deserializing Objects Using the BinaryFormatter................................................................................. 788 Serializing Objects Using the SoapFormatter ................................................................789 Serializing Objects Using the XmlSerializer...................................................................790  CONTENTS xxxii Controlling the Generated XML Data .................................................................................................... 791 Serializing Collections of Objects ..................................................................................792 Customizing the Soap/Binary Serialization Process ......................................................794 A Deeper Look at Object Serialization .................................................................................................. 795 Customizing Serialization Using ISerializable....................................................................................... 796 Customizing Serialization Using Attributes........................................................................................... 799 Summary .......................................................................................................................800  Chapter 21: ADO.NET Part I: The Connected Layer..............................................801 A High-Level Definition of ADO.NET...............................................................................801 The Three Faces of ADO.NET................................................................................................................ 802 Understanding ADO.NET Data Providers........................................................................803 The Microsoft-Supplied ADO.NET Data Providers................................................................................. 804 A Word Regarding System.Data.OracleClient.dll .................................................................................. 806 Obtaining Third-Party ADO.NET Data Providers.................................................................................... 806 Additional ADO.NET Namespaces..................................................................................806 The Types of the System.Data Namespace ...................................................................807 The Role of the IDbConnection Interface .............................................................................................. 809 The Role of the IDbTransaction Interface ............................................................................................. 809 The Role of the IDbCommand Interface................................................................................................ 809 The Role of the IDbDataParameter and IDataParameter Interfaces ..................................................... 810 The Role of the IDbDataAdapter and IDataAdapter Interfaces.............................................................. 811 The Role of the IDataReader and IDataRecord Interfaces .................................................................... 811 Abstracting Data Providers Using Interfaces .................................................................812 Increasing Flexibility Using Application Configuration Files................................................................. 814 Creating the AutoLot Database......................................................................................815 Creating the Inventory Table ................................................................................................................ 816 Adding Test Records to the Inventory Table......................................................................................... 818  CONTENTS xxxiii Authoring the GetPetName() Stored Procedure .................................................................................... 819 Creating the Customers and Orders Tables.......................................................................................... 820 Visually Creating Table Relationships................................................................................................... 823 The ADO.NET Data Provider Factory Model ...................................................................824 A Complete Data Provider Factory Example ......................................................................................... 825 A Potential Drawback with the Data Provider Factory Model............................................................... 828 The Element....................................................................................................... 829 Understanding the Connected Layer of ADO.NET ..........................................................830 Working with Connection Objects......................................................................................................... 831 Working with ConnectionStringBuilder Objects.................................................................................... 833 Working with Command Objects .......................................................................................................... 834 Working with Data Readers ...........................................................................................835 Obtaining Multiple Result Sets Using a Data Reader............................................................................ 837 Building a Reusable Data Access Library ......................................................................838 Adding the Connection Logic................................................................................................................ 839 Adding the Insertion Logic.................................................................................................................... 840 Adding the Deletion Logic..................................................................................................................... 841 Adding the Update Logic ...................................................................................................................... 841 Adding the Selection Logic................................................................................................................... 842 Working with Parameterized Command Objects.................................................................................. 843 Executing a Stored Procedure .............................................................................................................. 845 Creating a Console UI–Based Front End ........................................................................847 Implementing the Main() Method ......................................................................................................... 847 Implementing the ShowInstructions() Method...................................................................................... 849 Implementing the ListInventory() Method............................................................................................. 849 Implementing the DeleteCar() Method.................................................................................................. 850 Implementing the InsertNewCar() Method............................................................................................ 851 Implementing the UpdateCarPetName() Method .................................................................................. 851  CONTENTS xxxiv Implementing LookUpPetName().......................................................................................................... 852 Understanding Database Transactions..........................................................................853 Key Members of an ADO.NET Transaction Object................................................................................. 854 Adding a CreditRisks Table to the AutoLot Database ........................................................................... 855 Adding a Transaction Method to InventoryDAL .................................................................................... 856 Testing Your Database Transaction...................................................................................................... 857 Summary .......................................................................................................................858  Chapter 22: ADO.NET Part II: The Disconnected Layer........................................859 Understanding the Disconnected Layer of ADO.NET......................................................859 Understanding the Role of the DataSet..........................................................................861 Key Properties of the DataSet............................................................................................................... 861 Key Methods of the DataSet ................................................................................................................. 862 Building a DataSet................................................................................................................................ 863 Working with DataColumns ...........................................................................................864 Building a DataColumn ......................................................................................................................... 865 Enabling Autoincrementing Fields........................................................................................................ 866 Adding DataColumn Objects to a DataTable......................................................................................... 866 Working with DataRows ................................................................................................867 Understanding the RowState Property ................................................................................................. 868 Understanding the DataRowVersion Property ...................................................................................... 870 Working with DataTables...............................................................................................871 Inserting DataTables into DataSets ...................................................................................................... 872 Obtaining Data in a DataSet ................................................................................................................. 872 Processing DataTable Data Using DataTableReader Objects ............................................................... 873 Serializing DataTable/DataSet Objects As XML .................................................................................... 874 Serializing DataTable/DataSet Objects in a Binary Format................................................................... 876 Binding DataTable Objects to Windows Forms GUIs......................................................877  CONTENTS xxxv Hydrating a DataTable from a Generic List.................................................................................... 878 Deleting Rows from a DataTable .......................................................................................................... 881 Selecting Rows Based on Filter Criteria ............................................................................................... 883 Updating Rows Within a DataTable ...................................................................................................... 886 Working with the DataView Type.......................................................................................................... 886 Working with Data Adapters..........................................................................................888 A Simple Data Adapter Example........................................................................................................... 889 Mapping Database Names to Friendly Names...................................................................................... 890 Adding Disconnection Functionality to AutoLotDAL.dll..................................................892 Defining the Initial Class Type .............................................................................................................. 892 Configuring the Data Adapter Using the SqlCommandBuilder ............................................................. 892 Implementing GetAllInventory() ............................................................................................................ 894 Implementing UpdateInventory() .......................................................................................................... 894 Setting Your Version Number ............................................................................................................... 894 Testing the Disconnected Functionality................................................................................................ 894 Multitabled DataSet Objects and Data Relationships ....................................................896 Prepping the Data Adapters.................................................................................................................. 897 Building the Table Relationships .......................................................................................................... 899 Updating the Database Tables.............................................................................................................. 899 Navigating Between Related Tables..................................................................................................... 900 The Windows Forms Database Designer Tools..............................................................902 Visually Designing the DataGridView.................................................................................................... 902 The Generated App.config File ............................................................................................................. 907 Examining the Strongly Typed DataSet ................................................................................................ 908 Examining the Strongly Typed DataTable............................................................................................. 909 Examining the Strongly Typed DataRow............................................................................................... 910 Examining the Strongly Typed Data Adapter ........................................................................................ 911 Completing the Windows Forms Application........................................................................................ 912  CONTENTS xxxvi Isolating Strongly Typed Database Code into a Class Library........................................913 Viewing the Generated Code ................................................................................................................ 915 Selecting Data with the Generated Code.............................................................................................. 917 Inserting Data with the Generated Code............................................................................................... 918 Deleting Data with the Generated Code................................................................................................ 919 Invoking a Stored Procedure Using the Generated Code...................................................................... 919 Programming with LINQ to DataSet...............................................................................920 The Role of the DataSet Extensions Library ......................................................................................... 922 Obtaining a LINQ-Compatible DataTable .............................................................................................. 923 The Role of the DataRowExtensions.Field() Extension Method...................................................... 924 Hydrating New DataTables from LINQ Queries..................................................................................... 925 Summary .......................................................................................................................926  Chapter 23: ADO.NET Part III: The Entity Framework..........................................927 Understanding the Role of Entity Framework................................................................927 The Role of Entities............................................................................................................................... 929 The Building Blocks of the Entity Framework....................................................................................... 931 Building and Analyzing Your First EDM..........................................................................937 Programming Against the Conceptual Model ................................................................950 AutoLotDAL Version Four, Now with Entities .................................................................956 The Role of Navigation Properties ........................................................................................................ 957 Using Navigation Properties within LINQ to Entity Queries................................................................... 959 Invoking a Stored Procedure ................................................................................................................ 961 Data Binding Entities to Windows Forms GUIs ..............................................................961 Adding the Data-Binding Code ............................................................................................................. 964 Going Forward with .NET Data-Access APIs..................................................................965 Summary .......................................................................................................................966  CONTENTS xxxvii  Chapter 24: Introducing LINQ to XML .................................................................967 A Tale of Two XML APIs .................................................................................................967 LINQ to XML As a Better DOM............................................................................................................... 969 VB Literal Syntax As a Better LINQ to XML ........................................................................................... 969 Members of the System.Xml.Linq Namespace..............................................................971 The LINQ to XML Axis Methods............................................................................................................. 973 The Oddness of XName (and XNamespace) ......................................................................................... 975 Working with XElement and XDocument .......................................................................975 Generating Documents from Arrays and Containers ............................................................................ 978 Loading and Parsing XML Content........................................................................................................ 979 Manipulating an In-Memory XML Document .................................................................980 Building the UI of the LINQ to XML App ................................................................................................ 980 Import the Inventory.xml File................................................................................................................ 981 Defining a LINQ to XML Helper Class.................................................................................................... 981 Attaching the UI to Your Helper Class................................................................................................... 983 Summary .......................................................................................................................984  Chapter 25: Introducing Windows Communication Foundation .........................985 A Potpourri of Distributed Computing APIs....................................................................985 The Role of DCOM................................................................................................................................. 986 The Role of COM+/Enterprise Services ................................................................................................ 987 The Role of MSMQ................................................................................................................................ 987 The Role of .NET Remoting................................................................................................................... 988 The Role of XML Web Services............................................................................................................. 988 The Role of WCF.............................................................................................................990 An Overview of WCF Features .............................................................................................................. 990 An Overview of Service-Oriented Architecture..................................................................................... 991 WCF: The Bottom Line .......................................................................................................................... 992  CONTENTS xxxviii Investigating the Core WCF Assemblies ........................................................................992 The Visual Studio WCF Project Templates.....................................................................993 The WCF Service Web Site Project Template ....................................................................................... 995 The Basic Composition of a WCF Application ................................................................996 The ABCs of WCF ...........................................................................................................997 Understanding WCF Contracts.............................................................................................................. 997 Understanding WCF Bindings ............................................................................................................... 999 Understanding WCF Addresses .......................................................................................................... 1001 Building a WCF Service................................................................................................1002 The [ServiceContract] Attribute .......................................................................................................... 1004 The [OperationContract] Attribute....................................................................................................... 1005 Service Types As Operational Contracts............................................................................................. 1005 Hosting the WCF Service .............................................................................................1006 Establishing the ABCs Within an App.config File................................................................................ 1007 Coding Against the ServiceHost Type................................................................................................. 1007 Specifying Base Addresses ................................................................................................................ 1008 Details of the ServiceHost Type.......................................................................................................... 1009 Details of the Element................................................................................. 1011 Enabling Metadata Exchange ............................................................................................................. 1012 Building the WCF Client Application ............................................................................1015 Generating Proxy Code Using svcutil.exe ........................................................................................... 1015 Generating Proxy Code Using Visual Studio ....................................................................................... 1016 Configuring a TCP-Based Binding....................................................................................................... 1018 Simplifying Configuration Settings ..............................................................................1020 Leveraging Default Endpoints............................................................................................................. 1020 Exposing a Single WCF Service Using Multiple Bindings ................................................................... 1021 Changing Settings for a WCF Binding................................................................................................. 1022 Leveraging the Default MEX Behavior Configuration.......................................................................... 1024  CONTENTS xxxix Refreshing the Client Proxy and Selecting the Binding ...................................................................... 1025 Using the WCF Service Library Project Template ........................................................1027 Building a Simple Math Service.......................................................................................................... 1027 Testing the WCF Service with WcfTestClient.exe ............................................................................... 1027 Altering Configuration Files Using SvcConfigEditor.exe ..................................................................... 1028 Hosting the WCF Service Within a Windows Service...................................................1030 Specifying the ABCs in Code .............................................................................................................. 1031 Enabling MEX...................................................................................................................................... 1033 Creating a Windows Service Installer................................................................................................. 1033 Installing the Windows Service .......................................................................................................... 1035 Invoking a Service Asynchronously from the Client ....................................................1036 Designing WCF Data Contracts....................................................................................1039 Using the Web-Centric WCF Service Project Template....................................................................... 1040 Implementing the Service Contract .................................................................................................... 1042 The Role of the *.svc File.................................................................................................................... 1043 Examining the Web.config File ........................................................................................................... 1043 Testing the Service............................................................................................................................. 1044 Summary .....................................................................................................................1044  Chapter 26: Introducing Windows Workflow Foundation .................................1047 Defining a Business Process .......................................................................................1047 The Role of WF.................................................................................................................................... 1048 Building a Simple Workflow.........................................................................................1048 The Workflow Runtime ................................................................................................1051 Hosting a Workflow Using WorkflowInvoker ...................................................................................... 1052 Hosting a Workflow Using WorkflowApplication ................................................................................ 1055 Recap of Your First Workflow............................................................................................................. 1056 Examining the Workflow Activities ..............................................................................1057  CONTENTS xl Control Flow Activities........................................................................................................................ 1057 Flowchart Activities ............................................................................................................................ 1058 Messaging Activities........................................................................................................................... 1058 The State Machine Activities .............................................................................................................. 1059 The Runtime and Primitives Activities................................................................................................ 1059 The Transaction Activities .................................................................................................................. 1060 The Collection and Error Handling Activities ...................................................................................... 1060 Building a Flowchart Workflow....................................................................................1061 Connecting Activities in a Flowchart .................................................................................................. 1062 Working with the InvokeMethod Activity............................................................................................ 1063 Defining Workflow-Wide Variables..................................................................................................... 1064 Working with the FlowDecision Activity ............................................................................................. 1065 Working with the TerminateWorkflow Activity ................................................................................... 1066 Building the “True” Condition............................................................................................................. 1067 Working with the ForEach Activity............................................................................................... 1067 Completing the Application ................................................................................................................ 1070 Reflecting on What We Have Done ..................................................................................................... 1071 Building a Sequence Workflow (in a Dedicated DLL)...................................................1073 Defining the Initial Project .................................................................................................................. 1073 Importing Assemblies and Namespaces ............................................................................................ 1074 Defining the Workflow Arguments...................................................................................................... 1075 Defining Workflow Variables .............................................................................................................. 1076 Working with the Assign Activity........................................................................................................ 1077 Working with the If and Switch Activities........................................................................................... 1078 Building a Custom Code Activity......................................................................................................... 1081 Consuming the Workflow Library ................................................................................1084 Retrieving the Workflow Output Argument......................................................................................... 1085 Summary .....................................................................................................................1086  CONTENTS xli  Part VII: Windows Presentation Foundation .....................................................1089  Chapter 27: Introducing Windows Presentation Foundation and XAML ...........1091 The Motivation Behind WPF.........................................................................................1091 Unifying Diverse APIs.......................................................................................................................... 1092 Providing a Separation of Concerns via XAML.................................................................................... 1093 Providing an Optimized Rendering Model .......................................................................................... 1093 Simplifying Complex UI Programming................................................................................................ 1094 The Various Flavors of WPF .........................................................................................1094 Traditional Desktop Applications........................................................................................................ 1094 Navigation-Based WPF Applications................................................................................................... 1096 XBAP Applications .............................................................................................................................. 1097 The WPF/Silverlight Relationship........................................................................................................ 1099 Investigating the WPF Assemblies...............................................................................1099 The Role of the Application Class ....................................................................................................... 1101 The Role of the Window Class ............................................................................................................ 1103 Building a WPF Application Without XAML ..................................................................1107 Creating a Strongly Typed Window .................................................................................................... 1109 Creating a Simple User Interface........................................................................................................ 1110 Interacting with Application-Level Data ............................................................................................. 1112 Handling the Closing of a Window Object........................................................................................... 1113 Intercepting Mouse Events................................................................................................................. 1114 Intercepting Keyboard Events............................................................................................................. 1115 Building a WPF Application Using Only XAML..............................................................1117 Defining a Window Object in XAML .................................................................................................... 1118 Defining the Application Object in XAML ............................................................................................ 1119 Processing the XAML Files Using msbuild.exe................................................................................... 1120 Transforming Markup into a .NET Assembly ...............................................................1122 Mapping the Window XAML Markup to C# Code ................................................................................ 1122 4 CONTENTS xlii The Role of BAML ............................................................................................................................... 1124 Mapping the Application XAML Markup to C# Code ........................................................................... 1125 XAML-to-Assembly Process Summary............................................................................................... 1126 Understanding the Syntax of WPF XAML .....................................................................1127 Introducing Kaxaml............................................................................................................................. 1127 XAML XML Namespaces and XAML “Keywords”................................................................................ 1128 Controlling Class and Member Variable Visibility ............................................................................... 1131 XAML Elements, XAML Attributes, and Type Converters .................................................................... 1131 Understanding XAML Property-Element Syntax ................................................................................. 1133 Understanding XAML Attached Properties.......................................................................................... 1133 Understanding XAML Markup Extensions........................................................................................... 1134 Building a WPF Application Using Code-Behind Files..................................................1136 Adding a Code File for the MainWindow Class................................................................................... 1136 Adding a Code File for the MyApp Class............................................................................................. 1137 Processing the Code Files with msbuild.exe...................................................................................... 1138 Building WPF Applications Using Visual Studio ...........................................................1139 The WPF Project Templates................................................................................................................ 1139 The Toolbox and XAML Designer/Editor ............................................................................................. 1141 Setting Properties Using the Properties Window................................................................................ 1143 Handling Events Using the Properties Window................................................................................... 1145 Handling Events in the XAML Editor ................................................................................................... 1146 The Document Outline Window .......................................................................................................... 1146 Viewing the AutoGenerated Code Files............................................................................................... 1147 Building a Custom XAML Editor with Visual Studio .....................................................1148 Designing the GUI of Our Window....................................................................................................... 1148 Implementing the Loaded Event......................................................................................................... 1150 Implementing the Button’s Click Event............................................................................................... 1151 Implementing the Closed Event.......................................................................................................... 1153  CONTENTS xliii Testing Your Application..................................................................................................................... 1153 Exploring the WPF Documentation ..................................................................................................... 1154 Summary .....................................................................................................................1155  Chapter 28: Programming with WPF Controls ..................................................1157 A Survey of the Core WPF Controls..............................................................................1157 The WPF Ink Controls.......................................................................................................................... 1158 The WPF Document Controls .............................................................................................................. 1158 WPF Common Dialog Boxes................................................................................................................ 1159 The Details Are in the Documentation ................................................................................................ 1159 A Brief Review of the Visual Studio WPF Designer ......................................................1160 Working with WPF Controls Using Visual Studio ................................................................................ 1161 Working with the Document Outline Editor ........................................................................................ 1162 Controlling Content Layout Using Panels.....................................................................1163 Positioning Content Within Canvas Panels ......................................................................................... 1166 Positioning Content Within WrapPanel Panels.................................................................................... 1167 Positioning Content Within StackPanel Panels................................................................................... 1169 Positioning Content Within Grid Panels .............................................................................................. 1170 Positioning Content Within DockPanel Panels.................................................................................... 1173 Enabling Scrolling for Panel Types..................................................................................................... 1174 Configuring Panels Using the Visual Studio Designers....................................................................... 1175 Building a Window’s Frame Using Nested Panels .......................................................1178 Building the Menu System.................................................................................................................. 1179 Building the ToolBar ........................................................................................................................... 1183 Building the StatusBar........................................................................................................................ 1183 Finalizing the UI Design...................................................................................................................... 1183 Implementing the MouseEnter/MouseLeave Event Handlers ............................................................. 1184 Implementing the Spell Checking Logic ............................................................................................. 1185 Understanding WPF Commands ..................................................................................1186  CONTENTS xliv The Intrinsic Command Objects.......................................................................................................... 1186 Connecting Commands to the Command Property............................................................................. 1187 Connecting Commands to Arbitrary Actions....................................................................................... 1188 Working with the Open and Save Commands .................................................................................... 1190 A Deeper Look at WPF APIs and Controls ....................................................................1192 Working with the TabControl.............................................................................................................. 1192 Building the Ink API Tab...............................................................................................1195 Designing the ToolBar ........................................................................................................................ 1196 The RadioButton Control..................................................................................................................... 1199 Handling Events for the Ink API Tab ................................................................................................... 1201 The InkCanvas Control........................................................................................................................ 1202 The ComboBox Control ....................................................................................................................... 1205 Saving, Loading, and Clearing InkCanvas Data .................................................................................. 1206 Introducing the Documents API ...................................................................................1207 Block Elements and Inline Elements .................................................................................................. 1208 Document Layout Managers............................................................................................................... 1208 Building the Documents Tab........................................................................................1209 Populating a FlowDocument Using Code............................................................................................ 1210 Enabling Annotations and Sticky Notes.............................................................................................. 1211 Saving and Loading a Flow Document ............................................................................................... 1213 Introducing the WPF Data-Binding Model....................................................................1214 Building the Data Binding Tab ............................................................................................................ 1214 Establishing Data Bindings Using Visual Studio ................................................................................. 1215 The DataContext Property................................................................................................................... 1217 Data Conversion Using IValueConverter ............................................................................................. 1218 Establishing Data Bindings in Code.................................................................................................... 1219 Building the DataGrid Tab................................................................................................................... 1219 Summary .....................................................................................................................1221  CONTENTS xlv  Chapter 29: WPF Graphics Rendering Services ................................................1223 Understanding WPF’s Graphical Rendering Services ..................................................1223 WPF Graphical Rendering Options...................................................................................................... 1224 Rendering Graphical Data Using Shapes .....................................................................1225 Adding Rectangles, Ellipses, and Lines to a Canvas .......................................................................... 1227 Removing Rectangles, Ellipses, and Lines from a Canvas ................................................................. 1230 Working with Polylines and Polygons................................................................................................. 1231 Working with Paths ............................................................................................................................ 1232 WPF Brushes and Pens................................................................................................1236 Configuring Brushes Using Visual Studio ........................................................................................... 1236 Configuring Brushes in Code .............................................................................................................. 1239 Configuring Pens ................................................................................................................................ 1240 Applying Graphical Transformations............................................................................1241 A First Look at Transformations ......................................................................................................... 1242 Transforming Our Canvas Data........................................................................................................... 1243 Working with the Visual Studio Transform Editor........................................................1245 Building the Initial Layout................................................................................................................... 1245 Applying Transformations at Design Time.......................................................................................... 1247 Transforming the Canvas in Code....................................................................................................... 1249 Rendering Graphical Data Using Drawings and Geometries........................................1250 Building a DrawingBrush Using Geometries....................................................................................... 1251 Painting with the DrawingBrush......................................................................................................... 1252 Containing Drawing Types in a DrawingImage................................................................................... 1253 The Role of Expression Design ....................................................................................1254 Exporting a Sample Design File As XAML........................................................................................... 1254 Importing the Graphical Data into a WPF Project ............................................................................... 1256 Interacting with the Bear.................................................................................................................... 1258  CONTENTS xlvi Rendering Graphical Data Using the Visual Layer .......................................................1258 Summary .....................................................................................................................1265  Chapter 30: WPF Resources, Animations, and Styles.......................................1267 Understanding the WPF Resource System ..................................................................1267 Working with Binary Resources ......................................................................................................... 1267 Working with Object (Logical) Resources....................................................................1273 The Role of the Resources Property ................................................................................................... 1274 Defining Window-Wide Resources ..................................................................................................... 1274 The {StaticResource} Markup Extension ......................................................................................... 1277 The {DynamicResource} Markup Extension........................................................................................ 1277 Application-Level Resources .............................................................................................................. 1278 Defining Merged Resource Dictionaries ............................................................................................. 1280 Defining a Resource-Only Assembly .................................................................................................. 1281 Understanding WPF’s Animation Services...................................................................1284 The Role of the Animation Class Types .............................................................................................. 1284 The To, From, and By Properties ........................................................................................................ 1285 The Role of the Timeline Base Class .................................................................................................. 1285 Authoring an Animation in C# Code.................................................................................................... 1286 Controlling the Pace of an Animation ................................................................................................. 1288 Reversing and Looping an Animation................................................................................................. 1288 Authoring Animations in XAML ....................................................................................1289 The Role of Storyboards ..................................................................................................................... 1290 The Role of Event Triggers ................................................................................................................. 1290 Animation Using Discrete Key Frames ............................................................................................... 1291 Understanding the Role of WPF Styles ........................................................................1292 Defining and Applying a Style............................................................................................................. 1293 Overriding Style Settings.................................................................................................................... 1294 Automatically Applying a Style with TargetType ................................................................................ 1294  CONTENTS xlvii Subclassing Existing Styles................................................................................................................ 1295 The Role of Unnamed Styles............................................................................................................... 1295 Defining Styles with Triggers ............................................................................................................. 1296 Defining Styles with Multiple Triggers................................................................................................ 1297 Animated Styles.................................................................................................................................. 1297 Assigning Styles Programmatically .................................................................................................... 1298 Summary .....................................................................................................................1300  Chapter 31: Dependency Properties, Routed Events, and Templates ..............1301 Understanding the Role of Dependency Properties.....................................................1301 Examining an Existing Dependency Property ..................................................................................... 1303 Important Notes Regarding CLR Property Wrappers .......................................................................... 1305 Building a Custom Dependency Property ....................................................................1306 Adding a Data Validation Routine ....................................................................................................... 1311 Responding to the Property Change................................................................................................... 1312 Understanding Routed Events .....................................................................................1313 The Role of Routed Bubbling Events................................................................................................... 1314 Continuing or Halting Bubbling........................................................................................................... 1315 The Role of Routed Tunneling Events................................................................................................. 1315 Logical Trees, Visual Trees, and Default Templates....................................................1317 Programmatically Inspecting a Logical Tree ...................................................................................... 1318 Programmatically Inspecting a Visual Tree ........................................................................................ 1319 Programmatically Inspecting a Control’s Default Template ............................................................... 1321 Building a Control Template with the Trigger Framework...........................................1325 Templates As Resources .................................................................................................................... 1326 Incorporating Visual Cues Using Triggers........................................................................................... 1329 The Role of the {TemplateBinding} Markup Extension ....................................................................... 1329 The Role of ContentPresenter............................................................................................................. 1331 Incorporating Templates into Styles................................................................................................... 1332  CONTENTS xlviii Summary .....................................................................................................................1333  Part VIII: ASP.NET Web Forms ..........................................................................1335  Chapter 32: Introducing ASP.NET Web Forms...................................................1337 The Role of HTTP .........................................................................................................1337 The HTTP Request/Response Cycle.................................................................................................... 1337 HTTP Is a Stateless Protocol............................................................................................................... 1338 Understanding Web Applications and Web Servers.....................................................1338 The Role of IIS Virtual Directories....................................................................................................... 1339 The ASP.NET Development Web Server.............................................................................................. 1339 The Role of HTML.........................................................................................................1340 HTML Document Structure ................................................................................................................. 1340 The Role of an HTML Form ................................................................................................................. 1341 The Visual Studio HTML Designer Tools ............................................................................................. 1342 Building an HTML Form ...................................................................................................................... 1344 The Role of Client-Side Scripting.................................................................................1346 A Client-Side Scripting Example......................................................................................................... 1347 Posting Back to the Web Server ..................................................................................1348 Postbacks Under ASP.NET.................................................................................................................. 1349 An Overview of the ASP.NET API..................................................................................1349 Major Features of ASP.NET 2.0 and Higher ........................................................................................ 1350 Major Features of ASP.NET 3.5 (and .NET 3.5 SP1) and Higher.......................................................... 1351 Major Features of ASP.NET 4.0 and 4.5.............................................................................................. 1352 Building a Single-File ASP.NET Web Page...................................................................1352 Referencing AutoLotDAL.dll................................................................................................................ 1354 Designing the UI ................................................................................................................................. 1354 Adding the Data Access Logic ............................................................................................................ 1355 The Role of ASP.NET Directives.......................................................................................................... 1358  CONTENTS xlix Analyzing the “Script” Block .............................................................................................................. 1359 Analyzing the ASP.NET Control Declarations...................................................................................... 1360 Building an ASP.NET Web Page Using Code Files........................................................1361 Referencing the AutoLotDAL.dll Assembly ......................................................................................... 1364 Updating the Code File ....................................................................................................................... 1365 Debugging and Tracing ASP.NET Pages............................................................................................. 1365 ASP.NET Web Sites vs. ASP.NET Web Applications .....................................................1367 The ASP.NET Web Site Directory Structure .................................................................1369 Referencing Assemblies..................................................................................................................... 1369 The Role of the App_Code Folder ....................................................................................................... 1370 The Inheritance Chain of the Page Type ......................................................................1371 Interacting with the Incoming HTTP Request ..............................................................1372 Obtaining Browser Statistics .............................................................................................................. 1374 Access to Incoming Form Data........................................................................................................... 1374 The IsPostBack Property..................................................................................................................... 1375 Interacting with the Outgoing HTTP Response ............................................................1375 Emitting HTML Content....................................................................................................................... 1376 Redirecting Users ............................................................................................................................... 1377 The Life Cycle of an ASP.NET Web Page......................................................................1378 The Role of the AutoEventWireup Attribute ........................................................................................ 1379 The Error Event................................................................................................................................... 1380 The Role of the web.config File ...................................................................................1381 The ASP.NET Web Site Administration Utility ..................................................................................... 1382 Summary .....................................................................................................................1382  Chapter 33: ASP.NET Web Controls, Master Pages, and Themes......................1383 Understanding the Nature of Web Controls .................................................................1383 Understanding Server-Side Event Handling ....................................................................................... 1384  CONTENTS l The AutoPostBack Property................................................................................................................ 1384 The Control and WebControl Base Classes..................................................................1385 Enumerating Contained Controls........................................................................................................ 1386 Dynamically Adding and Removing Controls...................................................................................... 1389 Interacting with Dynamically Created Controls .................................................................................. 1390 Functionality of the WebControl Base Class....................................................................................... 1391 Major Categories of ASP.NET Web Controls ................................................................1392 A Brief Word Regarding System.Web.UI.HtmlControls ....................................................................... 1395 Web Control Documentation............................................................................................................... 1396 Building the ASP.NET Cars Web Site ...........................................................................1396 Working with ASP.NET Master Pages................................................................................................. 1397 Defining the Default Content Page ..................................................................................................... 1404 Designing the Inventory Content Page ............................................................................................... 1407 Designing the Build-a-Car Content Page............................................................................................ 1412 The Role of the Validation Controls..............................................................................1415 Enabling Client-Side JavaScript Validation Support........................................................................... 1417 The RequiredFieldValidator ................................................................................................................ 1417 The RegularExpressionValidator......................................................................................................... 1418 The RangeValidator ............................................................................................................................ 1418 The CompareValidator ........................................................................................................................ 1418 Creating Validation Summaries .......................................................................................................... 1419 Defining Validation Groups ................................................................................................................. 1421 Working with Themes..................................................................................................1422 Understanding *.skin Files.................................................................................................................. 1423 Applying Site-Wide Themes ............................................................................................................... 1426 Applying Themes at the Page Level.................................................................................................... 1426 The SkinID Property............................................................................................................................ 1426 Assigning Themes Programmatically ................................................................................................. 1427  CONTENTS li Summary .....................................................................................................................1428  Chapter 34: ASP.NET State Management Techniques.......................................1429 The Issue of State........................................................................................................1429 ASP.NET State Management Techniques ....................................................................1431 Understanding the Role of ASP.NET View State ..........................................................1432 Demonstrating View State.................................................................................................................. 1432 Adding Custom View State Data......................................................................................................... 1434 The Role of the Global.asax File...................................................................................1434 The Global Last-Chance Exception Event Handler.............................................................................. 1436 The HttpApplication Base Class.......................................................................................................... 1437 Understanding the Application/Session Distinction.....................................................1438 Maintaining Application-Level State Data .......................................................................................... 1438 Modifying Application Data................................................................................................................. 1440 Handling Web Application Shutdown ................................................................................................. 1442 Working with the Application Cache............................................................................1442 Fun with Data Caching ....................................................................................................................... 1443 Modifying the *.aspx File.................................................................................................................... 1445 Maintaining Session Data............................................................................................1448 Additional Members of HttpSessionState........................................................................................... 1450 Understanding Cookies................................................................................................1451 Creating Cookies................................................................................................................................. 1452 Reading Incoming Cookie Data........................................................................................................... 1453 The Role of the Element ....................................................................1454 Storing Session Data in the ASP.NET Session State Server ............................................................... 1454 Storing Session Data in a Dedicated Database .................................................................................. 1455 Introducing the ASP.NET Profile API ............................................................................1456 The ASPNETDB.mdf Database ............................................................................................................ 1456  CONTENTS lii Defining a User Profile Within web.config .......................................................................................... 1457 Accessing Profile Data Programmatically........................................................................................... 1458 Grouping Profile Data and Persisting Custom Objects........................................................................ 1460 Summary .....................................................................................................................1462  Index.................................................................................................................1463 liii About the Author  Andrew Troelsen fondly recalls his very first computer, an Atari 400 complete with a tape deck storage device and a black-and-white TV serving as a monitor (which his parents permitted him to have in his bedroom—thanks guys!). Andrew is employed with Intertech (www.intertech.com), a .NET/Java training and consulting center located in Minneapolis, Minnesota. He has authored a number of books, including Developer’s Workshop to COM and ATL 3.0 (Wordware Publishing, 2000), COM and .NET Interoperability (Apress, 2002), and Visual Basic 2008 and the .NET 3.5 Platform: An Advanced Guide (Apress, 2008). liv About the Technical Reviewer  Andy Olsen runs a software training company based in the UK, delivering training in .NET, Java, web, and mobile technologies in Europe, the U.S., and Asia. Andy has been working with .NET since the days of the first beta and is actively involved with the latest features in the .NET 4.5 platform. Andy lives by the sea in Swansea with his wife Jayne and their children, Emily and Tom. Andy enjoys running along the sea front (with copious coffee stops along the way), skiing, and following the Swans! Andy can be reached at andyo@olsensoft.comlv Acknowledgments One might think that it would be easier to update a book, rather than create a new book from the ground up. In my experience, I feel it is the exact opposite. While I am the individual responsible for the overall content, this book would never have been published if a number of people had not worked tirelessly beside me. Huge thanks are in order to my technical editor, Andy Olsen. As always, Andy made many excellent suggestions (I only wish I had the time to incorporate everything—maybe in the next edition!). To the folks at Apress, you have continued to show me why I am grateful to be working with you. Thanks to all for taking my raw manuscript and transforming it into a professional, high-quality, published text. lvi Introduction Many moons ago (circa 2001), I was given the opportunity to write a book on a forthcoming Microsoft technology that was, at the time, dubbed NGWS (Next Generation Windows Software). As I began to examine the source code provided by Microsoft, I noticed numerous code comments referring to the “COOL” (Common Object Oriented Language) programming language. While I worked on my first initial manuscript of C# and the .NET Platform using a pre-alpha build (and no documentation to speak of), NGWS was eventually rebranded as the Microsoft .NET platform. COOL, as you might guess, is what we now know today as C#. The first edition of this book was released in step with .NET 1.0, beta 2. Since then, I have updated the text to account for the numerous updates to the C# programming language, as well as the explosion of new APIs introduced with each new release of the .NET platform. Over the years, this book has (thankfully and gratefully) been very well received, by the press (a JOLT award finalist and ReferenceWare programming book of the year), readers, and various university programs in computer science and software engineering. It has been just wonderful to communicate with readers and educators around the globe. Thank you for all of your suggestions, comments, and (yes) criticism. I might not be able to respond to every e-mail, but everything is taken under consideration, to be sure. We’re a Team, You and I Technology authors write for a demanding group of people (I should know—I’m one of them). You know that building software solutions using any platform or language is extremely complicated and is very specific to your department, company, client base, and subject matter. Perhaps you work in the electronic publishing industry, develop systems for the state or local government, or work at NASA or a branch of the military. Speaking for myself, I have developed children’s educational software (Oregon Trail/Amazon Trail), various n-tier systems, and projects within the medical and financial industries. The chances are almost 100 percent that the code you write at your place of employment has little to do with the code I write at mine (unless we happened to work together previously!). Therefore, in this book, I have deliberately chosen to avoid creating demonstrations that tie the example code to a specific industry or vein of programming. Given this, I explain C#, OOP, the CLR, and the .NET base class libraries using industry-agnostic examples. Rather than having every blessed example fill a grid with data, calculate payroll, or whatnot, I’ll stick to subject matter we can all relate to: automobiles (with some geometric structures and employee payroll systems thrown in for good measure). And that’s where you come in. My job is to explain the C# programming language and the core aspects of the .NET platform the best I possibly can. As well, I will do everything I can to equip you with the tools and strategies you need to continue your studies at this book’s conclusion.  INTRODUCTION lvii Your job is to take this information and apply it to your specific programming assignments. I obviously understand that your projects most likely don’t revolve around automobiles with friendly pet names (Zippy the BMW, or a Yugo named Clunker, among others), but that’s what applied knowledge is all about! Rest assured, once you understand the topics and concepts presented within this text, you will be in a perfect position to build .NET solutions that map to your own unique programming environment. An Overview of This Book Pro C# 5.0 and the .NET 4.5 Framework, Sixth Edition, is logically divided into eight distinct parts, each of which contains a number of related chapters. Here is a part-by-part and chapter-by-chapter breakdown of the text. Part I: Introducing C# and the .NET Platform The purpose of Part 1 is to acclimate you to the nature of the .NET platform and various development tools (many of which are open source) used during the construction of .NET applications. Chapter 1: The Philosophy of .NET This first chapter functions as the backbone for the remainder of the text. The primary goal of this chapter is to acquaint you with a number of .NET-centric building blocks, such as the Common Language Runtime (CLR), Common Type System (CTS), Common Language Specification (CLS), and base class libraries. Here, you will take an initial look at the C# programming language and the .NET assembly format. As well, you will learn the role of the .NET platform within the Windows 8 operating system, and understand the distinction between a Windows 8 app and a .NET application. Chapter 2: Building C# Applications The goal of this chapter is to introduce you to the process of compiling C# source code files using various tools and techniques. You will begin by learning how to use the command-line compiler (csc.exe) and C# response files. Over the remainder of the chapter, you will examine numerous code editors and integrated development environments (IDEs), including Notepad++, SharpDevelop, Visual C# Express, and Visual Studio. You will also learn how to configure your development machine with a local installation of the all-important .NET Framework 4.5 SDK documentation. Part II: Core C# Programming The topics presented in this part of the book are quite important because you will use them regardless of which type of .NET software you intend to develop (e.g., web applications, desktop GUI applications, code libraries, or Windows services). Here, you will learn about the fundamental data types of .NET, work with text manipulation, and learn the role of various C# parameter modifiers (including optional and named arguments).  INTRODUCTION lviii Chapter 3: Core C# Programming Constructs, Part I This chapter begins your formal investigation of the C# programming language. Here, you will learn about the role of the Main() method and numerous details regarding the intrinsic data types of the .NET platform, including the manipulation of textual data using System.String and System.Text.StringBuilder. You will also examine iteration and decision constructs, narrowing and widening operations, and the unchecked keyword. Chapter 4: Core C# Programming Constructs, Part II This chapter completes your examination of the core aspects of C#, beginning with the construction of overloaded type methods and defining parameters using the out, ref, and params keywords. This chapter will examine two C# features called arguments and optional parameters. You will also learn how to create and manipulate arrays of data, define nullable data types (with the ? and ?? operators), and understand the distinction between value types (including enumerations and custom structures) and reference types. Part III: Object-Oriented Programming with C# In this section you will come to understand the core constructs of the C# language, including the details of object-oriented programming (OOP). This part will also examine how to process runtime exceptions, and dive into the details of working with strongly typed interfaces. Chapter 5: Understanding Encapsulation This chapter begins your examination of object-oriented programming (OOP) using the C# programming language. After you are introduced to the pillars of OOP (encapsulation, inheritance, and polymorphism), the remainder of this chapter will show you how to build robust class types using constructors, properties, static members, constants, and read-only fields. You will wrap up with an examination of partial type definitions, object initialization syntax, and automatic properties. Chapter 6: Understanding Inheritance and Polymorphism Here, you will examine the remaining pillars of OOP (inheritance and polymorphism), which allow you to build families of related class types. As you do this, you will examine the role of virtual methods, abstract methods (and abstract base classes), and the nature of the polymorphic interface. Last but not least, this chapter will explain the role of the supreme base class of the .NET platform, System.Object. Chapter 7: Understanding Structured Exception Handling The point of this chapter is to discuss how to handle runtime anomalies in your code base through the use of structured exception handling. Not only will you learn about the C# keywords that allow you to handle such problems (try, catch, throw, and finally), but you will also come to understand the  INTRODUCTION lix distinction between application-level and system-level exceptions. In addition, this chapter will examine various tools within Visual Studio that allow you to debug the exceptions that escape your notice. Chapter 8: Working with Interfaces The material in this chapter builds upon your understanding of object-based development by covering the topic of interface-based programming. Here, you will learn how to define classes and structures that support multiple behaviors, how to discover these behaviors at runtime, and how to selectively hide particular behaviors using explicit interface implementation. In addition to creating a number of custom interfaces, you will also learn how to implement standard interfaces found within the .NET platform. You will use these to build objects that can be sorted, copied, enumerated, and compared. Part IV: Advanced C# Programming This section of the book will deepen your understanding of the C# language by walking you through a number of more advanced (but very important) concepts. Here, you will complete your examination of the .NET type system by examining interfaces and delegates. You will also learn about the role of generics, take a first look at Language Integrated Query (LINQ), and examine a number of more advanced features of C# (e.g., extension methods, partial methods, and pointer manipulation). Chapter 9: Collections and Generics This chapter explores the topic of generics. As you will see, generic programming gives you a way to create types and type members, which contain various placeholders that can be specified by the caller. In a nutshell, generics greatly enhance application performance and type safety. Not only will you explore various generic types within the System.Collections.Generic namespace, but you will also learn how to build your own generic methods and types (with and without constraints). Chapter 10: Delegates, Events, and Lambda Expressions The purpose of Chapter 10 is to demystify the delegate type. Simply put, a .NET delegate is an object that points to other methods in your application. Using this type, you can build systems that allow multiple objects to engage in a two-way conversation. After you have examined the use of .NET delegates, you will then be introduced to the C# event keyword, which you can use to simplify the manipulation of raw delegate programming. You will wrap up this chapter by investigating the role of the C# lambda operator (=>) and exploring the connection between delegates, anonymous methods, and lambda expressions. Chapter 11: Advanced C# Language Features This chapter deepens your understanding of the C# programming language by introducing you to a number of advanced programming techniques. Here, you will learn how to overload operators and create custom conversion routines (both implicit and explicit) for your types. You will also learn how to build and interact with type indexers, as well as work with extension methods, anonymous types, partial methods, and C# pointers using an unsafe code context.  INTRODUCTION lx Chapter 12: LINQ to Objects This chapter will begin your examination of Language Integrated Query (LINQ). LINQ allows you to build strongly typed query expressions that can be applied to a number of LINQ targets to manipulate data in the broadest sense of the word. Here, you will learn about LINQ to Objects, which allows you to apply LINQ expressions to containers of data (e.g., arrays, collections, and custom types). This information will serve you well as you encounter a number of additional LINQ APIs throughout the remainder of this book (e.g., LINQ to XML, LINQ to DataSet, PLINQ, and LINQ to Entities). Chapter 13: Understanding Object Lifetime The final chapter of this section examines how the CLR manages memory using the .NET garbage collector. Here you will come to understand the role of application roots, object generations, and the System.GC type. Once you understand the basics, you will examine the topics of disposable objects (using the IDisposable interface) and the finalization process (using the System.Object.Finalize() method). This chapter will also investigate the Lazy class, which allows you to define data that will not be allocated until requested by a caller. As you will see, this feature can be very helpful when you want to ensure you do not clutter the heap with objects that are not actually required by your programs. Part V: Programming with .NET Assemblies Part 5 dives into the details of the .NET assembly format. Not only will you learn how to deploy and configure .NET code libraries, but you will also come to understand the internal composition of a .NET binary image. This part also explains the role of .NET attributes and the role of resolving type information at runtime. This section will also explain the role of the Dynamic Language Runtime (DLR) and the C# dynamic keyword. Later chapters will examine some fairly advanced topics regarding assemblies, such as application domains, the syntax of CIL, and the construction of in-memory assemblies. Chapter 14: Building and Configuring Class Libraries At a very high level, assembly is the term used to describe a *.dll or *.exe binary file created with a .NET compiler. However, the true story of .NET assemblies is far richer than that. Here, you will learn the distinction between single-file and multifile assemblies, as well as how to build and deploy each entity. You’ll also examine how you can configure private and shared assemblies using XML-based *.config files and publisher policy assemblies. Along the way, you will investigate the internal structure of the global assembly cache (GAC). Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming Chapter 15 continues your examination of .NET assemblies by checking out the process of runtime type discovery using the System.Reflection namespace. Using the types of this namespace, you can build applications that can read an assembly’s metadata on the fly. You will also learn how to load and create types at runtime dynamically using late binding. The final topic of this chapter will explore the role of  INTRODUCTION lxi .NET attributes (both standard and custom). To illustrate the usefulness of each of these topics, the chapter shows you how to construct an extendable Windows Forms application. Chapter 16: Dynamic Types and the Dynamic Language Runtime .NET 4.0 introduces a new aspect of the .NET runtime environment called the dynamic language runtime. Using the DLR and the C# 2010 dynamic keyword, you can define data that is not truly resolved until runtime. Using these features simplifies some very complex .NET programming tasks dramatically. In this chapter, you will learn some practical uses of dynamic data, including how to leverage the .NET reflection APIs in a streamlined manner, as well as how to communicate with legacy COM libraries with a minimum of fuss and bother. Chapter 17: Processes, AppDomains, and Object Contexts Now that you have a solid understanding of assemblies, this chapter dives deeper into the composition of a loaded .NET executable. The goal of this chapter is to illustrate the relationship between processes, application domains, and contextual boundaries. These topics provide the proper foundation for Chapter 19, where you will examine the construction of multithreaded applications. Chapter 18: Understanding CIL and the Role of Dynamic Assemblies The goal of the final chapter in this section is twofold. In the first half (more or less), you will examine the syntax and semantics of CIL in much greater detail than in previous chapters. The remainder of this chapter will cover the role of the System.Reflection.Emit namespace. You can use these types to build software that can generate .NET assemblies in memory at runtime. Formally speaking, assemblies defined and executed in memory are termed dynamic assemblies. Part VI: Introducing the .NET Base Class Libraries By this point in the text, you have a solid handle on the C# language and the details of the .NET assembly format. Part 6 leverages your newfound knowledge by exploring a number of commonly used services found within the base class libraries, including the creation of multithreaded applications, file I/O, and database access using ADO.NET. This part also covers the construction of distributed applications using Windows Communication Foundation (WCF), workflow-enabled applications that use the Windows Workflow Foundation (WF) API, and the LINQ to XML API. Chapter 19: Multithreaded, Parallel, and Async Programming This chapter examines how to build multithreaded applications and illustrates a number of techniques you can use to author thread-safe code. The chapter opens by revisiting the .NET delegate type to ensure, explaining a delegate’s intrinsic support for asynchronous method invocations. Next, you will investigate the types within the System.Threading namespace. The remainder of this chapter covers the Task Parallel Library (TPL). Using the TPL, .NET developers can build applications that distribute their workload across all available CPUs in a wickedly simple manner. At this point, you will also learn about  INTRODUCTION lxii the role of Parallel LINQ (PINQ), which provides a way to create LINQ queries that scale across multiple machine cores. We wrap up by examining some new C# keywords introduced in .NET 4.5, which integrate asynchronous method calls directly into the language. Chapter 20: File I/O and Object Serialization The System.IO namespace allows you to interact with a machine’s file and directory structure. Over the course of this chapter, you will learn how to create (and destroy) a directory system programmatically. You will also learn how to move data into and out of various streams (e.g., file based, string based, and memory based). The latter part of this chapter will examine the object serialization services of the .NET platform. Simply put, serialization allows you to persist the state of an object (or a set of related objects) into a stream for later use. Deserialization (as you might expect) is the process of plucking an object from the stream into memory for consumption by your application. After you understand the basics, you will learn how to customize the serialization process using the ISerializable interface and a set of .NET attributes. Chapter 21: ADO.NET Part I: The Connected Layer In this first of three database-centric chapters, you will take your first look at the database access API of the .NET platform, ADO.NET. Specifically, this chapter will introduce you to the role of .NET data providers and how to communicate with a relational database using the connected layer of ADO.NET, which is represented by connection objects, command objects, transaction objects, and data reader objects. Be aware that this chapter will also walk you through the creation of a custom database and the first iteration of a custom data access library (AutoLotDAL.dll); you will use this library throughout the remainder of this book. Chapter 22: ADO.NET Part II: The Disconnected Layer This chapter continues your study of database manipulation by examining the disconnected layer of ADO.NET. Here, you will learn the role of the DataSet type and data adapter objects. You will also learn about the many tools of Visual Studio 2010 that can greatly simplify the creation of data-driven applications. Along the way, you will learn how to bind DataTable objects to user interface elements, as well as how to apply LINQ queries to in-memory DataSet objects using LINQ to DataSet. Chapter 23: ADO.NET Part III: The Entity Framework This chapter wraps up your investigation of ADO.NET by examining the role of the Entity Framework (EF). Essentially, EF is a way for you to author data-access code using strongly typed classes that directly map to your business model. Here, you will come to understand the role of EF Object Services, the Entity Client and Object Context, and the composition of an *.edmx file. While doing so, you will learn to interact with relational databases using LINQ to Entities. You will also build the final version of your custom data-access library (AutoLotDAL.dll), which you will use in several of the remaining chapters of the book.  INTRODUCTION lxiii Chapter 24: Introducing LINQ to XML Chapter 14 introduced you to the core LINQ programming model—specifically LINQ to Objects. Here, you will deepen your understanding of Language Integrated Query by examining how to apply LINQ queries to XML documents. You will begin by learning about the “warts” that were present in .NET’s initial foray into XML manipulation as you use the types of the System.Xml.dll assembly. With this brief history lesson behind you, you will explore how to create XML documents in memory, how to persist them to the hard drive, and how to navigate their contents using the LINQ programming model (LINQ to XML). Chapter 25: Introducing Windows Communication Foundation Until this point in the book, all of the sample applications have executed on a single computer. In this chapter, you will learn about the Windows Communication Foundation (WCF) API that allows you to build distributed applications in a symmetrical manner, regardless of their underlying plumbing. This chapter will expose you to the construction of WCF services, hosts, and clients. As you will see, WCF services are extremely flexible because they allow clients and hosts to leverage XML-based configuration files to specify addresses, bindings, and contracts declaratively. Chapter 26: Introducing Windows Workflow Foundation In this chapter, you will begin by learning about the role of a workflow-enabled application, and you will come to understand the various ways to model business processes using the .NET 4.0 WF API. Next, you will examine the scope of the WF activity library, as well as learn how to build custom activities that will use the custom database access library you created earlier in the book. Part VII: Windows Presentation Foundation .NET 3.0 introduced programmers to an amazing API called Windows Presentation Foundation (WFP). This API has quickly become the heir apparent to the Windows Forms desktop programming model. In essence, WPF allows you to build desktop applications that incorporate vector graphics, interactive animations, and data-binding operations using a declarative markup grammar called XAML. Furthermore, the WPF control architecture provides a trivial way to restyle the look-and-feel of a typical control radically using little more than some well-formed XAML. Chapter 27: Introducing Windows Presentation Foundation and XAML Essentially, WPF allows you to build extremely interactive and media-rich front ends for desktop applications (and indirectly, web applications). Unlike Windows Forms, this supercharged UI framework integrates a number of key services (e.g., 2D and 3D graphics, animations, and rich documents) into a single, unified object model. In this chapter, you will begin your examination of WPF and the Extendable Application Markup Language (XAML). Here, you will learn how to build WPF programs without XAML, as well as using nothing but XAML, and by using a combination of both approaches. You will wrap up the chapter by building a custom XAML editor that you will use for the remainder of the WPF-centric chapters.  INTRODUCTION lxiv Chapter 28: Programming with WPF Controls This chapter will expose you to the process of using intrinsic WPF controls and layout managers. For example, you will learn to build menu systems, splitter windows, toolbars, and status bars. This chapter will also introduce you to a number of WPF APIs (and their related controls), including the WPF Documents API, the WPF Ink API, and the data-binding model. Just as importantly, this chapter will begin your investigation of Expression Blend IDE, which simplifies the task of creating rich UIs for a WPF application. Chapter 29: WPF Graphics Rendering Services WPF is a graphically intensive API; given this fact, WPF provides three ways to render graphics: shapes, drawings and geometrics, and visuals. In this chapter, you will evaluate each option and learn about a number of important graphics primitives (e.g., brushes, pens, and transformations) along the way. This chapter will also examine a number of ways in which Expression Blend can help you simplify the process of creating WPF graphics, as well as how to perform hit-testing operations against graphical data. Chapter 30: WPF Resources, Animations, and Styles This chapter will introduce you to three important (and interrelated) topics that will deepen your understanding of the Windows Presentation Foundation API. The first order of business is to learn the role of logical resources. As you will see, the logical resource (also termed an object resource) system provides a way for you to name and refer to commonly used objects within a WPF application. Next, you will learn how to define, execute, and control an animation sequence. Despite what you might be thinking, however, WPF animations are not limited to the confines of video game or multimedia applications. You will wrap up the chapter by learning about the role of WPF styles. Similar to a web page that uses CSS or the ASP.NET theme engine, a WPF application can define a common look-and-feel for a set of controls. Chapter 31: Dependency Properties, Routed Events, and Templates This chapter begins by examining two topics that are important when creating a custom control: dependency properties and routed events. After you understand these topics, you will learn about the role of a default template, as well as how to view them programmatically at runtime. After this foundation has been laid, the remainder of this chapter will examine how to build custom templates. Part VIII: ASP.NET Web Forms Part 8 is devoted to an examination of constructing web applications using the ASP.NET programming API. Microsoft designed ASP.NET to model the creation of desktop user interfaces by layering an event- driven, object-oriented framework on top of a standard HTTP request/response.  INTRODUCTION lxv Chapter 32: Introducing ASP.NET Web Forms This chapter begins your study of web application development using ASP.NET. As you will see, server- side scripting code has now been replaced with real object-oriented languages (e.g., C# and VB .NET). This chapter will examine the construction of an ASP.NET web page, the underlying programming model, and other key aspects of ASP.NET, such as your choice of web server and the use of web.config files. Chapter 33: ASP.NET Web Controls, Master Pages, and Themes Whereas the previous chapter showed you how to construct ASP.NET Page objects, this chapter will examine the controls that populate the internal control tree. Here, you will examine the core ASP.NET web controls, including validation controls, the intrinsic site navigation controls, and various data- binding operations. This chapter will also illustrate the role of master pages and the ASP.NET theme engine, which is a server-side alternative to traditional style sheets. Chapter 34: ASP.NET State Management Techniques This chapter extends your understanding of ASP.NET by examining various ways to handle state management under .NET. Like classic ASP, ASP.NET allows you to create cookies and application-level and session-level variables quite easily. However, ASP.NET also introduces a new state management technique: the application cache. After you look at the numerous ways to handle state with ASP.NET, you will examine the role of the HttpApplication base class and learn how to alter the runtime behavior of your web application dynamically using the web.config file. Downloadable Appendixes As if 34 chapters were not enough, I have made two additional chapters available for download from the home page of this book at the Apress web site (www.apress.com). The first appendix covers the basics of the Windows Forms API, which is used for a few of the UI examples in this text. The second appendix examines the platform-independent nature of .NET via the Mono platform. Downloadable Appendix A: Programming with Windows Forms The original desktop GUI toolkit that shipped with the .NET platform is called Windows Forms. This appendix will walk you through the role of this UI framework and illustrate how to build main windows, dialog boxes, and menu systems. You will also learn about the role of form inheritance and see how to render 2D graphical data using the System.Drawing namespace. You will wrap things up by building a (semicapable) painting application that illustrates the various topics discussed throughout this appendix.  INTRODUCTION lxvi Downloadable Appendix B: Platform-Independent .NET Development with Mono Last but not least, Appendix B covers how to use an open source implementation of the .NET platform named Mono. You can use Mono to build feature-rich .NET applications that can be created, deployed, and executed upon a variety of operating systems, including Mac OS X, Solaris, and numerous Linux distributions. Given that Mono is largely comparable with Microsoft’s .NET platform, you already know most of what Mono has to offer. Therefore, this appendix will focus on the Mono installation process, Mono development tools, and Mono runtime engine. Obtaining This Book’s Source Code You can find all of the code examples contained in this book available as a free download from the Source Code/Download area of the Apress website. Simply navigate to www.apress.com, select the Source Code/Download link, and look up this title by name. Once you are on the home page for Pro C# 5 With this book, you can download a self-extracting *.zip file. After you unzip the contents, you will find that the code has been partitioned on a chapter-by-chapter basis. On a related note, be aware that you will find “Source Code” notes, such as the following, in many of the book’s chapters. These notes serve as your visual cue that you can load the example under discussion into Visual Studio for further examination and modification.  Source Code This is a source code note that refers you to a specific directory in the ZIP archive. To open a solution into Visual Studio, use the File  Open  Project/Solution menu option, and then navigate to the correct *.sln file within the correct subdirectory of the unzipped archive. Obtaining Updates for This Book As you read through this text, you might find an occasional grammatical or code error (although I sure hope not). If this is the case, please accept my apologies. Being human, I am sure that a glitch or two might be present, despite my best efforts. If this is the case, you can obtain the current errata list from the Apress web site at www.apress.com (again, this is located on the home page for this book), as well as information on how to notify me of any errors you might find. Contacting Me If you have any questions regarding this book’s source code, are in need of clarification for a given example, or simply would like to offer your thoughts regarding the .NET platform, feel free to drop me a line at the following e-mail address (to ensure your messages don’t end up in my junk mail folder, please include “C# SixthEd” in the Subject line somewhere):  INTRODUCTION lxvii atroelsen@intertech.com Please understand that I will do my best to get back to you in a timely fashion; however, like yourself, I get busy from time to time. If I don’t respond within a week or two, please be aware that I am not trying to be a jerk, nor am I trying to avoid talking to you. I’m just busy (or, if I’m lucky, on vacation somewhere). So, then! Thanks for buying this text (or at least looking at it in the bookstore while you try to decide whether you will buy it). I hope you enjoy reading this book and putting your newfound knowledge to good use. — Andrew Troelsen P A R T I 1 Introducing C# and .NET Platform C H A P T E R 1 3 The Philosophy of .NET Microsoft’s .NET platform (and the related C# programming language) were formally introduced circa 2002, and have quickly become a mainstay of modern day software development. As mentioned in the book’s introductory section, the goal of this text is twofold. The first order of business is to provide you with deep and detailed examination of the syntax and semantics of C#. The second (equally important) order of business is to illustrate the use of numerous .NET APIs, including database access with ADO.NET and the Entity Framework (EF), the LINQ technology set, WPF, WCF, WF, and web site development using ASP.NET. As they say, the journey of a thousand miles begins with a single step; and with this I welcome you to Chapter 1. The point of this first chapter is to lay the conceptual groundwork for the remainder of the book. Here you will find a high-level discussion of a number of .NET-related topics such as assemblies, the Common Intermediate Language (CIL), and just-in-time (JIT) compilation. In addition to previewing some keywords of the C# programming language, you will also come to understand the relationship between various aspects of the .NET Framework, such as the Common Language Runtime (CLR), the Common Type System (CTS), and the Common Language Specification (CLS). This chapter also provides you with a survey of the functionality supplied by the .NET 4.5 base class libraries, sometimes abbreviated as BCLs. Here, you will also overview the language-agnostic and platform-independent nature of the .NET platform (yes, it’s true; .NET is not confined to the Windows operating system) and be provided with a brief overview regarding .NET’s role in constructing applications under the Windows 8 operating system. As you would hope, many of these topics are explored in further detail throughout the remainder of this text. An Initial Look at the .NET Platform Before Microsoft released the C# language and .NET platform, software developers who created applications for the Windows family of operating system frequently made use of the COM programming model. COM (which stands for the Component Object Model) allowed individuals to build libraries of code that could be shared across diverse programming languages. For example, a C++ programmer could build a COM library that could be used by a Visual Basic developer. The language-independent nature of COM was certainly useful; however, COM was plagued by complicated infrastructure, a fragile deployment model, and was only possible on the Windows operating system. Despite the complexity and limitations of COM, countless applications have been successful created with this architecture. However, nowadays, a majority of applications created for the Windows family of operating systems are not created with the COM model. Rather, desktop applications, web sites, OS services, and libraries of reusable data access/business logic are created using the .NET platform. CHAPTER 1  THE PHILOSOPHY OF .NET 4 Some Key Benefits of the .NET Platform As mentioned, C# and the .NET platform were first introduced to the world in 2002 and were intended to offer a much more powerful, more flexible, and simpler programming model than COM. As you will see during the remainder of this book, the .NET Framework is a software platform for building systems on the Windows family of operating systems, as well as on numerous non-Microsoft operating systems such as Mac OS X and various Unix/Linux distributions. To set the stage, here is a quick rundown of some core features provided courtesy of .NET: Interoperability with existing code: This is (of course) a good thing. Existing COM binaries can commingle (i.e., interop) with newer .NET software and vice versa. As of .NET 4.0 onward, interoperability has been further simplified with the addition of the dynamic keyword (covered in Chapter 16). Support for numerous programming languages: .NET applications can be created using any number of programming languages (C#, Visual Basic, F#, and so on). A common runtime engine shared by all .NET-aware languages: One aspect of this engine is a well-defined set of types that each .NET-aware language understands. Language integration: .NET supports cross-language inheritance, cross-language exception handling, and cross-language debugging of code. For example, you can define a base class in C#, and extend this type in Visual Basic. A comprehensive base class library: This library provides shelter from the complexities of low-level API calls and offers a consistent object model used by all .NET-aware languages. A simplified deployment model: Unlike COM, .NET libraries are not registered into the system registry. Furthermore, the .NET platform allows multiple versions of the same *.dll to exist in harmony on a single machine. You will see each of these topics (and many more) examined in the chapters to come. Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS) Now that you know some of the major benefits provided by .NET, let’s preview three key (and interrelated) topics that make it all possible: the CLR, CTS, and CLS. From a programmer’s point of view, .NET can be understood as a runtime environment and a comprehensive base class library. The runtime layer is properly referred to as the Common Language Runtime, or CLR. The primary role of the CLR is to locate, load, and manage .NET objects on your behalf. The CLR also takes care of a number of low-level details such as memory management, application hosting, coordinating threads, and performing security checks (among other low-level details). Another building block of the .NET platform is the Common Type System, or CTS. The CTS specification fully describes all possible data types and all programming constructs supported by the runtime, specifies how these entities can interact with each other, and details how they are represented in the .NET metadata format (more information on metadata later in this chapter; see Chapter 15 for complete details). Understand that a given .NET-aware language might not support each and every feature defined by the CTS. The Common Language Specification, or CLS, is a related specification that defines a subset of CHAPTER 1  THE PHILOSOPHY OF .NET 5 common types and programming constructs that all .NET programming languages can agree on. Thus, if you build .NET types that expose only CLS-compliant features, you can rest assured that all .NET-aware languages can consume them. Conversely, if you make use of a data type or programming construct that is outside of the bounds of the CLS, you cannot guarantee that every .NET programming language can interact with your .NET code library. Thankfully, as you will see later in this chapter, it is very simple to tell your C# compiler to check all of your code for CLS compliance. The Role of the Base Class Libraries In addition to the CLR and CTS/CLS specifications, the .NET platform provides a base class library that is available to all .NET programming languages. Not only does this base class library encapsulate various primitives such as threads, file input/output (I/O), graphical rendering systems, and interaction with various external hardware devices, but it also provides support for a number of services required by most real-world applications. The base class libraries define types that can be used to build any type of software application. For example, you can use ASP.NET to build web sites, WCF to build networked services, WPF to build desktop GUI applications, and so forth. As well, the base class libraries provide types to interact with XML documents, the local directory and file system on a given computer, communicate with a relational databases (via ADO.NET), and so forth. From a high level, you can visualize the relationship between the CLR, CTS, CLS, and the base class library, as shown in Figure 1-1. Figure 1-1. The CLR, CTS, CLS, and base class library relationship What C# Brings to the Table C# is a programming language whose core syntax looks very similar to the syntax of Java. However, to call C# a Java clone is inaccurate. In reality, both C# and Java are members of the C family of programming languages (e.g., C, Objective C, C++) and, therefore, share a similar syntax. The truth of the matter is that many of C#’s syntactic constructs are modeled after various aspects of Visual Basic (VB) and C++. For example, like VB, C# supports the notion of class properties (as opposed to traditional getter and setter methods) and optional parameters. Like C++, C# allows you to overload operators, as well as create structures, enumerations, and callback functions (via delegates). CHAPTER 1  THE PHILOSOPHY OF .NET 6 Moreover, as you work through this text, you will quickly see that C# supports a number of features traditionally found in various functional languages (e.g., LISP or Haskell) such as lambda expressions and anonymous types. Furthermore, with the advent of LINQ (Language Integrated Query), C# supports a number of constructs that make it quite unique in the programming landscape. Nevertheless, the bulk of C# is indeed influenced by C-based languages. Due to the fact that C# is a hybrid of numerous languages, the result is a product that is as syntactically clean—if not cleaner—as Java, is about as simple as VB, and provides just about as much power and flexibility as C++. Here is a partial list of core C# features that are found in all versions of the language. No pointers required! C# programs typically have no need for direct pointer manipulation (although you are free to drop down to that level if absolutely necessary, as shown in Chapter 11). Automatic memory management through garbage collection. Given this, C# does not support a delete keyword. Formal syntactic constructs for classes, interfaces, structures, enumerations, and delegates. The C++-like ability to overload operators for a custom type, without the complexity (e.g., making sure to “return *this to allow chaining” is not your problem). Support for attribute-based programming. This brand of development allows you to annotate types and their members to further qualify their behavior. For example, if you mark a method with the [Obsolete] attribute, programmers will see your custom warning message print out if they attempt to make use of the decorated member. With the release of .NET 2.0 (circa 2005), the C# programming language was updated to support numerous new bells and whistles, most notability the following: The ability to build generic types and generic members. Using generics, you are able to build very efficient and type-safe code that defines numerous placeholders specified at the time you interact with the generic item. Support for anonymous methods, which allow you to supply an inline function anywhere a delegate type is required. The ability to define a single type across multiple code files (or if necessary, as an in-memory representation) using the partial keyword. .NET 3.5 (released circa 2008) added even more functionality to the C# programming language, including the following features: Support for strongly typed queries (e.g., LINQ) used to interact with various forms of data. You will first encounter LINQ in Chapter 12. Support for anonymous types that allow you to model the shape of a type rather than its behavior. The ability to extend the functionality of an existing type (without subclassing) using extension methods. CHAPTER 1  THE PHILOSOPHY OF .NET 7 Inclusion of a lambda operator (=>), which even further simplifies working with .NET delegate types. A new object initialization syntax, which allows you to set property values at the time of object creation. .NET 4.0 (released in 2010) updated C# yet again with a handful of features. For example: Support for optional method parameters, as well as named method arguments. Support for dynamic lookup of members at runtime via the dynamic keyword. As you will see in Chapter 18 this provides a unified approach to invoking members on the fly, regardless of which framework the member implemented (COM, IronRuby, IronPython, or via .NET reflection services). Working with generic types is much more intuitive, given that you can easily map generic data to and from general System.Object collections via covariance and contravariance. And that brings us to the current version of C# found with .NET 4.5. The current version of C# provides a pair of new keywords (async and await), which massively simplify multithreaded and asynchronous programming. If you have worked with previous versions of C#, you might recall that calling methods via secondary threads required a fair amount of cryptic code and the use of various .NET namespaces. Given that C# now supports language keywords that handle this complexity for you, the process of calling methods asynchronously is almost as easy as calling a method in a synchronous manner. Chapter 19 will cover these topics in detail. Managed vs. Unmanaged Code Perhaps the most important point to understand about the C# language is that it can produce code that can execute only within the .NET runtime (you could never use C# to build a native COM server or an unmanaged C/C++ application). Officially speaking, the term used to describe the code targeting the .NET runtime is managed code. The binary unit that contains the managed code is termed an assembly (more details on assemblies in just a bit). Conversely, code that cannot be directly hosted by the .NET runtime is termed unmanaged code. Additional .NET-Aware Programming Languages Understand that C# is not the only language that can be used to build .NET applications. Out of the box, Visual Studio provides you with five managed languages, specifically, C#, Visual Basic, C++/CLI, JavaScript, and F#.  Note F# is a .NET language based on the syntax of functional languages. While F# can be used as a purely functional language, it also has support for OOP constructs and the .NET base class libraries. If you are interested in learning more about this managed language, navigate online to the official F# homepage, http://msdn.microsoft.com/fsharp. CHAPTER 1  THE PHILOSOPHY OF .NET 8 In addition to the managed languages provided by Microsoft, there are .NET compilers for Smalltalk, Ruby, Python, COBOL, and Pascal (to name a few). Although this book focuses almost exclusively on C#, you might be interested in the following web site: www.dotnetlanguages.net If you click the Resources link at the top of the homepage, you will find a list of .NET programming languages and related links where you are able to download various compilers (see Figure 1-2). Figure 1-2. DotNetLanguages.net is one of many sites documenting known .NET programming languages While I assume you are primarily interested in building .NET programs using the syntax of C#, I encourage you to visit this site, as you are sure to find many .NET languages worth investigating at your leisure (LISP.NET, anyone?). Life in a Multilanguage World As developers first come to understand the language-agnostic nature of .NET, numerous questions arise. The most prevalent of these questions would have to be, “If all .NET languages compile down to managed code, why do we need more than one language/compiler?” CHAPTER 1  THE PHILOSOPHY OF .NET 9 There are a number of ways to answer this question. First, we programmers are a very particular lot when it comes to our choice of programming language. Some of us prefer languages full of semicolons and curly brackets with as few language keywords as possible. Others enjoy a language that offers more human-readable syntactic tokens (such as Visual Basic). Still others might want to leverage their mainframe skills while moving to the .NET platform (via the COBOL .NET compiler). Now, be honest. If Microsoft were to build a single “official” .NET language derived from the BASIC family of languages, can you really say all programmers would be happy with this choice? Or, if the only “official” .NET language was based on Fortran syntax, imagine all the folks out there who would ignore .NET altogether. Because the .NET runtime couldn’t care less which language was used to build a block of managed code, .NET programmers can stay true to their syntactic preferences and share the compiled code among teammates, departments, and external organizations (regardless of which .NET language others choose to use). Another excellent byproduct of integrating various .NET languages into a single, unified software solution is the simple fact that all programming languages have their own sets of strengths and weaknesses. For example, some programming languages offer excellent intrinsic support for advanced mathematical processing. Others offer superior support for financial calculations, logical calculations, interaction with mainframe computers, and so forth. When you take the strengths of a particular programming language and then incorporate the benefits provided by the .NET platform, everybody wins. Of course, in reality the chances are quite good that you will spend much of your time building software using your .NET language of choice. However, once you master the syntax of one .NET language, it is very easy to learn another. This is also quite beneficial, especially to the software consultants of the world. If your language of choice happens to be C# but you are placed at a client site that has committed to Visual Basic, you are still able to leverage the functionality of the .NET Framework, and you should be able to understand the overall structure of the code base with minimal fuss and bother. An Overview of .NET Assemblies Regardless of which .NET language you choose to program with, understand that despite the fact that .NET binaries take the same file extension as unmanaged Windows binaries (*.dll or *.exe), they have absolutely no internal similarities. Specifically, .NET binaries do not contain platform-specific instructions, but rather platform-agnostic Intermediate Language (IL) and type metadata. Figure 1-3 shows the big picture of the story thus far. Figure 1-3. All .NET-aware compilers emit IL instructions and metadata CHAPTER 1  THE PHILOSOPHY OF .NET 10  Note There is one point to be made regarding the abbreviation “IL.” IL is also known as Microsoft Intermediate Language (MSIL) or alternatively as the Common Intermediate Language (CIL). Thus, as you read the .NET literature, understand that IL, MSIL, and CIL are all describing the same concept. In this text, I will use the abbreviation CIL to refer to this low-level instruction set. When a *.dll or *.exe has been created using a .NET-aware compiler, the binary blob is termed an assembly. You will examine numerous details of .NET assemblies in Chapter 14. However, to facilitate the current discussion, you do need to understand some basic properties of this new file format. As mentioned, an assembly contains CIL code, which is conceptually similar to Java bytecode in that it is not compiled to platform-specific instructions until absolutely necessary. Typically, “absolutely necessary” is the point at which a block of CIL instructions (such as a method implementation) is referenced for use by the .NET runtime. In addition to CIL instructions, assemblies also contain metadata that describes in vivid detail the characteristics of every “type” within the binary. For example, if you have a class named SportsCar, the type metadata describes details such as SportsCar’s base class, which interfaces are implemented by SportsCar (if any), as well as a full description of each member supported by the SportsCar type. .NET metadata is always present within an assembly, and is automatically generated by a .NET-aware language compiler. Finally, in addition to CIL and type metadata, assemblies themselves are also described using metadata, which is officially termed a manifest. The manifest contains information about the current version of the assembly, culture information (used for localizing string and image resources), and a list of all externally referenced assemblies that are required for proper execution. You’ll examine various tools that can be used to examine an assembly’s types, metadata, and manifest information over the course of the next few chapters. The Role of the Common Intermediate Language Let’s examine CIL code, type metadata, and the assembly manifest in a bit more detail. CIL is a language that sits above any particular platform-specific instruction set. For example, the following C# code models a trivial calculator. Don’t concern yourself with the exact syntax for now, but do notice the format of the Add() method in the Calc class. // Calc.cs using System; namespace CalculatorExample { // This class contains the app's entry point. class Program { static void Main() { Calc c = new Calc(); int ans = c.Add(10, 84); Console.WriteLine(“10 + 84 is {0}.“, ans); CHAPTER 1  THE PHILOSOPHY OF .NET 11 // Wait for user to press the Enter key before shutting down. Console.ReadLine(); } } // The C# calculator. class Calc { public int Add(int x, int y) { return x + y; } } } After you compile this code file using the C# compiler (csc.exe), you end up with a single-file *.exe assembly that contains a manifest, CIL instructions, and metadata describing each aspect of the Calc and Program classes.  Note Chapter 2 examines the details of compiling code using the C# compiler, as well as the use of graphical IDEs such as Microsoft Visual Studio (among others). For example, if you were to open this assembly using ildasm.exe (examined a little later in this chapter), you would find that the Add() method is represented using CIL such as the following: .method public hidebysig instance int32 Add(int32 x, int32 y) cil managed { // Code size 9 (0x9) .maxstack 2 .locals init (int32 V_0) IL_0000: nop IL_0001: ldarg.1 IL_0002: ldarg.2 IL_0003: add IL_0004: stloc.0 IL_0005: br.s IL_0007 IL_0007: ldloc.0 IL_0008: ret } // end of method Calc::Add Don’t worry if you are unable to make heads or tails of the resulting CIL for this method—Chapter 17 will describe the basics of the CIL programming language. The point to concentrate on is that the C# compiler emits CIL, not platform-specific instructions. Now, recall that this is true of all .NET-aware compilers. To illustrate, assume you created this same application using Visual Basic, rather than C#. ' Calc.vb Imports System Namespace CalculatorExample CHAPTER 1  THE PHILOSOPHY OF .NET 12 ' A VB “Module“ is a class that contains only ' static members. Module Program Sub Main() Dim c As New Calc Dim ans As Integer = c.Add(10, 84) Console.WriteLine(“10 + 84 is {0}.“, ans) Console.ReadLine() End Sub End Module Class Calc Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer Return x + y End Function End Class End Namespace If you examine the CIL for the Add() method, you find similar instructions (slightly tweaked by the Visual Basic compiler, vbc.exe). .method public instance int32 Add(int32 x, int32 y) cil managed { // Code size 8 (0x8) .maxstack 2 .locals init (int32 V_0) IL_0000: ldarg.1 IL_0001: ldarg.2 IL_0002: add.ovf IL_0003: stloc.0 IL_0004: br.s IL_0006 IL_0006: ldloc.0 IL_0007: ret } // end of method Calc::Add  Source Code The Calc.cs and Calc.vb code files are included under the Chapter 1 subdirectory. Benefits of CIL At this point, you might be wondering exactly what is gained by compiling source code into CIL rather than directly to a specific instruction set. One benefit is language integration. As you have already seen, each .NET-aware compiler produces nearly identical CIL instructions. Therefore, all languages are able to interact within a well-defined binary arena. Furthermore, given that CIL is platform-agnostic, the .NET Framework itself is platform-agnostic, providing the same benefits Java developers have grown accustomed to (e.g., a single code base running CHAPTER 1  THE PHILOSOPHY OF .NET 13 on numerous operating systems). In fact, there is an international standard for the C# language and a large subset of the .NET platform and implementations already exist for many non-Windows operating systems (more details at the conclusion of this chapter). Compiling CIL to Platform-Specific Instructions Due to the fact that assemblies contain CIL instructions rather than platform-specific instructions, CIL code must be compiled on the fly before use. The entity that compiles CIL code into meaningful CPU instructions is a JIT compiler, which sometimes goes by the friendly name of Jitter. The .NET runtime environment leverages a JIT compiler for each CPU targeting the runtime, each optimized for the underlying platform. For example, if you are building a .NET application to be deployed to a handheld device (such as a Windows mobile device), the corresponding Jitter is well equipped to run within a low-memory environment. On the other hand, if you are deploying your assembly to a back-end company server (where memory is seldom an issue), the Jitter will be optimized to function in a high-memory environment. In this way, developers can write a single body of code that can be efficiently JIT compiled and executed on machines with different architectures. Furthermore, as a given Jitter compiles CIL instructions into corresponding machine code, it will cache the results in memory in a manner suited to the target operating system. In this way, if a call is made to a method named PrintDocument(), the CIL instructions are compiled into platform-specific instructions on the first invocation and retained in memory for later use. Therefore, the next time PrintDocument() is called, there is no need to recompile the CIL.  Note It is also possible to perform a “pre-JIT” of an assembly when installing your application using the ngen.exe command-line tool that ships with the .NET 4.5 Framework SDK. Doing so can improve startup time for graphically intensive applications. The Role of .NET Type Metadata In addition to CIL instructions, a .NET assembly contains full, complete, and accurate metadata, which describes each and every type (e.g., class, structure, enumeration) defined in the binary, as well as the members of each type (e.g., properties, methods, events). Thankfully, it is always the job of the compiler (not the programmer) to emit the latest and greatest type metadata. Because .NET metadata is so wickedly meticulous, assemblies are completely self-describing entities. To illustrate the format of .NET type metadata, let’s take a look at the metadata that has been generated for the Add() method of the C# Calc class you examined previously (the metadata generated for the Visual Basic version of the Add() method is similar). TypeDef #2 (02000003) ------------------------------------------------------- TypDefName: CalculatorExample.Calc (02000003) Flags : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100001) Extends : 01000001 [TypeRef] System.Object Method #1 (06000003) CHAPTER 1  THE PHILOSOPHY OF .NET 14 ------------------------------------------------------- MethodName: Add (06000003) Flags : [Public] [HideBySig] [ReuseSlot] (00000086) RVA : 0x00002090 ImplFlags : [IL] [Managed] (00000000) CallCnvntn: [DEFAULT] hasThis ReturnType: I4 2 Arguments Argument #1: I4 Argument #2: I4 2 Parameters (1) ParamToken : (08000001) Name : x flags: [none] (00000000) (2) ParamToken : (08000002) Name : y flags: [none] (00000000) Metadata is used by numerous aspects of the .NET runtime environment, as well as by various development tools. For example, the IntelliSense feature provided by tools such as Visual Studio is made possible by reading an assembly’s metadata at design time. Metadata is also used by various object- browsing utilities, debugging tools, and the C# compiler itself. To be sure, metadata is the backbone of numerous .NET technologies including Windows Communication Foundation (WCF), reflection, late binding, and object serialization. Chapter 15 will formalize the role of .NET metadata. The Role of the Assembly Manifest Last but not least, remember that a .NET assembly also contains metadata that describes the assembly itself (technically termed a manifest). Among other details, the manifest documents all external assemblies required by the current assembly to function correctly, the assembly’s version number, copyright information, and so forth. Like type metadata, it is always the job of the compiler to generate the assembly’s manifest. Here are some relevant details of the manifest generated when compiling the Calc.cs code file shown earlier in this chapter (assume we instructed the compiler to name our assembly Calc.exe). .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 4:0:0:0 } .assembly Calc { .hash algorithm 0x00008004 .ver 0:0:0:0 } .module Calc.exe .imagebase 0x00400000 .subsystem 0x00000003 .file alignment 0x00000200 .corflags 0x00000001 In a nutshell, this manifest documents the set of external assemblies required by Calc.exe (via the .assembly extern directive) as well as various characteristics of the assembly itself (e.g., version number, module name). Chapter 14 will examine the usefulness of manifest data in much more detail. CHAPTER 1  THE PHILOSOPHY OF .NET 15 Understanding the Common Type System A given assembly may contain any number of distinct types. In the world of .NET, type is simply a general term used to refer to a member from the set {class, interface, structure, enumeration, delegate}. When you build solutions using a .NET-aware language, you will most likely interact with many of these types. For example, your assembly might define a single class that implements some number of interfaces. Perhaps one of the interface methods takes an enumeration type as an input parameter and returns a structure to the caller. Recall that the CTS is a formal specification that documents how types must be defined in order to be hosted by the CLR. Typically, the only individuals who are deeply concerned with the inner workings of the CTS are those building tools and/or compilers that target the .NET platform. It is important, however, for all .NET programmers to learn about how to work with the five types defined by the CTS in their language of choice. Following is a brief overview. CTS Class Types Every .NET-aware language supports, at the very least, the notion of a class type, which is the cornerstone of OOP (Object Oriented Programming). A class may be composed of any number of members (such as constructors, properties, methods, and events) and data points (fields). In C#, classes are declared using the class keyword, like so: // A C# class type with 1 method. class Calc { public int Add(int x, int y) { return x + y; } } Chapter 5 will begin your formal examination of building class types with C#; however, Table 1-1 documents a number of characteristics pertaining to class types. Table 1-1. CTS Class Characteristics Class Characteristic Meaning in Life Is the class sealed or not? Sealed classes cannot function as a base class to other classes. Does the class implement any interfaces? An interface is a collection of abstract members that provide a contract between the object and object user. The CTS allows a class to implement any number of interfaces. Is the class abstract or concrete? Abstract classes cannot be directly instantiated, but are intended to define common behaviors for derived types. Concrete classes can be instantiated directly. CHAPTER 1  THE PHILOSOPHY OF .NET 16 Class Characteristic Meaning in Life What is the visibility of this class? Each class must be configured with a visibility keyword such as public or internal. Basically, this controls if the class may be used by external assemblies or only from within the defining assembly. CTS Interface Types Interfaces are nothing more than a named collection of abstract member definitions, which may be supported (i.e., implemented) by a given class or structure. In C#, interface types are defined using the interface keyword. By convention, all .NET interfaces begin with a capital letter I, as in the following example: // A C# interface type is usually // declared as public, to allow types in other // assemblies to implement their behavior. public interface IDraw { void Draw(); } On their own, interfaces are of little use. However, when a class or structure implements a given interface in its unique way, you are able to request access to the supplied functionality using an interface reference in a polymorphic manner. Interface-based programming will be fully explored in Chapter 8. CTS Structure Types The concept of a structure is also formalized under the CTS. If you have a C background, you should be pleased to know that these user-defined types (UDTs) have survived in the world of .NET (although they behave a bit differently under the hood). Simply put, a structure can be thought of as a lightweight class type having value-based semantics. For more details on the subtleties of structures, see Chapter 4. Typically, structures are best suited for modeling geometric and mathematical data and are created in C# using the struct keyword, as follows: // A C# structure type. struct Point { // Structures can contain fields. public int xPos, yPos; // Structures can contain parameterized constructors. public Point(int x, int y) { xPos = x; yPos = y;} // Structures may define methods. public void PrintPosition() { Console.WriteLine(“({0}, {1})“, xPos, yPos); } } CHAPTER 1  THE PHILOSOPHY OF .NET 17 CTS Enumeration Types Enumerations are a handy programming construct that allow you to group name/value pairs. For example, assume you are creating a video game application that allows the player to select one of three character categories (Wizard, Fighter, or Thief). Rather than keeping track of simple numerical values to represent each possibility, you could build a strongly typed enumeration using the enum keyword. // A C# enumeration type. enum CharacterType { Wizard = 100, Fighter = 200, Thief = 300 } By default, the storage used to hold each item is a 32-bit integer; however, it is possible to alter this storage slot if need be (e.g., when programming for a low-memory device such as a mobile device). Also, the CTS demands that enumerated types derive from a common base class, System.Enum. As you will see in Chapter 4, this base class defines a number of interesting members that allow you to extract, manipulate, and transform the underlying name/value pairs programmatically. CTS Delegate Types Delegates are the .NET equivalent of a type-safe, C-style function pointer. The key difference is that a .NET delegate is a class that derives from System.MulticastDelegate, rather than a simple pointer to a raw memory address. In C#, delegates are declared using the delegate keyword. // This C# delegate type can “point to“ any method // returning an int and taking two ints as input. delegate int BinaryOp(int x, int y); Delegates are critical when you want to provide a way for one object to forward a call to another object and provide the foundation for the .NET event architecture. As you will see in Chapters 11 and 19, delegates have intrinsic support for multicasting (i.e., forwarding a request to multiple recipients) and asynchronous method invocations (i.e., invoking the method on a secondary thread). CTS Type Members Now that you have previewed each of the types formalized by the CTS, realize that most types take any number of members. Formally speaking, a type member is constrained by the set {constructor, finalizer, static constructor, nested type, operator, method, property, indexer, field, read-only field, constant, event}. The CTS defines various adornments that may be associated with a given member. For example, each member has a given visibility trait (e.g., public, private, protected). Some members may be declared as abstract (to enforce a polymorphic behavior on derived types) as well as virtual (to define a canned, but overridable, implementation). Also, most members may be configured as static (bound at the class level) or instance (bound at the object level). The creation of type members is examined over the course of the next several chapters. CHAPTER 1  THE PHILOSOPHY OF .NET 18  Note As described in Chapter 9, the C# language also supports the creation of generic types and generic members. Intrinsic CTS Data Types The final aspect of the CTS to be aware of for the time being is that it establishes a well-defined set of fundamental data types. Although a given language typically has a unique keyword used to declare a fundamental data type, all .NET language keywords ultimately resolve to the same CTS type defined in an assembly named mscorlib.dll. Consider Table 1-2, which documents how key CTS data types are expressed in various .NET languages. Table 1-2. The Intrinsic CTS Data Types CTS Data Type VB Keyword C# Keyword C++/CLI Keyword System.Byte Byte byte unsigned char System.SByte SByte sbyte signed char System.Int16 Short short short System.Int32 Integer int int or long System.Int64 Long long __int64 System.UInt16 UShort ushort unsigned short System.UInt32 UInteger uint unsigned int or unsigned long System.UInt64 ULong ulong unsigned __int64 System.Single Single float float System.Double Double double double System.Object Object object object^ System.Char Char char wchar_t System.String String string String^ System.Decimal Decimal decimal Decimal System.Boolean Boolean bool bool CHAPTER 1  THE PHILOSOPHY OF .NET 19 Given the fact that the unique keywords of a managed language are simply shorthand notations for a real type in the System namespace, we no longer have to worry about overflow/underflow conditions for numerical data, or how strings and Booleans are internally represented across different languages. Consider the following code snippets, which define 32-bit numerical variables in C# and Visual Basic, using language keywords as well as the formal CTS data type: // Define some “ints“ in C#. int i = 0; System.Int32 j = 0; ' Define some “ints“ in VB. Dim i As Integer = 0 Dim j As System.Int32 = 0 Understanding the Common Language Specification As you are aware, different languages express the same programming constructs in unique, language- specific terms. For example, in C# you denote string concatenation using the plus operator (+), while in VB you typically make use of the ampersand (&). Even when two distinct languages express the same programmatic idiom (e.g., a function with no return value), the chances are very good that the syntax will appear quite different on the surface. // C# method returning nothing. public void MyMethod() { // Some interesting code... } ' VB method returning nothing. Public Sub MyMethod() ' Some interesting code... End Sub As you have already seen, these minor syntactic variations are inconsequential in the eyes of the .NET runtime, given that the respective compilers (csc.exe or vbc.exe, in this case) emit a similar set of CIL instructions. However, languages can also differ with regard to their overall level of functionality. For example, a .NET language might or might not have a keyword to represent unsigned data, and might or might not support pointer types. Given these possible variations, it would be ideal to have a baseline to which all .NET-aware languages are expected to conform. The CLS is a set of rules that describe in vivid detail the minimal and complete set of features a given .NET-aware compiler must support to produce code that can be hosted by the CLR, while at the same time be accessed in a uniform manner by all languages that target the .NET platform. In many ways, the CLS can be viewed as a subset of the full functionality defined by the CTS. The CLS is ultimately a set of rules that compiler builders must conform to if they intend their products to function seamlessly within the .NET universe. Each rule is assigned a simple name (e.g., CLS Rule 6) and describes how this rule affects those who build the compilers as well as those who (in some way) interact with them. The crème de la crème of the CLS is Rule 1. Rule 1: CLS rules apply only to those parts of a type that are exposed outside the defining assembly. CHAPTER 1  THE PHILOSOPHY OF .NET 20 Given this rule, you can (correctly) infer that the remaining rules of the CLS do not apply to the logic used to build the inner workings of a .NET type. The only aspects of a type that must conform to the CLS are the member definitions themselves (i.e., naming conventions, parameters, and return types). The implementation logic for a member may use any number of non-CLS techniques, as the outside world won’t know the difference. To illustrate, the following C# Add() method is not CLS compliant, as the parameters and return values make use of unsigned data (which is not a requirement of the CLS): class Calc { // Exposed unsigned data is not CLS compliant! public ulong Add(ulong x, ulong y) { return x + y; } } However, if you were to only make use of unsigned data internally in a method, as follows: class Calc { public int Add(int x, int y) { // As this ulong variable is only used internally, // we are still CLS compliant. ulong temp = 0; ... return x + y; } } you have still conformed to the rules of the CLS and can rest assured that all .NET languages are able to invoke the Add() method. Of course, in addition to Rule 1, the CLS defines numerous other rules. For example, the CLS describes how a given language must represent text strings, how enumerations should be represented internally (the base type used for storage), how to define static members, and so forth. Luckily, you don’t have to commit these rules to memory to be a proficient .NET developer. Again, by and large, an intimate understanding of the CTS and CLS specifications is typically only of interest to tool/compiler builders. Ensuring CLS Compliance As you will see over the course of this book, C# does define a number of programming constructs that are not CLS compliant. The good news, however, is that you can instruct the C# compiler to check your code for CLS compliance using a single .NET attribute. // Tell the C# compiler to check for CLS compliance. [assembly: CLSCompliant(true)] Chapter 15 dives into the details of attribute-based programming. Until then, simply understand that the [CLSCompliant] attribute will instruct the C# compiler to check each and every line of code against the rules of the CLS. If any CLS violations are discovered, you receive a compiler error and a description of the offending code. CHAPTER 1  THE PHILOSOPHY OF .NET 21 Understanding the Common Language Runtime In addition to the CTS and CLS specifications, the final TLA (three-letter abbreviation) to contend with at the moment is the CLR. Programmatically speaking, the term runtime can be understood as a collection of services that are required to execute a given compiled unit of code. For example, when Java developers deploy software to a new computer, they need to ensure the machine has been installed with the Java Virtual Machine (JVM) in order to run their software. The .NET platform offers yet another runtime system. The key difference between the .NET runtime and the various other runtimes I just mentioned is the fact that the .NET runtime provides a single, well- defined runtime layer that is shared by all languages and platforms that are .NET-aware. The crux of the CLR is physically represented by a library named mscoree.dll (a.k.a. the Common Object Runtime Execution Engine). When an assembly is referenced for use, mscoree.dll is loaded automatically, which in turn loads the required assembly into memory. The runtime engine is responsible for a number of tasks. First and foremost, it is the agent in charge of resolving the location of an assembly and finding the requested type within the binary by reading the contained metadata. The CLR then lays out the type in memory, compiles the associated CIL into platform-specific instructions, performs any necessary security checks, and then executes the code in question. In addition to loading your custom assemblies and creating your custom types, the CLR will also interact with the types contained within the .NET base class libraries when required. Although the entire base class library has been broken into a number of discrete assemblies, the key assembly is mscorlib.dll, which contains a large number of core types that encapsulate a wide variety of common programming tasks, as well as the core data types used by all .NET languages. When you build .NET solutions, you automatically have access to this particular assembly. Figure 1-4 illustrates the workflow that takes place between your source code (which is making use of base class library types), a given .NET compiler, and the .NET execution engine. CHAPTER 1  THE PHILOSOPHY OF .NET 22 Figure 1-4. mscoree.dll in action The Assembly/Namespace/Type Distinction Each of us understands the importance of code libraries. The point of framework libraries is to give developers a well-defined set of existing code to leverage in their applications. However, the C# language does not come with a language-specific code library. Rather, C# developers leverage the language- neutral .NET libraries. To keep all the types within the base class libraries well organized, the .NET platform makes extensive use of the namespace concept. A namespace is a grouping of semantically related types contained in an assembly or possibly spread across multiple related assemblies. For example, the System.IO namespace contains file I/O- related types, the System.Data namespace defines basic database types, and so on. It is very important to point out that a single assembly (such as mscorlib.dll) can contain any number of namespaces, each of which can contain any number of types. To clarify, Figure 1-5 shows a screenshot of the Visual Studio Object Browser utility. This tool allows you to examine the assemblies referenced by your current project, the namespaces within a particular assembly, the types within a given namespace, and the members of a specific type. Note that the CHAPTER 1  THE PHILOSOPHY OF .NET 23 mscorlib.dll assembly contains many different namespaces (such as System.IO), each with its own semantically related types (e.g., BinaryReader). Figure 1-5. A single assembly can have any number of namespaces The key difference between this approach and a language-specific library is that any language targeting the .NET runtime makes use of the same namespaces and same types. For example, the following three programs all illustrate the ubiquitous “Hello World” application, written in C#, VB, and C++/CLI: // Hello world in C#. using System; public class MyApp { static void Main() { Console.WriteLine(“Hi from C#“); } } ' Hello world in VB. Imports System CHAPTER 1  THE PHILOSOPHY OF .NET 24 Public Module MyApp Sub Main() Console.WriteLine(“Hi from VB“) End Sub End Module // Hello world in C++/CLI. #include “stdafx.h“ using namespace System; int main(array ^args) { Console::WriteLine(L“Hi from C++/CLI“); return 0; } Notice that each language is making use of the Console class defined in the System namespace. Beyond some obvious syntactic variations, these three applications look and feel very much alike, both physically and logically. Clearly, your primary goal as a .NET developer is to get to know the wealth of types defined in the (numerous) .NET namespaces. The most fundamental namespace to get your hands around initially is named System. This namespace provides a core body of types that you will need to leverage time and again as a .NET developer. In fact, you cannot build any sort of functional C# application without at least making a reference to the System namespace, as the core data types (e.g., System.Int32, System.String) are defined here. Table 1-3 offers a rundown of some (but certainly not all) of the .NET namespaces grouped by related functionality. Table 1-3. A Sampling of .NET Namespaces .NET Namespace Meaning in Life System Within System, you find numerous useful types dealing with intrinsic data, mathematical computations, random number generation, environment variables, and garbage collection, as well as a number of commonly used exceptions and attributes. System.Collections System.Collections.Generic These namespaces define a number of stock container types, as well as base types and interfaces that allow you to build customized collections. System.Data System.Data.Common System.Data.EntityClient System.Data.SqlClient These namespaces are used for interacting with relational databases using ADO.NET. System.IO System.IO.Compression System.IO.Ports These namespaces define numerous types used to work with file I/O, compression of data, and port manipulation. CHAPTER 1  THE PHILOSOPHY OF .NET 25 .NET Namespace Meaning in Life System.Reflection System.Reflection.Emit These namespaces define types that support runtime type discovery as well as dynamic creation of types. System.Runtime.InteropServices This namespace provides facilities to allow .NET types to interact with unmanaged code (e.g., C-based DLLs and COM servers) and vice versa. System.Drawing System.Windows.Forms These namespaces define types used to build desktop applications using .NET’s original UI toolkit (Windows Forms). System.Windows System.Windows.Controls System.Windows.Shapes The System.Windows namespace is the root for several namespaces that represent the Windows Presentation Foundation (WPF) UI toolkit. System.Linq System.Xml.Linq System.Data.DataSetExtensions These namespaces define types used when programming against the LINQ API. System.Web This is one of many namespaces that allow you to build ASP.NET web applications. System.ServiceModel This is one of many namespaces used to build distributed applications using the Windows Communication Foundation API. System.Workflow.Runtime System.Workflow.Activities These are two of many namespaces that define types used to build “workflow-enabled” applications using the Windows Workflow Foundation API. System.Threading System.Threading.Tasks This namespace defines numerous types to build multithreaded applications that can distribute workloads across multiple CPUs. System.Security Security is an integrated aspect of the .NET universe. In the security-centric namespaces, you find numerous types dealing with permissions, cryptography, and so on. System.Xml The XML-centric namespaces contain numerous types used to interact with XML data. The Role of the Microsoft Root Namespace I’m sure you noticed while reading over the listings in Table 1-3, that System is the root namespace for a majority of nested namespaces (e.g., System.IO, System.Data). As it turns out, however, the .NET base class library defines a number of topmost root namespaces beyond System, the most useful of which is named Microsoft. CHAPTER 1  THE PHILOSOPHY OF .NET 26 Any namespace nested within Microsoft (e.g., Microsoft.CSharp, Microsoft.ManagementConsole, Microsoft.Win32) contains types that are used to interact with services unique to the Windows operating system. Given this point, you should not assume that these types could be used successfully on other .NET-enabled operating systems such as Mac OS X. For the most part, this text will not dig into the details of the Microsoft rooted namespaces, so be sure to consult the .NET Framework 4.5 SDK documentation if you are interested.  Note Chapter 2 will illustrate the use of the .NET Framework 4.5 SDK documentation, which provides details regarding every namespace, type, and member found within the base class libraries. Accessing a Namespace Programmatically It is worth reiterating that a namespace is nothing more than a convenient way for us mere humans to logically understand and organize related types. Consider again the System namespace. From your perspective, you can assume that System.Console represents a class named Console that is contained within a namespace called System. However, in the eyes of the .NET runtime, this is not so. The runtime engine only sees a single class named System.Console. In C#, the using keyword simplifies the process of referencing types defined in a particular namespace. Here is how it works. Let’s say you are interested in building a graphical desktop application using the WPF API. While learning the types each namespace contains takes study and experimentation, here are some possible candidates to reference in your program. // Here are some possible namespaces used to build a WPF application. using System; // General base class library types. using System.Windows.Shapes; // Graphical rendering types. using System.Windows.Controls; // Windows Forms GUI widget types. using System.Data; // General data-centric types. using System.Data.SqlClient; // MS SQL Server data-access types. Once you have specified some number of namespaces (and set a reference to the assemblies that define them), you are free to create instances of the types they contain. For example, if you are interested in creating an instance of the Button class (defined in the System.Windows.Controls namespace), you can write the following: // Explicitly list the namespaces used by this file. using System; using System.Windwos.Controls; class MyGUIBuilder { public void BuildUI() { // Create a button control. Button btnOK = new Button(); ... } } CHAPTER 1  THE PHILOSOPHY OF .NET 27 Because your code file is importing the System.Windows.Controls namespace, the compiler is able to resolve the Bitmap class as a member of this namespace. If you did not import the System.Windows.Controls namespace, you would be issued a compiler error. However, you are free to declare variables using a fully qualified name as well. // Not listing System.Drawing namespace! using System; class MyGUIBuilder { public void BuildUI() { // Using fully qualified name. System.Windows.Controls.Button btnOK = new System.Windows.Controls.Button(); ... } } While defining a type using the fully qualified name provides greater readability, I think you’d agree that the C# using keyword reduces keystrokes. In this text, I will avoid the use of fully qualified names (unless there is a definite ambiguity to be resolved) and opt for the simplified approach of the C# using keyword. However, always remember that the using keyword is simply a shorthand notation for specifying a type’s fully qualified name, and either approach results in the exact same underlying CIL (given the fact that CIL code always makes use of fully qualified names) and has no effect on performance or the size of the assembly. Referencing External Assemblies In addition to specifying a namespace via the C# using keyword, you also need to tell the C# compiler the name of the assembly containing the actual CIL definition for the referenced type. As mentioned, many core .NET namespaces are defined within mscorlib.dll. However, the System.Drawing.Bitmap class is contained within a separate assembly named System.Drawing.dll. A vast majority of the .NET Framework assemblies are located under a specific directory termed the global assembly cache (GAC). On a Windows machine, this can be located by default under C:\Windows\Assembly, as shown in Figure 1-6. CHAPTER 1  THE PHILOSOPHY OF .NET 28 Figure 1-6. Many .NET libraries reside in the GAC Depending on the development tool you are using to build your .NET applications, you will have various ways to inform the compiler which assemblies you want to include during the compilation cycle. You’ll examine how to do so in Chapter 2, so I’ll hold off on the details for now.  Note Beginning with .NET 4.0, Microsoft has opted to isolate .NET 4.0 and .NET 4.5 assemblies in a unique location, which is independent from C:\Windows\Assembly. Chapter 14 will address this point in detail. Exploring an Assembly Using ildasm.exe If you are beginning to feel a tad overwhelmed at the thought of gaining mastery over every namespace in the .NET platform, just remember that what makes a namespace unique is that it contains types that are somehow semantically related. Therefore, if you have no need for a user interface beyond a simple Console Application, you can forget all about the desktop and web namespaces (among others). If you are building a painting application, the database namespaces are most likely of little concern. Like any new set of prefabricated code, you learn as you go. The Intermediate Language Disassembler utility (ildasm.exe), which ships with the .NET Framework 4.5 SDK, allows you to load up any .NET assembly and investigate its contents, including the associated manifest, CIL code, and type metadata. This tool allows a programmer to dive deeply into how their C# code maps to CIL, and ultimately helps one understand the inner workings of the .NET platform. While you never need to use ildasm.exe to become a proficient .NET programmer, I highly recommend you fire up this tool from time to time to better understand how your C# code maps to runtime concepts. CHAPTER 1  THE PHILOSOPHY OF .NET 29  Note You can easily run ildasm.exe by opening a Developer Command Prompt, and type “ildasm” (without the quotes) followed by the Enter key. Chapter 2 will explain how to open this “special” command window, as well as other tools loaded from the command line. After you launch ildasm.exe, proceed to the File Open menu command and navigate to an assembly you would like to explore. By way of illustration, Figure 1-7 shows the Calc.exe assembly generated based on the Calc.cs file shown earlier in this chapter. ildasm.exe presents the structure of an assembly using a familiar tree-view format. Figure 1-7. ildasm.exe allows you to see the CIL code, manifest, and metadata within a .NET assembly Viewing CIL Code In addition to showing the namespaces, types, and members contained in a given assembly, ildasm.exe also allows you to view the CIL instructions for a given member. For example, if you were to double-click the Main() method of the Program class, a separate window would display the underlying CIL (see Figure 1-8). CHAPTER 1  THE PHILOSOPHY OF .NET 30 Figure 1-8. Viewing the underlying CIL Viewing Type Metadata If you want to view the type metadata for the currently loaded assembly, press Ctrl+M. Figure 1-9 shows the metadata for the Calc.Add() method. Figure 1-9. Viewing type metadata via ildasm.exe CHAPTER 1  THE PHILOSOPHY OF .NET 31 Viewing Assembly Metadata (a.k.a. the Manifest) Finally, if you are interested in viewing the contents of the assembly’s manifest, simply double-click the MANIFEST icon (see Figure 1-10). Figure 1-10. Viewing manifest data via ildasm.exe To be sure, ildasm.exe has more options than shown here, and I will illustrate additional features of the tool where appropriate in the text. The Platform-Independent Nature of .NET Allow me to briefly comment on the platform-independent nature of the .NET platform. To the surprise of most developers, .NET assemblies can be developed and executed on non-Microsoft operating systems, including Mac OS X, various Linux distributions, Solaris, as well as iOS and Android mobile devices (via the MonoTouch API). To understand how this is possible, you need to come to terms with yet another abbreviation in the .NET universe: CLI (Common Language Infrastructure). When Microsoft released the C# programming language and the .NET platform, they also crafted a set of formal documents that described the syntax and semantics of the C# and CIL languages, the .NET assembly format, core .NET namespaces, and the mechanics of a hypothetical .NET runtime engine (known as the Virtual Execution System, or VES). These documents have been submitted to (and ratified by) Ecma International (www.ecma- international.org) as official international standards. The specifications of interest are as follows: ECMA-334: The C# Language Specification ECMA-335: The Common Language Infrastructure (CLI) The importance of these documents becomes clear when you understand that they enable third parties to build distributions of the .NET platform for any number of operating systems and/or processors. ECMA-335 is the more “meaty” of the two specifications, so much so that is has been broken into various partitions, including those seen in Table 1-4. CHAPTER 1  THE PHILOSOPHY OF .NET 32 Table 1-4. Partitions of the CLI Partitions of ECMA-335 Meaning in Life Partition I: Concepts and Architecture Describes the overall architecture of the CLI, including the rules of the CTS and CLS, and the mechanics of the .NET runtime engine. Partition II: Metadata Definition and Semantics Describes the details of .NET metadata and the assembly format. Partition III: CIL Instruction Set Describes the syntax and semantics of CIL code. Partition IV: Profiles and Libraries Gives a high-level overview of the minimal and complete class libraries that must be supported by a .NET distribution. Partition V: Debug Interchange Format Describes a standard way to interchange debugging information between CLI producers and consumers. Partition VI: Annexes Provides a collection of odds-and-ends details such as class library design guidelines and the implementation details of a CIL compiler. Be aware that Partition IV (Profiles and Libraries) defines only a minimal set of namespaces that represent the core services expected by a CLI distribution (e.g., collections, console I/O, file I/O, threading, reflection, network access, core security needs, XML data manipulation). The CLI does not define namespaces that facilitate web development (ASP.NET), database access (ADO.NET), or desktop graphical user interface (GUI) application development (Windows Presentation Foundation or Windows Forms). The good news, however, is that the mainstream .NET distributions extend the CLI libraries with Microsoft-compatible equivalents of ASP.NET implementations and ADO.NET implementations (and so on) in order to provide full-featured, production-level development platforms. To date, there are two major implementations of the CLI (beyond Microsoft’s Windows-specific offering). Although this text focuses on the creation of .NET applications using Microsoft’s .NET distribution, Table 1-5 provides information regarding the Mono and Portable .NET projects. Table 1-5. Open Source .NET Distributions Distribution Meaning in Life www.mono-project.com The Mono project is an open source distribution of the CLI that targets various Linux distributions (e.g., SuSe, Fedora), Mac OS X, iOS devices (iPad, iPhone), Android devices, and (surprise!) Windows. www.dotgnu.org Portable.NET is another open source distribution of the CLI that runs on numerous operating systems. Portable.NET aims to target as many operating systems as possible (e.g., Windows, AIX, Mac OS X, Solaris, all major Linux distributions). CHAPTER 1  THE PHILOSOPHY OF .NET 33 Both Mono and Portable.NET provide an ECMA-compliant C# compiler, .NET runtime engine, code samples, documentation, as well as numerous development tools that are functionally equivalent to the tools that ship with Microsoft’s .NET Framework 4.5 SDK.  Note An introduction to creating cross-platform .NET applications using Mono can be found in Appendix A. A Brief Word Regarding Windows 8 Applications To close this chapter, allow me to briefly comment on how the Microsoft .NET platform relates to a brand-new set of technologies introduced with the Windows 8 operating system. If you have not yet had time to explore Windows 8, let me just say that the new user interface is radically different than any previous version of Microsoft Windows (in fact, I’d say it is as radical as the shift from Windows 3.11 to Windows 95—for those of you old enough to remember). Figure 1-11 illustrates the new start screen of Windows 8 OS. Figure 1-11. The Windows 8 start screen As seen in Figure 1-11, the Windows 8 start screen supports a tiled layout, where each tile represents an application installed on the machine. Unlike a typical Windows desktop application, Windows 8 applications are built for touch-screen interactions. They are also run in full-screen renderings and lack the usual “chrome” (e.g., menu systems, status bars, and toolbar buttons) we see in many desktop applications. Figure 1-12 shows a partial view of a Windows 8 weather application. CHAPTER 1  THE PHILOSOPHY OF .NET 34 Figure 1-12. Windows 8 applications are full-screeen, graphically rich desktop applications  Note Building a Windows 8 application requires developers to adhere to an entirely new set of UI design guidelines, data storage methodologies, and user input options. To be sure, a “Win8” app is much more than a tile installed on the Windows 8 start screen. Building Windows 8 Applications Creating and running a Windows 8 application is only possible on Windows 8 and is not supported under Windows 7. In fact, if you install Visual Studio on Windows 7 (or earlier), you will not even see Windows 8 project templates appear in the New Project dialog box. Programming a Windows 8 application requires developers to tap into an entirely new runtime layer termed (appropriately enough) Windows Runtime (WinRT). Be very aware that WinRT is not the .NET CLR, however it does offer some similar services such as garbage collection, support for multiple programming languages (including C#), and so forth. CHAPTER 1  THE PHILOSOPHY OF .NET 35 In addition, these applications are created using a completely new set of namespaces, all of which begin with the root name Windows. Figure 1-13 shows various WinRT namespaces displayed in the Visual Studio object browser. Figure 1-13. The Windows.* namespaces are only used to build Windows 8 applications As luck would have it, the Windows.* namespaces offer functionality that mirrors many APIs of the .NET base class libraries. In fact, from a programming point of view, building an application for WinRT feels very similar to building a .NET application for the CLR. For example, Windows 8 applications can be constructed using C# (as well as with Visual Basic, JavaScript, or C++). As well, many of the Windows.* namespaces offer similar (though not identical) functionality as is found in the .NET base class libraries. By way of one example, later in this text (see Chapters 27–31), you will examine a .NET technology termed Windows Presentation Foundation (WPF). Here, you will learn about an XML-based grammar named XAML. Should you begin to explore how to Windows 8 applications, you will be happy to see that they are created using XAML as well (or possibly using HTML5, if your language of choice is JavaScript), and have a very similar programming model to a WPF program constructed with the .NET platform. The Role of .NET Under Windows 8 In addition to the Windows.* namespaces, Windows 8 applications can also make use of a large subset of the .NET platform. Collectively, the .NET APIs for Windows 8 applications, provide support for generic collections, LINQ, XML document processing, I/O services, security services, and other topics explored CHAPTER 1  THE PHILOSOPHY OF .NET 36 in this book. This is certainly a good thing, as many topics explored in this book will map directly to the construction of a Windows 8 application. Also be aware that even though the classic Windows desktop is not the default display under Windows 8, it is still accessible by clicking on the Desktop tile or simply pressing the Windows button on your computer keyboard. Once opened, the OS will display a desktop that looks almost identical to the Windows 7 desktop (see Figure 1-14). Figure 1-14. The desktop of Windows 8 allows you to run non-Windows 8 applications Here you are able to run all of your expected desktop applications including Visual Studio, Word, Excel, Photoshop and whatnot. As well, if you are building .NET applications under Windows 8, the applications will run within the Windows 8 desktop environment. So, the short answer is that the Microsoft .NET platform is alive and well under Windows 8, and will be a key part of your development efforts. Just keep in mind that .NET proper is not used to build Windows 8 applications. Rather, you will make use of new libraries (Windows.*), a new runtime (WinRT), and a subset of .NET proper (the .NET APIs) to create such a program. This text will not examine the process of building Windows 8 applications using WinRT, but will focus exclusively on the .NET platform. However, do keep in mind that a majority of topics examined in this text will most certainly prepare you to explore Windows 8 application development in the future, should you have the need. CHAPTER 1  THE PHILOSOPHY OF .NET 37 Summary The point of this chapter was to lay out the conceptual framework necessary for the remainder of this book. I began by examining a number of limitations and complexities found within the technologies prior to .NET, and followed up with an overview of how .NET and C# attempt to simplify the current state of affairs. .NET basically boils down to a runtime execution engine (mscoree.dll) and base class library (mscorlib.dll and associates). The Common Language Runtime (CLR) is able to host any .NET binary (a.k.a. assembly) that abides by the rules of managed code. As you have seen, assemblies contain CIL instructions (in addition to type metadata and the assembly manifest) that are compiled to platform- specific instructions using a just-in-time (JIT) compiler. In addition, you explored the role of the Common Language Specification (CLS) and Common Type System (CTS). This was followed by an examination of the ildasm.exe and reflector.exe object browsing utilities, as well as coverage of how to configure a machine to host .NET applications using the full and client profiles. I wrapped up by briefly addressing the platform-independent nature of C# and the .NET platform (a topic further examined in Appendix A) and how the .NET platform is positioned within the Windows 8 operating system.C H A P T E R 2 39 Building C# Applications As a C# programmer, you may choose among numerous tools to build .NET applications. The point of this chapter is to provide a tour of various .NET development options, including, of course, Visual Studio proper. The chapter opens, however, with an examination of working with the C# command-line compiler, csc.exe, and the simplest of all text editors, the Notepad application that ships with the Microsoft Windows OS, as well as the freely downloadable Notepad++. While you could work through this entire text using nothing other than csc.exe and a basic text editor, I’d bet you are also interested in working with feature-rich integrated development environments (IDEs). To address this need, you will be introduced to a free, open source .NET IDE named SharpDevelop. Then, after briefly examining the Visual C# Express IDE (which is also free), you will be given a guided tour of the key features of Visual Studio Professional.  Note Over the course of this chapter, you will encounter some C# syntax we have not formally examined. If you are unfamiliar with the syntax, don’t fret. Chapter 3 will formally begin your examination of the C# language. The Role of the .NET Framework 4.5 SDK One common misconception regarding .NET development is the belief that programmers must purchase a copy of Visual Studio in order to build their C# applications. The truth of the matter is that you are able to build any sort of .NET program using the freely downloadable .NET Framework 4.5 Software Development Kit (SDK). This SDK provides you with numerous managed compilers, command-line utilities, sample code, the .NET class libraries, and a complete documentation system. Now, be aware that if you are going to be using Visual Studio proper or Visual C# Express, you have no need to manually download or install the .NET Framework 4.5 SDK. When you install either of these products, the SDK is installed automatically, thereby giving you everything you need out of the box. However, if you are not going to be using a Microsoft IDE as you work through this text, be sure to install the SDK before proceeding.  Note The .NET Framework 4.5 SDK setup program (dotNetFx45_Full_x86_x64.exe) can be obtained from the .NET download web site (http://msdn.microsoft.com/netframework). CHAPTER 2  BUILDING C# APPLICATIONS 40 The Developer Command Prompt When you install the .NET Framework 4.5 SDK, Visual Studio, or Visual C# Express, you will end up with a number of new directories on your local hard drive, each of which contains various .NET development tools. Many of these tools are driven from the command prompt, so if you wish to use these utilities from any Windows command window, you will need to register these paths with the operating system. While you could update your machine’s PATH variable manually to do so, you can save yourself some time by simply making use of the Developer Command Prompt (see Figure 2-1) that is accessible from the Start  All Programs  Microsoft Visual Studio 11  Visual Studio Tools folder. Figure 2-1. The Developer Command Prompt The benefit of using this particular command prompt is that it has been preconfigured to provide access to each of the .NET development tools. Assuming you have a .NET development environment installed, type the following command and press the Enter key: csc -? If all is well, you should see a list of command-line arguments of the C# command-line compiler (where csc stands for the C-sharp compiler). As you can see, this command-line compiler has a bevy of options; however, in practice you will only need to make use of a handful of settings to build C# programs at the command prompt. Building C# Applications Using csc.exe While it is true that you might never decide to build a large-scale application using nothing but the C# command-line compiler, it is important to understand the basics of how to compile your code files by hand. I can think of a few reasons you should get a grip on the process. • The most obvious reason is the simple fact that you might not have a copy of Visual Studio or another graphical IDE. • You might be in a college or university setting where you are prohibited from using code-generation tools/IDEs in the classroom. • You plan to make use of automated build tools, such as msbuild.exe, which require you to know the command-line options of the tools you are utilizing. • You want to deepen your understanding of C#. When you use graphical IDEs to build applications, you are ultimately instructing csc.exe how to manipulate your C# input files. In this light, it’s edifying to see what takes place behind the scenes. CHAPTER 2  BUILDING C# APPLICATIONS 41 Another nice by-product of working with csc.exe in the raw is that you become that much more comfortable manipulating other command-line tools included with the .NET Framework 4.5 SDK. As you will see throughout this book, a number of important utilities are accessible only from the command line (such as gacutil.exe, ngen.exe, ilasm.exe, and aspnet_regiis.exe). To illustrate how to build a .NET application IDE-free, we will build a simple executable assembly named TestApp.exe using the C# command-line compiler and Notepad. First, you need some source code. Open Notepad (using the Start  All Programs  Accessories menu option) and enter the following trivial C# class definition: // A simple C# application. using System; class TestApp { static void Main() { Console.WriteLine(“Testing! 1, 2, 3“); } } After you have finished, save the file in a convenient location (e.g., C:\CscExample) as TestApp.cs. Now let’s get to know the core options of the C# compiler.  Note C# code files take a *.cs file extension. Unlike Java, the name of the file need not have any mapping to the name of the type (or types) it is defining. Specifying Input and Output Targets The first point of interest is to understand how to specify the name and type of assembly to create (e.g., a console application named MyShell.exe, a code library named MathLib.dll, a Windows Presentation Foundation application named Halo8.exe, and so forth). Each possibility is represented by a specific flag passed into csc.exe as a command-line parameter (see Table 2-1). Table 2-1. Common Output Options of the C# Compiler Option Meaning in Life /out This option is used to specify the name of the assembly to be created. By default, the assembly name is the same as the name of the initial input *.cs file. /target:exe This option builds an executable console application. This is the default assembly output type, and thus may be omitted when building this type of application. /target:library This option builds a single-file *.dll assembly. CHAPTER 2  BUILDING C# APPLICATIONS 42 Option Meaning in Life /target:winexe Although you are free to build graphical user interface–based applications using the /target:exe option, /target:winexe prevents a console window from appearing in the background.  Note The options sent to the command-line compiler (as well as most other command-line tools) can be prefixed with either a dash (-) or a slash (/). To compile TestApp.cs into a console application named TestApp.exe, change to the directory containing your source code file using the cd (“change directory”) command: cd C:\CscExample Then, enter the following command set (note that command-line flags must come before the name of the input files, not after): csc /target:exe TestApp.cs Here I did not explicitly specify an /out flag; therefore, the executable will be named TestApp.exe given that TestApp is the name of the input file. Also be aware that most of the C# compiler flags support an abbreviated version, such as /t rather than /target (you can view all abbreviations by entering csc -? at the command prompt). csc /t:exe TestApp.cs Furthermore, given that the /t:exe flag is the default output used by the C# compiler, you could also compile TestApp.cs simply by typing the following: csc TestApp.cs TestApp.exe can now be run from the command line by typing the name of the executable, as shown in Figure 2-2. Figure 2-2. Compiling and running TestApp.exe CHAPTER 2  BUILDING C# APPLICATIONS 43 Referencing External Assemblies Next, let’s examine how to compile an application that makes use of types defined in a separate .NET assembly. And just in case you are wondering how the C# compiler understood your reference to the System.Console type, recall from Chapter 1 that mscorlib.dll is automatically referenced during the compilation process (if for some strange reason you wish to disable this feature, you may specify the /nostdlib option of csc.exe). Let’s update the TestApp application to display a Windows Forms message box. Open your TestApp.cs file and modify it as follows: using System; // Add this! using System.Windows.Forms; class TestApp { static void Main() { Console.WriteLine(“Testing! 1, 2, 3“); // Add this! MessageBox.Show(“Hello...“); } } Notice you are importing the System.Windows.Forms namespace via the C# using keyword (introduced in Chapter 1). Recall that when you explicitly list the namespaces used within a given *.cs file, you avoid the need to make use of fully qualified names of a type (which can lead to hand cramps). At the command line, you must inform csc.exe which assembly contains the namespaces you are using. Given that you have made use of the System.Windows.Forms.MessageBox class, you must specify the System.Windows.Forms.dll assembly using the /reference flag (which can be abbreviated to /r). csc /r:System.Windows.Forms.dll TestApp.cs If you now rerun your application, you should see a message box appear (see Figure 2-3) in addition to the console output. Figure 2-3. Your first graphical user interface application CHAPTER 2  BUILDING C# APPLICATIONS 44 Referencing Multiple External Assemblies On a related note, what if you need to reference numerous external assemblies using csc.exe? Simply list each assembly using a semicolon-delimited list. You don’t need to specify multiple external assemblies for the current example, but some sample usage follows: csc /r:System.Windows.Forms.dll;System.Drawing.dll *.cs  Note As explained a bit later in this chapter, the C# compiler will automatically reference a set of core .NET assemblies (such as System.Windows.Forms.dll), even if they are not specified using the /r flag. Compiling Multiple Source Files The current incarnation of the TestApp.exe application was created using a single *.cs source code file. While it is perfectly permissible to have all of your .NET types defined in a single *.cs file, most projects are composed of multiple *.cs files to keep your code base a bit more flexible. Assume you have authored a new class contained in a new file named HelloMsg.cs. // The HelloMessage class using System; using System.Windows.Forms; class HelloMessage { public void Speak() { MessageBox.Show(“Hello...“); } } Now, update your initial TestApp class to make use of this new class type and comment out the previous Windows Forms logic. using System; // Don't need this anymore. // using System.Windows.Forms; class TestApp { static void Main() { Console.WriteLine(“Testing! 1, 2, 3“); // Don't need this anymore either. // MessageBox.Show(“Hello...“); CHAPTER 2  BUILDING C# APPLICATIONS 45 // Use the HelloMessage class! HelloMessage h = new HelloMessage(); h.Speak(); } } You can compile your C# files by listing each input file explicitly. csc /r:System.Windows.Forms.dll TestApp.cs HelloMsg.cs As an alternative, the C# compiler allows you to make use of the wildcard character (*) to inform csc.exe to include all *.cs files contained in the project directory as part of the current build. csc /r:System.Windows.Forms.dll *.cs When you run the program again, the output is identical to the previous compiled code. The only difference between the two applications is the fact that the current logic has been split among multiple files. Working with C# Response Files As you might guess, if you were to build a complex C# application at the command prompt, you would have to specify a tedious number of input options to inform the compiler how to process your source code. To help lessen your typing burden, the C# compiler honors the use of response files. C# response files contain all the instructions to be used during the compilation of your current build. By convention, these files end in a *.rsp (response) extension. Assume that you have created a response file named TestApp.rsp that contains the following options (as you can see, comments are denoted with the # character): # This is the response file # for the TestApp.exe example # of Chapter 2. # External assembly references. /r:System.Windows.Forms.dll # output and files to compile (using wildcard syntax). /target:exe /out:TestApp.exe *.cs Now, assuming this file is saved in the same directory as the C# source code files to be compiled, you are able to build your entire application as follows (note the use of the @ symbol): csc @TestApp.rsp If the need should arise, you can also specify multiple *.rsp files as input (e.g., csc @FirstFile.rsp @SecondFile.rsp @ThirdFile.rsp). If you take this approach, be aware that the compiler processes the command options as they are encountered. Therefore, command-line arguments in a later *.rsp file can override options in a previous response file. Also note that flags listed explicitly on the command line before a response file will be overridden by the specified *.rsp file. Thus, if you were to enter the following: csc /out:MyCoolApp.exe @TestApp.rsp CHAPTER 2  BUILDING C# APPLICATIONS 46 the name of the assembly would still be TestApp.exe (rather than MyCoolApp.exe), given the /out:TestApp.exe flag listed in the TestApp.rsp response file. However, if you list flags after a response file, the flag will override settings in the response file.  Note The effect of the /reference flag is cumulative. Regardless of where you specify external assemblies (before, after, or within a response file), the end result is a summation of each reference assembly. The Default Response File (csc.rsp) The final point to be made regarding response files is that the C# compiler has an associated default response file (csc.rsp), which is located in the same directory as csc.exe itself (which is by default installed under C:\Windows\Microsoft.NET\Framework\, where is a given version of the platform). If you were to open this file using Notepad, you will find that numerous .NET assemblies have already been specified using the /r: flag, including various libraries for web development, LINQ programming, data access, and other core libraries (beyond mscorlib.dll). When you are building your C# programs using csc.exe, this response file will be automatically referenced, even when you supply a custom *.rsp file. Given the presence of the default response file, the current TestApp.exe application could be successfully compiled using the following command set (as System.Windows.Forms.dll is referenced within csc.rsp): csc /out:TestApp.exe *.cs In the event that you wish to disable the automatic reading of csc.rsp, you can specify the /noconfig option. csc @TestApp.rsp /noconfig  Note If you reference assemblies (via the /r option) that you do not actually make use of, they are ignored by the compiler. Therefore, you have no need to worry about “code bloat.” If you reference a library you do not actually make use of, it is ignored by the compiler. Obviously, the C# command-line compiler has many other options that can be used to control how the resulting .NET assembly is to be generated. You’ll see other important features where necessary over the course of this text; however, full details of these options can be found within the .NET Framework 4.5 SDK documentation.  Source Code The CscExample application can be found under the Chapter 2 subdirectory. CHAPTER 2  BUILDING C# APPLICATIONS 47 Building .NET Applications Using Notepad++ Another simple text editor I’d like to quickly point out is the freely downloadable Notepad++ application. This tool can be obtained from http://notepad-plus.sourceforge.net. Unlike the primitive Windows Notepad application, Notepad++ allows you to author code in a variety of languages and supports various plug-in utilities. In addition, Notepad++ provides a few other niceties, including the following: • Out-of-the-box support for C# keywords (including keyword color coding) • Support for syntax folding, which allows you to collapse and expand groups of code statements within the editor (similar to Visual Studio /C# Express) • The ability to zoom in/zoom out text via Ctrl-mouse wheel • Configurable autocompletion for a variety of C# keywords and .NET namespaces Regarding this last point, the Ctrl+space keyboard combination will activate C# autocompletion support (see Figure 2-4). Figure 2-4. Autocompletion using Notepad++  Note The options displayed by the autocomplete window can be modified and extended. Simply open up the C:\Program Files (x86)\Notepad++\plugins\APIs\cs.xml file for editing, and add any additional entries. I won’t go into too many details of Notepad++ beyond what we have examined here. If you require more assistance, select the Help content menu option. CHAPTER 2  BUILDING C# APPLICATIONS 48 Building .NET Applications Using SharpDevelop As you might agree, authoring C# code with Notepad++ is a step in the right direction, compared to Notepad. However, these tools do not provide rich IntelliSense capabilities for C# code, designers for building graphical user interfaces, project templates, or database manipulation utilities. To address such needs, allow me to introduce the next .NET development option: SharpDevelop (also known as #Develop). SharpDevelop is an open source and feature-rich IDE that you can utilize to build .NET assemblies using C#, VB, Iron Ruby, Iron Python, C++ F#, or a Python-inspired .NET language named Boo. Beyond the fact that this IDE is completely free, it is interesting to note that it was written entirely in C#. In fact, you have the choice to download and compile the *.cs files manually or run a setup.exe program to install SharpDevelop on your development machine. Both distributions can be obtained from http://www.sharpdevelop.com. SharpDevelop provides numerous productivity enhancements. Here is a hit list of some of the major benefits. • Support for multiple .NET languages, .NET versions and project types • IntelliSense, code completion, and code snippet capabilities • An Add Reference dialog box to reference external assemblies, including assemblies deployed to the global assembly cache (GAC) • Integrated GUI designers for desktop and web applications • Integrated object browsing and code definition utilities • Visual database designer utilities • A C#-to-VB (and vice versa) code-conversion utility Impressive for a free IDE, is it not? Although this chapter doesn’t cover each of these points in detail, let’s walk through a few items of interest.  Note The current version of SharpDevelop (4.2) does support all C# or .NET 4.5 features. Be sure to check the SharpDevelop web site for future releases. Building a Simple Test Project After you have installed SharpDevelop, the File  New  Solution menu option allows you to pick which type of project you wish to generate (and in which .NET language). For example, assume you have created a C# Windows Application solution named MySDWinApp (see Figure 2-5). CHAPTER 2  BUILDING C# APPLICATIONS 49 Figure 2-5. The SharpDevelop New Project dialog box Like Visual Studio, you have a Windows Forms GUI designer toolbox (to drag and drop controls onto the designer), and a Properties window to set up the look and feel of each UI item. Figure 2-6 illustrates configuring a button control using the IDE (note that I clicked on the Design tab mounted on the bottom of the opened code file). CHAPTER 2  BUILDING C# APPLICATIONS 50 Figure 2-6. Graphically designing a Windows Forms Application with SharpDevelop If you were to click the Source button mounted to the bottom of the form’s designer, you would find the expected IntelliSense, code completion, and integrated help features (see Figure 2-7). CHAPTER 2  BUILDING C# APPLICATIONS 51 Figure 2-7. SharpDevelop supports numerous code-generation utilities SharpDevelop was designed to mimic much of the same functionality found within Microsoft’s .NET IDEs (which we will examine next). Given this point, I won’t dive into all of the features of this open source .NET IDE. If you require more information, simply use the provided Help menu.  Note MonoDevelop is an open source IDE based on the SharpDevelop code base. MonoDevelop is the preferred IDE for .NET programmers building applications for Mac OS X or Linux operating systems using the Mono platform. Consult http://monodevelop.com/ for more details on this IDE. Building .NET Applications Using Visual C# Express Microsoft provides a line of IDEs that fall under the designation of Express products (http://msdn.microsoft.com/express). To date, there are various members of the Express family (all of which are completely free and supported and maintained by Microsoft Corporation), including the following: CHAPTER 2  BUILDING C# APPLICATIONS 52 • Visual Web Developer Express: A lightweight tool for building dynamic ASP.NET web sites and WCF services • Visual Basic Express: A streamlined programming tool ideal for novice programmers who want to learn how to build applications using the user-friendly syntax of Visual Basic • Visual C# Express and Visual C++ Express: Targeted IDEs for students and enthusiasts who wish to learn the fundamentals of computer science in their syntax of choice • SQL Server Express: An entry-level, database management system geared toward hobbyists, enthusiasts, and student developers Some Unique Features of Visual C# Express By and large, the Express products are slimmed-down versions of their Visual Studio proper counterparts and are primarily targeted at .NET hobbyists and students. Like SharpDevelop, Visual C# Express provides various object browsing tools, GUI designers for desktop applications, the Add References dialog box, IntelliSense capabilities, and code expansion templates. However, Visual C# Express offers a few (important) features currently not available in SharpDevelop, including the following: • Rich support for Windows Presentation Foundation (WPF) XAML applications • The ability to download additional free templates that support Xbox 360 development, WPF applications with Twitter integration, and much more Because the look and feel of Visual C# Express is so similar to that of Visual Studio proper (and, to some degree, SharpDevelop), I do not provide a walk-through of this particular IDE here. You are free to use this IDE as you work through the book, but do be aware that Visual C# Express does not support project templates for building ASP.NET web sites. If you wish to also build web applications, be sure to also download Visual Web Developer, also available from http://msdn.microsoft.com/express. Building .NET Applications Using Visual Studio If you are a professional .NET software engineer, the chances are extremely good that your employer has purchased Microsoft’s premier IDE, Visual Studio, for your development endeavors (http://msdn.microsoft.com/vstudio). This tool is far and away the most feature-rich, enterprise-ready IDE examined in this chapter. Of course, this power comes at a price, which will vary based on the version of Visual Studio you purchase. As you might suspect, each version supplies a unique set of features.  Note There are a staggering number of members within the Visual Studio family. My assumption during the remainder of this text is that you have chosen to make use of Visual Studio Professional as your IDE of choice. CHAPTER 2  BUILDING C# APPLICATIONS 53 Although I will assume you have a copy of Visual Studio Professional, understand that owning a copy of this IDE is not required to use this edition of the text. In the worst case, I might examine an option that is not provided by your IDE (such as Microsoft C# Express or SharpDevelop). However, rest assured that all of this book’s sample code will compile just fine when processed by your tool of choice.  Note After you download the source code for this book from the Source Code/Downloads area of the Apress web site (http://www.apress.com), you may load the current example into Visual Studio (or C# Express) by double-clicking the example’s *.sln file. If you are not using Visual Studio / C# Express, you will need to manually insert the provided *.cs files into your IDE’s project work space. Some Unique Features of Visual Studio Visual Studio ships with the expected GUI designers, code snippet support, database manipulation tools, object and project browsing utilities, and an integrated help system. Unlike many of the IDEs we have already examined, Visual Studio provides numerous additions. Here is a partial list. • Visual XML editors/designers • Support for Windows mobile device development • Support for Microsoft Office development • Designer support for Windows Workflow Foundation projects • Integrated support for code refactoring • Visual class designer tools To be completely honest, Visual Studio provides so many features that it would take an entire book (a rather large book at that) to fully describe every aspect of the IDE. This is not that book. However, I do want to point out some of the major features in the pages that follow. As you progress through the text, you’ll learn more about the Visual Studio IDE where appropriate.  Note If you have used a previous version of Microsoft Visual Studio, you will quickly notice that the entire look and feel has changed quite a bit. If you find the default “Dark” theme is a tad too dark for your liking, you can change to the “Light” theme by selecting the Tools  Options menu and picking from the Color Theme drop-down list box in the Environment  General section. All screen shots in this text are shown in the “Light” theme. CHAPTER 2  BUILDING C# APPLICATIONS 54 Targeting the .NET Framework Using the New Project Dialog Box If you are following along, create a new C# Console Application (named VsExample) using the File  New  Project menu item. As you can see in Figure 2-8, Visual Studio supports the ability to select which version of the .NET Framework you wish to build against (2.0, 3.0, 3.5, 4.0, or 4.5) using the drop-down list box on the top-center of the New Project dialog box. For each project in this text, you can simply leave the default selection of .NET Framework 4.5. Figure 2-8. Visual Studio allows you to target a particular version of the .NET Framework Using the Solution Explorer Utility The Solution Explorer utility (accessible from the View menu) allows you to view the set of all content files and referenced assemblies that comprise the current project (see Figure 2-9). Also notice, that you can expand a given file (such as Program.cs) to see the code types defined within the file in question. As you move through this text, I’ll point out other useful features of the Solution Explorer where necessary; however, feel free to take some time to tinker with each option, if you so choose. CHAPTER 2  BUILDING C# APPLICATIONS 55 Figure 2-9. The Solution Explorer utility Notice that the References folder of Solution Explorer displays a list of each assembly you have currently referenced, which will differ based on the type of project you select and the version of the Framework you are compiling against. Because you have created a Console Application, you will find a minimal set of libraries is automatically included (such as System.dll, System.Core.dll, System.Data.dll, and so forth).  Note Recall from Chapter 1 that the primary .NET library is named mscorlib.dll. This library will not be shown in the Solution Explorer; however, you have access to all the contained types regardless. Referencing External Assemblies When you need to reference additional assemblies, right-click the References folder and select Add Reference. At this point, you can select your assembly from the resulting dialog box (this is essentially the way Visual Studio allows you to specify the /reference option of the command-line compiler). The Framework tab (see Figure 2-10) displays a number of commonly used .NET assemblies; however, the Browse tab allows you to navigate to any .NET assembly on your hard drive. Also, the very useful Recent tab keeps a running tally of frequently referenced assemblies you have used in other projects. CHAPTER 2  BUILDING C# APPLICATIONS 56 Figure 2-10. The Add Reference dialog box By way of a simple test, locate the System.Windows.Forms.dll assembly from the Framework area, and check the related check box. After you do so (and close the dialog), you will see this library appear under the References folder of your Solution Explorer. You can delete this reference by selecting it in the Solution Explorer and pressing the Delete key on your keyboard (or via the Delete menu option of a right mouse click). Viewing Project Properties Finally, notice an icon named Properties within Solution Explorer. When you double-click this item, you are presented with a sophisticated project configuration editor (see Figure 2-11). CHAPTER 2  BUILDING C# APPLICATIONS 57 Figure 2-11. The Project Properties window You will see various aspects of the Project Properties window as you progress through this book. However, if you take some time to poke around, you will see that you can establish various security settings, strongly name your assembly (see Chapter 14), deploy your application, insert application resources, and configure pre- and post-build events. The Class View Utility The next tool to examine is the Class View utility, which you can load from the View menu. The purpose of this utility is to show all of the types in your current project from an object-oriented perspective (rather than the default file-based view of Solution Explorer). The top pane displays the set of namespaces and their types, while the bottom pane displays the currently selected type’s members (see Figure 2-12). CHAPTER 2  BUILDING C# APPLICATIONS 58 Figure 2-12. The Class View utility When you double-click on a type or type member using the Class View tool, Visual Studio will automatically open the correct C# code file, and place your mouse cursor at the correct location. Another nice feature of the Visual Studio Class View tool is that you are able to open up any referenced assembly and drill into the contained namespaces, types, and members. The Object Browser Utility Visual Studio also provides a second utility to investigate the set of referenced assemblies within your current project. Activate the Object Browser using the View menu, and then select the assembly you wish to investigate (see Figure 2-13). CHAPTER 2  BUILDING C# APPLICATIONS 59 Figure 2-13. The Object Browser utility Integrated Support for Code Refactoring One major feature that ships with Visual Studio is support to refactor existing code. Simply put, refactoring is a formal and mechanical process whereby you improve an existing code base. In the bad old days, refactoring typically involved a ton of manual labor. Luckily, Visual Studio does a great deal to automate the refactoring process by supporting a number of commonly used techniques. Using the Refactor menu (which will only be available when a code file is open in the IDE for editing), related keyboard shortcuts, smart tags, and/or context-sensitive mouse clicks, you can dramatically reshape your code with minimal fuss and bother. Table 2-2 defines some common refactorings recognized by Visual Studio. Table 2-2. Visual Studio Refactorings Refactoring Technique Meaning in Life Extract Method Allows you to define a new method based on a selection of code statements Encapsulate Field Turns a public field into a private field encapsulated by a C# property Extract Interface Defines a new interface type based on a set of existing type members Reorder Parameters Provide a way to reorder member arguments CHAPTER 2  BUILDING C# APPLICATIONS 60 Refactoring Technique Meaning in Life Remove Parameters Remove a given argument from the current list of parameters (as you would expect) Rename Allows you to rename a code token (method name, field, local variable, and so on) throughout a project To illustrate refactoring in action, update your Main() method with the following simple code: static void Main(string[] args) { // Set up Console UI (CUI) Console.Title = “My Rocking App“; Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine(“*************************************“); Console.WriteLine(“***** Welcome to My Rocking App *****“); Console.WriteLine(“*************************************“); Console.BackgroundColor = ConsoleColor.Black; // Wait for Enter key to be pressed. Console.ReadLine(); } While there is nothing wrong with the preceding code as it now stands, imagine that you want to display this welcome message at various places throughout your program. Rather than retyping the same exact console user interface logic, it would be ideal to have a helper function that could be called to do so. Given this, you will apply the Extract Method refactoring to your existing code. First, select each code statement within Main() (except the final call to Console.ReadLine()) using the editor. Then right-click the selected text and select the Extract Method option within the Refactor context menu (see Figure 2-14). CHAPTER 2  BUILDING C# APPLICATIONS 61 Figure 2-14. Activating a code refactoring Name your new method ConfigureCUI using the resulting dialog box. When you have finished, you will find that your Main() method calls the newly generated ConfigureCUI() method, which now contains the previously selected code. class Program { static void Main(string[] args) { ConfigureCUI(); // Wait for key press to close. Console.ReadLine(); } private static void ConfigureCUI() { // Set up Console UI (CUI) Console.Title = “My Rocking App“; Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine(“*************************************“); Console.WriteLine(“***** Welcome to My Rocking App *****“); Console.WriteLine(“*************************************“); Console.BackgroundColor = ConsoleColor.Black; } } CHAPTER 2  BUILDING C# APPLICATIONS 62 This is a simple example of using the built-in refactorings of Visual Studio, and you’ll see additional examples here and there over the course of this text. However, feel free to activate the remaining refactoring options to see their net effect firsthand (remember, we are not going to use the current VsExample project for anything later in the book, so hack away). Code Snippets and Surround with Technology Visual Studio (as well as Visual C# Express) is capable of inserting prefabricated blocks of C# code using menu selections, context-sensitive mouse clicks, and/or keyboard shortcuts. The number of available code expansions is impressive and can be broken down into two main groups. • Snippets: These templates insert common code blocks at the location of the mouse cursor. • Surround With: These templates wrap a block of selected statements within a relevant scope. To see this functionality firsthand, assume that you wish to iterate over the incoming parameters of the Main() method using a foreach construct. Rather than typing the code in by hand, you can activate the foreach code snippet. When you have done so, the IDE will dump out a foreach code template at the current location of the mouse cursor. To illustrate, place the mouse cursor after the initial opening curly bracket of Main(). One way to activate a code snippet is to right-click the mouse and activate the Insert Snippet (or Surround With) menu option. Here, you will find a list of all code snippets of this category (press the Esc key to dismiss the pop-up menu). As a shortcut, however, you can simply type in the name of the code snippet, foreach in this case. In Figure 2-15, notice how the icon for a code snippet looks a bit like a torn piece of paper. Figure 2-15. Activating a code snippet CHAPTER 2  BUILDING C# APPLICATIONS 63 After you find the snippet you want to activate, press the Tab key twice. This will autocomplete the entire snippet and leave you with a set of placeholders that you can fill in to complete the snippet. If you press the Tab key, you can cycle between each placeholder and fill in the gaps (press the Esc key to exit the code snippet edit mode). For our example, we can change the first placeholder (the var keyword) with the string data type; the second placeholder (the item variable name) with arg; and the final placeholder with the name of the incoming string array parameter, leaving us with the following: static void Main(string[] args) { foreach (string arg in args) { } ... } If you were to right-click in an open C# code file and select the Surround With menu, you would likewise be presented with a list of options. When using Surround With snippets, typically you first select a block of code statements to represent what should be used to wrap them (e.g., try/catch block). Be sure to take time to explore these predefined code expansion templates, as they can radically speed up the development process.  Note All code expansion templates are XML-based descriptions of the code to generate within the IDE. Using Visual Studio (as well as Visual C# Express), you can create your own custom code templates. Details of how to do so can be found in my article “Investigating Code Snippet Technology” at http://msdn.microsoft.com /en-US/library/ms379562. The Visual Class Designer Visual Studio gives you the ability to design classes and other types (such as interfaces or delegates) in a visual manner (this functionality is not included in Visual C# Express). The Class Designer utility allows you to view and modify the relationships of the types (classes, interfaces, structures, enumerations, and delegates) in your project. Using this tool, you are able to visually add (or remove) members to (or from) a type and have your modifications reflected in the corresponding C# file. Also, as you modify a given C# file, changes are reflected in the class diagram. To work with this aspect of Visual Studio, the first step is to insert a new class diagram file. There are many ways to do so, one of which is to click the View Class Diagram button located on Solution Explorer’s right side (see Figure 2-16; be sure your project—not the containing solution—is the selected item in the window). CHAPTER 2  BUILDING C# APPLICATIONS 64 Figure 2-16. Inserting a class diagram file into the current project After you do so, you will find class icons that represent the types found in your current project. If you click the arrow icon for a given type, you can show or hide the type’s members (see Figure 2-17).  Note Using the Class Designer toolbar, you can fine-tune the display options of the designer surface. Figure 2-17. The Class Diagram viewer The Class Designer utility works in conjunction with two other aspects of Visual Studio: the Class Details window (activated using the View  Other Windows menu) and the Class Designer Toolbox CHAPTER 2  BUILDING C# APPLICATIONS 65 (activated using the View  Toolbox menu item). The Class Details window not only shows you the details of the currently selected item in the diagram, but also allows you to modify existing members and insert new members on the fly (see Figure 2-18). Figure 2-18. The Class Details window The Class Designer Toolbox, which can also be activated using the View menu, allows you to insert new types (and create relationships between these types) into your project visually (see Figure 2-19). (Be aware you must have a class diagram as the active window to view this toolbox.) As you do so, the IDE automatically creates new C# type definitions in the background. Figure 2-19. The Class Designer Toolbox CHAPTER 2  BUILDING C# APPLICATIONS 66 By way of example, drag a new Class from the Class Designer Toolbox onto your Class Designer. Name this class Car in the resulting dialog box. Now, using the Class Details window, add a public string field named petName (see Figure 2-20). Figure 2-20. Adding a field with the Class Details window If you now look at the C# definition of the Car class, you will see it has been updated accordingly (minus the additional code comments seen below). public class Car { // Public data is typically a bad idea; however, // it keeps this example simple. public string petName; } Now, activate the designer file once again and drag another new Class onto the designer and name it SportsCar. Now, select the Inheritance icon from the Class Designer Toolbox and click the top of the SportsCar icon. Next, click the mouse on top of the Car class icon. If you performed these steps correctly, you have just derived the SportsCar class from Car (see Figure 2-21). Figure 2-21. Visually deriving from an existing class CHAPTER 2  BUILDING C# APPLICATIONS 67 To complete this example, update the generated SportsCar class with a public method named GetPetName(), authored as follows: public class SportsCar : Car { public string GetPetName() { petName = “Fred“; return petName; } } You will make use of these (and other) visual tools of Visual Studio over the course of this book. However, you should now feel a bit more comfortable regarding the basics of the IDE.  Note The concept of inheritance will be fully examined in Chapter 6. The Integrated .NET Framework 4.5 SDK Documentation System The final aspect of Visual Studio you must be comfortable with from the outset is the fully integrated help system. The .NET Framework 4.5 SDK documentation is extremely good, very readable, and full of useful information. Given the huge number of predefined .NET types (which number well into the thousands), you must be willing to roll up your sleeves and dig into the provided documentation. If you resist, you are doomed to a long, frustrating, and painful existence as a .NET developer. If you have an Internet connection, you can view the .NET Framework 4.5 SDK documentation online at the following web address: http://msdn.microsoft.com/library From here, use the tree view navigation on the main page to navigate to the .NET Development area, and select .NET Framework 4.5. After you have done so, you will find the all-important .NET Framework Class Library link, which documents every single type in every single .NET namespace (Figure 2-22). CHAPTER 2  BUILDING C# APPLICATIONS 68 Figure 2-22. Viewing the .NET Framework documentation online During the installation of Visual Studio, you will be given the option to install the same help system locally to your computer (which can be very helpful when you don’t have an active Internet connection). If you wish to perform a local installation of the help system after the fact, navigate to the All Programs  Microsoft Visual Studio 11  Microsoft Help Viewer tool using your Windows Start button. After you have done so, you can then elect to add the help documentation you are interested in using the Manage Content tab, as seen in Figure 2-23 (if hard drive space allows, I’d recommend adding all possible documentation). CHAPTER 2  BUILDING C# APPLICATIONS 69 Figure 2-23. The Help Library Manager allows you to download a local copy of the .NET Framework 4.5 SDK Documentation Regardless if you have installed the help system locally or are going online, the simplest way to interact with the documentation is to highlight a C# keyword, type name or member name within the Visual Studio code window, and press the F1 key. This will automatically open a documentation window for the selected item. For example, select the string keyword within your Car class definition. After you press F1, you will see the Help page for the string type appear. Another useful aspect of the documentation is the Search tab of the display. Here you can enter in the name of any namespace, type, or member and navigate to the correct location. If you were to try to search for the System.Reflection namespace, you would be able to learn about the details of this namespace, examine the contained types, view code examples, and so forth. CHAPTER 2  BUILDING C# APPLICATIONS 70  Note At the risk of sounding like a broken record, I really can’t emphasize enough how important it is that you learn to use the .NET Framework 4.5 SDK documentation. No book, no matter how lengthy, can cover every aspect of the .NET platform. Make sure you take some time to get comfortable using the help system—you’ll thank yourself later. Summary So as you can see, you have many new toys at your disposal! The point of this chapter was to provide you with a tour of the major programming tools a C# programmer may leverage during the development process. You began the journey by learning how to generate .NET assemblies using nothing other than the free C# compiler (csc.exe) and Notepad/Notepad++ applications, and were walked through the process of using these tools to edit and compile *.cs code files. You were also introduced three feature-rich IDEs, starting with the open source SharpDevelop, followed by Microsoft’s Visual C# Express and Visual Studio Professional. While this chapter only scratched the surface of each tool’s functionality, you should be in a good position to explore your chosen IDE at your leisure (and remember that you’ll see additional features of Visual Studio as you progress through the book).P A R T I I 1 Core C# Programming C H A P T E R 3 73 Core C# Programming Constructs, Part I This chapter begins your formal investigation of the C# programming language by presenting a number of bite-sized, stand-alone topics you must be comfortable with as you explore the .NET Framework. The first order of business is to understand how to build your program’s application object and to examine the composition of an executable program’s entry point: the Main() method. Next, you will investigate the fundamental C# data types (and their equivalent types in the System namespace) including an examination of the System.String and System.Text.StringBuilder classes. After you know the details of the fundamental .NET data types, you will then examine a number of data type conversion techniques, including narrowing operations, widening operations, and the use of the checked and unchecked keywords. This chapter will also examine the role of the C# var keyword, which allows you to implicitly define a local variable. As you will see later in this book, implicit typing is extremely helpful, if not occasionally mandatory, when working with the LINQ technology set. We wrap up this chapter by quickly examining the C# keywords and operators which allow you to control the flow of an application using various looping and decision constructs. The Anatomy of a Simple C# Program C# demands that all program logic be contained within a type definition (recall from Chapter 1 that type is a general term referring to a member of the set {class, interface, structure, enumeration, delegate}). Unlike many other languages, in C# it is not possible to create global functions or global points of data. Rather, all data members and all methods must be contained within a type definition. To get the ball rolling, create a new Console Application project named SimpleCSharpApp. You might agree that the code within the initial Program.cs file is rather uneventful. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleCSharpApp { class Program { static void Main(string[] args) CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 74 { } } } Given this, update the Main() method of your Program class with the following code statements: class Program { static void Main(string[] args) { // Display a simple message to the user. Console.WriteLine(“***** My First C# App *****“); Console.WriteLine(“Hello World!“); Console.WriteLine(); // Wait for Enter key to be pressed before shutting down. Console.ReadLine(); } }  Note C# is a case-sensitive programming language. Therefore, Main is not the same as main, and Readline is not the same as ReadLine. Be aware that all C# keywords are lowercase (e.g., public, lock, class, dynamic), while namespaces, types, and member names begin (by convention) with an initial capital letter and have capitalized the first letter of any embedded words (e.g., Console.WriteLine, System.Windows.MessageBox, System.Data.SqlClient). As a rule of thumb, whenever you receive a compiler error regarding “undefined symbols,” be sure to check your spelling and casing first! Here we have a definition for a class type that supports a single method named Main(). By default, Visual Studio names the class defining Main() Program; however, you are free to change this if you so choose. Every executable C# application (console program, Windows desktop program, or Windows service) must contain a class defining a Main() method, which is used to signify the entry point of the application. Formally speaking, the class that defines the Main() method is termed the application object. While it is possible for a single executable application to have more than one application object (which can be useful when performing unit tests), you must inform the compiler which Main() method should be used as the entry point via the /main option of the command-line compiler, or via the Startup Object drop- down list box, located under the Application tab of the Visual Studio project properties editor (see Chapter 2). Note that the signature of Main() is adorned with the static keyword, which will be examined in detail in Chapter 5. For the time being, simply understand that static members are scoped to the class level (rather than the object level) and can thus be invoked without the need to first create a new class instance. In addition to the static keyword, this Main() method has a single parameter, which happens to be an array of strings (string[] args). Although you are not currently bothering to process this array, this parameter may contain any number of incoming command-line arguments (you’ll see how to access CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 75 them momentarily). Finally, this Main() method has been set up with a void return value, meaning we do not explicitly define a return value using the return keyword before exiting the method scope. The logic of Program is within Main() itself. Here, you make use of the Console class, which is defined within the System namespace. Among its set of members is the static WriteLine() which, as you might assume, sends a text string and carriage return to the standard output. You also make a call to Console.ReadLine() to ensure the command prompt launched by the Visual Studio IDE remains visible during a debugging session until you press the Enter key. You will learn more about the System.Console class shortly. Variations on the Main() Method By default, Visual Studio will generate a Main() method that has a void return value and an array of string types as the single input parameter. This is not the only possible form of Main(), however. It is permissible to construct your application’s entry point using any of the following signatures (assuming it is contained within a C# class or structure definition): // int return type, array of strings as the parameter. static int Main(string[] args) { // Must return a value before exiting! return 0; } // No return type, no parameters. static void Main() { } // int return type, no parameters. static int Main() { // Must return a value before exiting! return 0; }  Note The Main() method may also be defined as public as opposed to private, which is assumed if you do not supply a specific access modifier. Visual Studio automatically defines a program’s Main() method as implicitly private. Obviously, your choice of how to construct Main() will be based on two questions. First, do you want to return a value to the system when Main() has completed and your program terminates? If so, you need to return an int data type rather than void. Second, do you need to process any user-supplied, command-line parameters? If so, they will be stored in the array of strings. Let’s examine all of our options in more detail. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 76 Specifying an Application Error Code While a vast majority of your Main() methods will return void as the return value, the ability to return an int from Main() keeps C# consistent with other C-based languages. By convention, returning the value 0 indicates the program has terminated successfully, while another value (such as -1) represents an error condition (be aware that the value 0 is automatically returned, even if you construct a Main() method prototyped to return void). On the Windows operating system, an application’s return value is stored within a system environment variable named %ERRORLEVEL%. If you were to create an application that programmatically launches another executable (a topic examined in Chapter 17), you can obtain the value of %ERRORLEVEL% using the static System.Diagnostics.Process.ExitCode property. Given that an application’s return value is passed to the system at the time the application terminates, it is obviously not possible for an application to obtain and display its final error code while running. However, to illustrate how to view this error level upon program termination, begin by updating the Main() method, as follows: // Note we are now returning an int, rather than void. static int Main(string[] args) { // Display a message and wait for Enter key to be pressed. Console.WriteLine(“***** My First C# App *****“); Console.WriteLine(“Hello World!“); Console.WriteLine(); Console.ReadLine(); // Return an arbitrary error code. return -1; } Now let’s capture the return value of Main() with the help of a batch file. Using Windows Explorer, navigate to the folder containing your compiled application (for example, C:\SimpleCSharpApp\bin\Debug). Add a new text file (named SimpleCSharpApp.bat) to the Debug folder that contains the following instructions (if you have not authored *.bat files before, don’t concern yourself with the details; this is a test . . . this is only a test): @echo off rem A batch file for SimpleCSharpApp.exe rem which captures the app's return value. SimpleCSharpApp @if “%ERRORLEVEL%“ == “0“ goto success :fail echo This application has failed! echo return value = %ERRORLEVEL% goto end :success echo This application has succeeded! echo return value = %ERRORLEVEL% goto end :end CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 77 echo All Done. At this point, open a Developer Command Prompt (see Chapter 2) and navigate to the folder containing your executable and new *.bat file. Execute the batch logic by typing its name and pressing the Enter key. You should find the output shown below, given that your Main() method is returning -1. Had the Main() method returned 0, you would see the message “This application has succeeded!” print to the console. ***** My First C# App ***** Hello World! This application has failed! return value = -1 All Done. Again, a vast majority (if not all) of your C# applications will use void as the return value from Main(), which, as you recall, implicitly returns the error code of zero. To this end, the Main() methods used in this text (beyond the current example) will indeed return void (and the remaining projects will certainly not need to make use of batch files to capture return codes). Processing Command-Line Arguments Now that you better understand the return value of the Main() method, let’s examine the incoming array of string data. Assume that you now wish to update your application to process any possible command- line parameters. One way to do so is using a C# for loop. (Note that C#’s iteration constructs will be examined in some detail near the end of this chapter.) static int Main(string[] args) { ... // Process any incoming args. for(int i = 0; i < args.Length; i++) Console.WriteLine(“Arg: {0}“, args[i]); Console.ReadLine(); return -1; } Here, you are checking to see whether the array of strings contains some number of items using the Length property of System.Array. As you’ll see in Chapter 4, all C# arrays actually alias the System.Array class, and therefore, share a common set of members. As you loop over each item in the array, its value is printed to the console window. Supplying the arguments at the command line is equally simple, as shown here: C:\SimpleCSharpApp\bin\Debug>SimpleCSharpApp.exe /arg1 -arg2 ***** My First C# App ***** Hello World! CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 78 Arg: /arg1 Arg: -arg2 As an alternative to the standard for loop, you may iterate over an incoming string array using the C# foreach keyword. Here is some sample usage (but again, you will see specifics of looping constructs later in this chapter). // Notice you have no need to check the size of the array when using “foreach“. static int Main(string[] args) { ... // Process any incoming args using foreach. foreach(string arg in args) Console.WriteLine(“Arg: {0}“, arg); Console.ReadLine(); return -1; } Finally, you are also able to access command-line arguments using the static GetCommandLineArgs() method of the System.Environment type. The return value of this method is an array of strings. The first index identifies the name of the application itself, while the remaining elements in the array contain the individual command-line arguments. Note that when using this approach, it is no longer necessary to define Main() as taking a string array as the input parameter, although there is no harm in doing so. static int Main(string[] args) { ... // Get arguments using System.Environment. string[] theArgs = Environment.GetCommandLineArgs(); foreach(string arg in theArgs) Console.WriteLine(“Arg: {0}“, arg); Console.ReadLine(); return -1; } Of course, it is up to you to determine which command-line arguments your program will respond to (if any) and how they must be formatted (such as with a - or / prefix). Here, we simply passed in a series of options that were printed directly to the command prompt. Assume, however, you were creating a new video game and programmed your application to process an option named -godmode. If the user starts your application with the flag, you know he is, in fact, a cheater and you can take an appropriate course of action. Specifying Command-Line Arguments with Visual Studio In the real world, an end user has the option of supplying command-line arguments when starting a program. However, during the development cycle, you might wish to specify possible command-line flags for testing purposes. To do so with Visual Studio, double-click the Properties icon from Solution Explorer and select the Debug tab on the left side. From there, specify values using the command-line arguments text box (see Figure 3-1). CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 79 Figure 3-1. Setting command arguments via Visual Studio After you have established such command-line arguments, they will automatically be passed to the Main() method when debugging or running your application within the Visual Studio IDE. An Interesting Aside: Some Additional Members of the System.Environment Class The Environment class exposes a number of extremely helpful methods beyond GetCommandLineArgs(). Specifically, this class allows you to obtain a number of details regarding the operating system currently hosting your .NET application using various static members. To illustrate the usefulness of System.Environment, update your Main() method to call a helper method named ShowEnvironmentDetails(). static int Main(string[] args) { ... // Helper method within the Program class. ShowEnvironmentDetails(); Console.ReadLine(); return -1; } Implement this method within your Program class to call various members of the Environment type. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 80 static void ShowEnvironmentDetails() { // Print out the drives on this machine, // and other interesting details. foreach (string drive in Environment.GetLogicalDrives()) Console.WriteLine(“Drive: {0}“, drive); Console.WriteLine(“OS: {0}“, Environment.OSVersion); Console.WriteLine(“Number of processors: {0}“, Environment.ProcessorCount); Console.WriteLine(“.NET Version: {0}“, Environment.Version); } The following output shows a possible test run of invoking this method. Of course, if you did not specify command-line arguments via the Visual Studio Debug tab, you will not find them printed to the console. ***** My First C# App ***** Hello World! Arg: -godmode Arg: -arg1 Arg: /arg2 Drive: C:\ Drive: D:\ Drive: E:\ Drive: F:\ Drive: G:\ Drive: H:\ Drive: I:\ OS: Microsoft Windows NT 6.1.7601 Service Pack 1 Number of processors: 4 .NET Version: 4.0.30319.17020 The Environment type defines members other than those shown in the previous example. Table 3-1 documents some additional properties of interest; however, be sure to check out the .NET Framework 4.5 SDK documentation for full details. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 81 Table 3-1. Select Properties of System.Environment Property Meaning in Life ExitCode Gets or sets the exit code for the application Is64BitOperatingSystem Returns a bool to represent if the host machine is running a 64-bit OS MachineName Gets the name of the current machine NewLine Gets the newline symbol for the current environment SystemDirectory Returns the full path to the system directory UserName Returns the name of the user that started this application Version Returns a Version object that represents the version of the .NET platform  Source Code The SimpleCSharpApp project is located under the Chapter 3 subdirectory. The System.Console Class Almost all of the example applications created over the course of the initial chapters of this book make extensive use of the System.Console class. While it is true that a console user interface (CUI) is not as enticing as a graphical user interface (GUI) or web-application, restricting the early examples to console programs will allow us to keep focused on the syntax of C# and the core aspects of the .NET platform, rather than dealing with the complexities of building desktop GUIs or web sites. As its name implies, the Console class encapsulates input, output, and error-stream manipulations for console-based applications. Table 3-2 lists some (but definitely not all) members of interest. As you can see, the Console class does provide some members that can spice up a simple command-line application, such as the ability to change background and foreground colors and issue beep noises (in a variety of frequencies!). Table 3-2. Select Members of System.Console Member Meaning in Life Beep() This method forces the console to emit a beep of a specified frequency and duration. BackgroundColor ForegroundColor These properties set the background/foreground colors for the current output. They may be assigned any member of the ConsoleColor enumeration. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 82 Member Meaning in Life BufferHeight BufferWidth These properties control the height/width of the console’s buffer area. Title This property sets the title of the current console. WindowHeight WindowWidth WindowTop WindowLeft These properties control the dimensions of the console in relation to the established buffer. Clear() This method clears the established buffer and console display area. Basic Input and Output with the Console Class In addition to the members in Table 3-2, the Console type defines a set of methods to capture input and output, all of which are static and are, therefore, called by prefixing the name of the class (Console) to the method name. As you have seen, WriteLine() pumps a text string (including a carriage return) to the output stream. The Write() method pumps text to the output stream without a carriage return. ReadLine() allows you to receive information from the input stream up until the Enter key is pressed, while Read() is used to capture a single character from the input stream. To illustrate basic I/O using the Console class, create a new Console Application project named BasicConsoleIO and update your Main() method to call a helper method named GetUserData(): class Program { static void Main(string[] args) { Console.WriteLine(“***** Basic Console I/O *****“); GetUserData(); Console.ReadLine(); } }  Note In Chapter 2, you briefly examined Visual Studio code snippets. The cw code snippet is quite useful during the early chapters of this text, in that it will automatically expand to Console.WriteLine()! To test this for yourself, type in cw somewhere within your Main() method and hit the Tab key twice. Sadly, there is no code snippet for Console.ReadLine(). Implement this method within the Program class with logic that prompts the user for some bits of information and echoes each item to the standard output stream. For example, we could ask the user for his or her name and age (which we will treat as a text value for simplicity, rather than the expected numerical value), as follows: CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 83 static void GetUserData() { // Get name and age. Console.Write(“Please enter your name: “); string userName = Console.ReadLine(); Console.Write(“Please enter your age: “); string userAge = Console.ReadLine(); // Change echo color, just for fun. ConsoleColor prevColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; // Echo to the console. Console.WriteLine(“Hello {0}! You are {1} years old.“, userName, userAge); // Restore previous color. Console.ForegroundColor = prevColor; } Not surprisingly, when you run this application, the input data is printed to the console (using a custom color to boot!). Formatting Console Output During these first few chapters, you might have noticed numerous occurrences of tokens such as {0} and {1} embedded within various string literals. The .NET platform supports a style of string formatting slightly akin to the printf() statement of C. Simply put, when you are defining a string literal that contains segments of data whose value is not known until runtime, you are able to specify a placeholder within the literal using this curly-bracket syntax. At runtime, the value(s) passed into Console.WriteLine() are substituted for each placeholder. The first parameter to WriteLine() represents a string literal that contains optional placeholders designated by {0}, {1}, {2}, and so forth. Be very aware that the first ordinal number of a curly-bracket placeholder always begins with 0. The remaining parameters to WriteLine() are simply the values to be inserted into the respective placeholders.  Note If you have more uniquely numbered curly-bracket placeholders than fill arguments, you will receive a format exception at runtime. However, if you have more fill arguments than placeholders, the unused fill arguments are ignored. It is permissible for a given placeholder to repeat within a given string. For example, if you are a Beatles fan and want to build the string “9, Number 9, Number 9“, you would write: // John says... Console.WriteLine(“{0}, Number {0}, Number {0}“, 9); CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 84 Also know that it is possible to position each placeholder in any location within a string literal, and it need not follow an increasing sequence. For example, consider the following code snippet: // Prints: 20, 10, 30 Console.WriteLine(“{1}, {0}, {2}“, 10, 20, 30); Formatting Numerical Data If you require more elaborate formatting for numerical data, each placeholder can optionally contain various format characters. Table 3-3 shows the most common formatting options. Table 3-3. .NET Numerical Format Characters String Format Character Meaning in Life C or c Used to format currency. By default, the flag will prefix the local cultural symbol (a dollar sign [$] for U.S. English). D or d Used to format decimal numbers. This flag may also specify the minimum number of digits used to pad the value. E or e Used for exponential notation. Casing controls whether the exponential constant is uppercase (E) or lowercase (e). F or f Used for fixed-point formatting. This flag may also specify the minimum number of digits used to pad the value. G or g Stands for general. This character can be used to format a number to fixed or exponential format. N or n Used for basic numerical formatting (with commas). X or x Used for hexadecimal formatting. If you use an uppercase X, your hex format will also contain uppercase characters. These format characters are suffixed to a given placeholder value using the colon token (e.g., {0:C}, {1:d}, {2:X}). To illustrate, update the Main() method to call a new helper function named FormatNumericalData(). Implement this method in your Program class to format a fixed numerical value in a variety of ways. // Now make use of some format tags. static void FormatNumericalData() { Console.WriteLine(“The value 99999 in various formats:“); Console.WriteLine(“c format: {0:c}“, 99999); Console.WriteLine(“d9 format: {0:d9}“, 99999); Console.WriteLine(“f3 format: {0:f3}“, 99999); Console.WriteLine(“n format: {0:n}“, 99999); CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 85 // Notice that upper- or lowercasing for hex // determines if letters are upper- or lowercase. Console.WriteLine(“E format: {0:E}“, 99999); Console.WriteLine(“e format: {0:e}“, 99999); Console.WriteLine(“X format: {0:X}“, 99999); Console.WriteLine(“x format: {0:x}“, 99999); } The following output shows the result of calling the FormatNumericalData() method. The value 99999 in various formats: c format: $99,999.00 d9 format: 000099999 f3 format: 99999.000 n format: 99,999.00 E format: 9.999900E+004 e format: 9.999900e+004 X format: 1869F x format: 1869f You’ll see additional formatting examples where required throughout this text; however, if you are interested in digging into .NET string formatting further, look up the topic “Formatting types” within the .NET Framework 4.5 SDK documentation.  Source Code The BasicConsoleIO project is located under the Chapter 3 subdirectory. Formatting Numerical Data Beyond Console Applications On a final note, be aware that the use of the .NET string formatting characters is not limited to console programs. This same formatting syntax can be used when calling the static string.Format() method. This can be helpful when you need to compose textual data at runtime for use in any application type (e.g., desktop GUI app, ASP.NET web app, and so forth). The string.Format() method returns a new string object, which is formatted according to the provided flags. After this point, you are free to use the textual data as you see fit. For example, assume you are building a graphical WPF desktop application and need to format a string for display in a message box. The following code illustrates how to do so, but be aware that this code will not compile until you reference the PresentationFramework.dll assembly for use by your project (see Chapter 2 for information on referencing libraries using Visual Studio). static void DisplayMessage() { // Using string.Format() to format a string literal. string userMessage = string.Format(“100000 in hex is {0:x}“, 100000); CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 86 // You need to reference PresentationFramework.dll // in order to compile this line of code! System.Windows.MessageBox.Show(userMessage); } System Data Types and Corresponding C# Keywords Like any programming language, C# defines keywords for fundamental data types, which are used to represent local variables, class data member variables, method return values, and parameters. Unlike other programming languages, however, these keywords are much more than simple compiler- recognized tokens. Rather, the C# data type keywords are actually shorthand notations for full-blown types in the System namespace. Table 3-4 lists each system data type, its range, the corresponding C# keyword, and the type’s compliance with the common language specification (CLS).  Note Recall from Chapter 1 that CLS-compliant .NET code can be used by any managed programming language. If you expose non–CLS-compliant data from your programs, other languages might not be able to make use of it. Table 3-4. The Intrinsic Data Types of C# C# Shorthand CLS Compliant? System Type Range Meaning in Life bool Yes System.Boolean true or false Represents truth or falsity sbyte No System.SByte –128 to 127 Signed 8-bit number byte Yes System.Byte 0 to 255 Unsigned 8-bit number short Yes System.Int16 –32,768 to 32,767 Signed 16-bit number ushort No System.UInt16 0 to 65,535 Unsigned 16-bit number int Yes System.Int32 –2,147,483,648 to 2,147,483,647 Signed 32-bit number uint No System.UInt32 0 to 4,294,967,295 Unsigned 32-bit number long Yes System.Int64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit number ulong No System.UInt64 0 to 18,446,744,073,709,551, 615 Unsigned 64-bit number CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 87 char Yes System.Char U+0000 to U+ffff Single 16-bit Unicode character float Yes System.Single -3.4 1038 to +3.4 1038 32-bit floating-point number double Yes System.Double ±5.0 10–324 to ±1.7 10308 64-bit floating-point number decimal Yes System.Decimal (-7.9 x 1028 to 7.9 x 1028) / (100 to 28) 128-bit signed number string Yes System.String Limited by system memory Represents a set of Unicode characters Object Yes System.Object Can store any data type in an object variable The base class of all types in the .NET universe  Note By default, a floating-point number is treated as a double. To declare a float variable, use the suffix f or F to the raw numerical value (5.3F); suffix m or M to a floating-point number to declare a decimal (300.5M). Finally, raw whole numbers default to an int data type. To set the underlying data type to a long, suffix l or L (4L). Variable Declaration and Initialization When you are declaring a local variable (e.g., a variable within a member scope), you do so by specifying the data type followed by the variable’s name. To begin, create a new Console Application project named BasicDataTypes. Update the Program class with the following helper method that is called from within Main(): static void LocalVarDeclarations() { Console.WriteLine(“=> Data Declarations:“); // Local variables are declared as so: // dataType varName; int myInt; string myString; Console.WriteLine(); } Be aware that it is a compiler error to make use of a local variable before assigning an initial value. Given this, it is good practice to assign an initial value to your local data points at the time of declaration. You may do so on a single line, or by separating the declaration and assignment into two code statements. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 88 static void LocalVarDeclarations() { Console.WriteLine(“=> Data Declarations:“); // Local variables are declared and initialized as follows: // dataType varName = initialValue; int myInt = 0; // You can also declare and assign on two lines. string myString; myString = “This is my character data“; Console.WriteLine(); } It is also permissible to declare multiple variables of the same underlying type on a single line of code, as in the following three bool variables: static void LocalVarDeclarations() { Console.WriteLine(“=> Data Declarations:“); int myInt = 0; string myString; myString = “This is my character data“; // Declare 3 bools on a single line. bool b1 = true, b2 = false, b3 = b1; Console.WriteLine(); } Since the C# bool keyword is simply a shorthand notation for the System.Boolean structure, it is also possible to allocate any data type using its full name (of course, the same point holds true for any C# data type keyword). Here is the final implementation of LocalVarDeclarations(), which illustrates various ways to declare a local variable. static void LocalVarDeclarations() { Console.WriteLine(“=> Data Declarations:“); // Local variables are declared and initialized as follows: // dataType varName = initialValue; int myInt = 0; string myString; myString = “This is my character data“; // Declare 3 bools on a single line. bool b1 = true, b2 = false, b3 = b1; // Use System.Boolean data type to declare a bool. System.Boolean b4 = false; Console.WriteLine(“Your data: {0}, {1}, {2}, {3}, {4}, {5}“, myInt, myString, b1, b2, b3, b4); CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 89 Console.WriteLine(); } Intrinsic Data Types and the new Operator All intrinsic data types support what is known as a default constructor (see Chapter 5). This feature allows you to create a variable using the new keyword, which automatically sets the variable to its default value. • bool variables are set to false. • Numeric data is set to 0 (or 0.0 in the case of floating-point data types). • char variables are set to a single empty character. • BigInteger variables are set to 0. • DateTime variables are set to 1/1/0001 12:00:00 AM. • Object references (including strings) are set to null.  Note The BigInteger data type seen in the previous list will be explained in just a bit. Although it is more cumbersome to use the new keyword when creating a basic data type variable, the following is syntactically well-formed C# code: static void NewingDataTypes() { Console.WriteLine(“=> Using new to create variables:“); bool b = new bool(); // Set to false. int i = new int(); // Set to 0. double d = new double(); // Set to 0. DateTime dt = new DateTime(); // Set to 1/1/0001 12:00:00 AM Console.WriteLine(“{0}, {1}, {2}, {3}“, b, i, d, dt); Console.WriteLine(); } The Data Type Class Hierarchy It is very interesting to note that even the primitive .NET data types are arranged in a class hierarchy. If you are new to the world of inheritance, you will discover the full details in Chapter 6. Until then, just understand that types at the top of a class hierarchy provide some default behaviors that are granted to the derived types. The relationship between these core system types can be understood as shown in Figure 3-2. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 90 Figure 3-2. The class hierarchy of system types Notice that each of these types ultimately derive from System.Object, which defines a set of methods (e.g., ToString(), Equals(), GetHashCode()) common to all types in the .NET base class libraries (these methods are fully detailed in Chapter 6). Also note that many numerical data types derive from a class named System.ValueType. Descendents of ValueType are automatically allocated on the stack and, therefore, have a very predictable lifetime and are quite efficient. On the other hand, types that do not have System.ValueType in their inheritance chain (such as System.Type, System.String, System.Array, System.Exception, and System.Delegate) are not allocated on the stack, but on the garbage-collected heap. (More information on this distinction can be found in Chapter 4.) Without getting too hung up on the details of System.Object and System.ValueType, just understand that because a C# keyword (such as int) is simply shorthand notation for the corresponding system type (in this case, System.Int32), the following is perfectly legal syntax, given that System.Int32 (the C# int) eventually derives from System.Object and, therefore, can invoke any of its public members, as illustrated by this additional helper function: CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 91 static void ObjectFunctionality() { Console.WriteLine(“=> System.Object Functionality:“); // A C# int is really a shorthand for System.Int32, // which inherits the following members from System.Object. Console.WriteLine(“12.GetHashCode() = {0}“, 12.GetHashCode()); Console.WriteLine(“12.Equals(23) = {0}“, 12.Equals(23)); Console.WriteLine(“12.ToString() = {0}“, 12.ToString()); Console.WriteLine(“12.GetType() = {0}“, 12.GetType()); Console.WriteLine(); } If you were to call this method from within Main(), you would find the output shown in here: => System.Object Functionality: 12.GetHashCode() = 12 12.Equals(23) = False 12.ToString() = 12 12.GetType() = System.Int32 Members of Numerical Data Types To continue experimenting with the intrinsic C# data types, understand that the numerical types of .NET support MaxValue and MinValue properties that provide information regarding the range a given type can store. In addition to the MinValue/MaxValue properties, a given numerical system type may define further useful members. For example, the System.Double type allows you to obtain the values for epsilon and infinity (which might be of interest to those of you with a mathematical flare). To illustrate, consider the following helper function: static void DataTypeFunctionality() { Console.WriteLine(“=> Data type Functionality:“); Console.WriteLine(“Max of int: {0}“, int.MaxValue); Console.WriteLine(“Min of int: {0}“, int.MinValue); Console.WriteLine(“Max of double: {0}“, double.MaxValue); Console.WriteLine(“Min of double: {0}“, double.MinValue); Console.WriteLine(“double.Epsilon: {0}“, double.Epsilon); Console.WriteLine(“double.PositiveInfinity: {0}“, double.PositiveInfinity); Console.WriteLine(“double.NegativeInfinity: {0}“, double.NegativeInfinity); Console.WriteLine(); } CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 92 Members of System.Boolean Next, consider the System.Boolean data type. The only valid assignment a C# bool can take is from the set {true | false}. Given this point, it should be clear that System.Boolean does not support a MinValue/MaxValue property set, but rather TrueString/FalseString (which yields the string “True“ or “False“, respectively). For example: Console.WriteLine(“bool.FalseString: {0}“, bool.FalseString); Console.WriteLine(“bool.TrueString: {0}“, bool.TrueString); Members of System.Char C# textual data is represented by the string and char keywords, which are simple shorthand notations for System.String and System.Char, both of which are Unicode under the hood. As you might already know, a string represents a contiguous set of characters (e.g., “Hello“), while the char can represent a single slot in a string (e.g., 'H'). The System.Char type provides you with a great deal of functionality beyond the ability to hold a single point of character data. Using the static methods of System.Char, you are able to determine whether a given character is numerical, alphabetical, a point of punctuation, or whatnot. Consider the following method: static void CharFunctionality() { Console.WriteLine(“=> char type Functionality:“); char myChar = 'a'; Console.WriteLine(“char.IsDigit('a'): {0}“, char.IsDigit(myChar)); Console.WriteLine(“char.IsLetter('a'): {0}“, char.IsLetter(myChar)); Console.WriteLine(“char.IsWhiteSpace('Hello There', 5): {0}“, char.IsWhiteSpace(“Hello There“, 5)); Console.WriteLine(“char.IsWhiteSpace('Hello There', 6): {0}“, char.IsWhiteSpace(“Hello There“, 6)); Console.WriteLine(“char.IsPunctuation('?'): {0}“, char.IsPunctuation('?')); Console.WriteLine(); } As illustrated in the previous method, many members of System.Char have two calling conventions: a single character, or a string with a numerical index that specifies the position of the character to test. Parsing Values from String Data The .NET data types provide the ability to generate a variable of their underlying type given a textual equivalent (e.g., parsing). This technique can be extremely helpful when you wish to convert a bit of user input data (such as a selection from a GUI-based, drop-down list box) into a numerical value. Consider the following parsing logic within a method named ParseFromStrings(): static void ParseFromStrings() { Console.WriteLine(“=> Data type parsing:“); CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 93 bool b = bool.Parse(“True“); Console.WriteLine(“Value of b: {0}“, b); double d = double.Parse(“99.884“); Console.WriteLine(“Value of d: {0}“, d); int i = int.Parse(“8“); Console.WriteLine(“Value of i: {0}“, i); char c = Char.Parse(“w“); Console.WriteLine(“Value of c: {0}“, c); Console.WriteLine(); } System.DateTime and System.TimeSpan The System namespace defines a few useful data types for which there are no C# keywords, such as the DateTime and TimeSpan structures (I’ll leave the investigation of System.Guid and System.Void, as shown in Figure 3-2, to interested readers; but do be aware that these two data types in the System namespace are seldom useful in most applications). The DateTime type contains data that represents a specific date (month, day, year) and time value, both of which may be formatted in a variety of ways using the supplied members. The TimeSpan structure allows you to easily define and transform units of time using various members. static void UseDatesAndTimes() { Console.WriteLine(“=> Dates and Times:“); // This constructor takes (year, month, day). DateTime dt = new DateTime(2011, 10, 17); // What day of the month is this? Console.WriteLine(“The day of {0} is {1}“, dt.Date, dt.DayOfWeek); // Month is now December. dt = dt.AddMonths(2); Console.WriteLine(“Daylight savings: {0}“, dt.IsDaylightSavingTime()); // This constructor takes (hours, minutes, seconds). TimeSpan ts = new TimeSpan(4, 30, 0); Console.WriteLine(ts); // Subtract 15 minutes from the current TimeSpan and // print the result. Console.WriteLine(ts.Subtract(new TimeSpan(0, 15, 0))); } The System.Numerics.dll Assembly The System.Numerics namespace defines a structure named BigInteger. As its name implies, the BigInteger data type can be used when you need to represent humongous numerical values, which are not constrained by a fixed upper or lower limit. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 94  Note The System.Numerics namespace defines a second structure named Complex, which allows you to model mathematically complex numerical data (e.g., imaginary units, real data, hyperbolic tangents). Consult the .NET Framework 4.5 SDK documentation if you are interested. While many of your .NET applications might never need to make use of the BigInteger structure, if you do find the need to define a massive numerical value, your first step is to reference the System.Numerics.dll assembly into your project. If you wish to follow along with the current example, perform the following tasks: 1. Select the Project  Add Reference… menu option of Visual Studio. 2. Locate and select the System.Numerics.dll assembly within the list of presented libraries. 3. Press the Add button, then the Close button. After you have done so, add the following using directive to the file, which will be using the BigInteger data type: // BigInteger lives here! using System.Numerics; At this point, you can create a BigInteger variable using the new operator. Within the constructor, you can specify a numerical value, including floating-point data. However, recall that when you define a literal whole number (such as 500), the runtime will default the data type to an int. Likewise, literal floating-point data (such as 55.333) will default to a double. How, then, can you set BigInteger to a massive value while not overflowing the default data types used for raw numerical values? The simplest approach is to establish the massive numerical value as a text literal, which can be converted into a BigInteger variable via the static Parse() method. If required, you can also pass in a byte array directly to the constructor of the BigInteger class.  Note After you assign a value to a BigInteger variable, you cannot change it, as the data is immutable. However, the BigInteger class defines a number of members that will return new BigInteger objects based on your data modifications (such as the static Multiply() method used in the proceeding code sample). In any case, after you have defined a BigInteger variable, you will find this class defines very similar members as other intrinsic C# data types (e.g., float, int). In addition, the BigInteger class defines several static members that allow you to apply basic mathematical expressions (such as adding and multiplying) to BigInteger variables. Here is an example of working with the BigInteger class. static void UseBigInteger() { Console.WriteLine(“=> Use BigInteger:“); BigInteger biggy = BigInteger.Parse(“9999999999999999999999999999999999999999999999“); CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 95 Console.WriteLine(“Value of biggy is {0}“, biggy); Console.WriteLine(“Is biggy an even value?: {0}“, biggy.IsEven); Console.WriteLine(“Is biggy a power of two?: {0}“, biggy.IsPowerOfTwo); BigInteger reallyBig = BigInteger.Multiply(biggy, BigInteger.Parse(“8888888888888888888888888888888888888888888“)); Console.WriteLine(“Value of reallyBig is {0}“, reallyBig); } It is also important to note that the BigInteger data type responds to C#’s intrinsic mathematical operators, such as +, -, and *. Therefore, rather than calling BigInteger.Multiply() to multiply two huge numbers, you could author the following code: BigInteger reallyBig2 = biggy * reallyBig; At this point, I hope you understand that the C# keywords representing basic data types have a corresponding type in the .NET base class libraries, each of which exposes a fixed functionality. While I have not detailed each member of these data types, you are in a great position to dig into the details as you see fit. Be sure to consult the .NET Framework 4.5 SDK documentation for full details regarding the various .NET data types—you will likely be surprised at the amount of built in functionality.  Source Code The BasicDataTypes project is located under the Chapter 3 subdirectory. Working with String Data System.String provides a number of methods you would expect from such a utility class, including methods that return the length of the character data, find substrings within the current string, and convert to and from uppercase/lowercase. Table 3-5 lists some (but by no means all) of the interesting members. Table 3-5. Select Members of System.String String Member Meaning in Life Length This property returns the length of the current string. Compare() This static method compares two strings. Contains() This method determines whether a string contains a specific substring. Equals() This method tests whether two string objects contain identical character data. Format() This static method formats a string using other primitives (e.g., numerical data, other strings) and the {0} notation examined earlier in this chapter. Insert() This method inserts a string within a given string. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 96 String Member Meaning in Life PadLeft() PadRight() These methods are used to pad a string with some characters. Remove() Replace() Use these methods to receive a copy of a string with modifications (characters removed or replaced). Split() This method returns a String array containing the substrings in this instance that are delimited by elements of a specified char array or string array. Trim() This method removes all occurrences of a set of specified characters from the beginning and end of the current string. ToUpper() ToLower() These methods create a copy of the current string in uppercase or lowercase format, respectively. Basic String Manipulation Working with the members of System.String is as you would expect. Simply declare a string variable and make use of the provided functionality via the dot operator. Be aware that a few of the members of System.String are static members and are, therefore, called at the class (rather than the object) level. Assume you have created a new Console Application project named FunWithStrings. Author the following method, which should be called from within Main(): static void BasicStringFunctionality() { Console.WriteLine(“=> Basic String functionality:“); string firstName = “Freddy“; Console.WriteLine(“Value of firstName: {0}“, firstName); Console.WriteLine(“firstName has {0} characters.“, firstName.Length); Console.WriteLine(“firstName in uppercase: {0}“, firstName.ToUpper()); Console.WriteLine(“firstName in lowercase: {0}“, firstName.ToLower()); Console.WriteLine(“firstName contains the letter y?: {0}“, firstName.Contains(“y“)); Console.WriteLine(“firstName after replace: {0}“, firstName.Replace(“dy“, ““)); Console.WriteLine(); } Not too much to say here, as this method simply invokes various members, such as ToUpper() and Contains(), on a local string variable to yield various formats and transformations. Here is the initial output: ***** Fun with Strings ***** => Basic String functionality: Value of firstName: Freddy CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 97 firstName has 6 characters. firstName in uppercase: FREDDY firstName in lowercase: freddy firstName contains the letter y?: True firstName after replace: Fred While this output might not seem too surprising, the output seen via calling the Replace() method is a bit misleading. In reality, the firstName variable has not changed at all; rather, we receive back a new string in a modified format. We will revisit the immutable nature of strings in just a few moments. String Concatenation string variables can be connected together to build larger strings via the C# + operator. As you might know, this technique is formally termed string concatenation. Consider the following new helper function: static void StringConcatenation() { Console.WriteLine(“=> String concatenation:“); string s1 = “Programming the “; string s2 = “PsychoDrill (PTP)“; string s3 = s1 + s2; Console.WriteLine(s3); Console.WriteLine(); } You might be interested to know that the C# + symbol is processed by the compiler to emit a call to the static String.Concat() method. Given this, it is possible to perform string concatenation by calling String.Concat() directly (although you really have not gained anything by doing so—in fact, you have incurred additional keystrokes!). static void StringConcatenation() { Console.WriteLine(“=> String concatenation:“); string s1 = “Programming the “; string s2 = “PsychoDrill (PTP)“; string s3 = String.Concat(s1, s2); Console.WriteLine(s3); Console.WriteLine(); } Escape Characters As in other C-based languages, C# string literals may contain various escape characters, which qualify how the character data should be printed to the output stream. Each escape character begins with a backslash, followed by a specific token. In case you are a bit rusty on the meanings behind these escape characters, Table 3-6 lists the more common options. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 98 Table 3-6. String Literal Escape Characters Character Meaning in Life \' Inserts a single quote into a string literal. \“ Inserts a double quote into a string literal. \\ Inserts a backslash into a string literal. This can be quite helpful when defining file or network paths. \a Triggers a system alert (beep). For console programs, this can be an audio clue to the user. \n Inserts a new line (on Windows platforms). \r Inserts a carriage return. \t Inserts a horizontal tab into the string literal. For example, to print a string that contains a tab between each word, you can make use of the \t escape character. Or assume you wish to create a string literal that contains quotation marks, another that defines a directory path, and a final string literal that inserts three blank lines after printing the character data. To do so without compiler errors, you would need to make use of the \“, \\, and \n escape characters. Also, to annoy any person within a 10-foot radius from you, notice that I have embedded an alarm within each string literal (to trigger a beep). Consider the following: static void EscapeChars() { Console.WriteLine(“=> Escape characters:\a“); string strWithTabs = “Model\tColor\tSpeed\tPet Name\a “; Console.WriteLine(strWithTabs); Console.WriteLine(“Everyone loves \“Hello World\“\a “); Console.WriteLine(“C:\\MyApp\\bin\\Debug\a “); // Adds a total of 4 blank lines (then beep again!). Console.WriteLine(“All finished.\n\n\n\a “); Console.WriteLine(); } Defining Verbatim Strings When you prefix a string literal with the @ symbol, you have created what is termed a verbatim string. Using verbatim strings, you disable the processing of a literal’s escape characters and print out a string as is. This can be most useful when working with strings representing directory and network paths. Therefore, rather than making use of \\ escape characters, you can simply write the following: CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 99 // The following string is printed verbatim, // thus all escape characters are displayed. Console.WriteLine(@“C:\MyApp\bin\Debug“); Also note that verbatim strings can be used to preserve white space for strings that flow over multiple lines. // White space is preserved with verbatim strings. string myLongString = @“This is a very very very long string“; Console.WriteLine(myLongString); Using verbatim strings, you can also directly insert a double quote into a literal string by doubling the “ token. Console.WriteLine(@“Cerebus said ““Darrr! Pret-ty sun-sets“““); Strings and Equality As fully explained in Chapter 4, a reference type is an object allocated on the garbage-collected managed heap. By default, when you perform a test for equality on reference types (via the C# == and != operators), you will be returned true if the references are pointing to the same object in memory. However, even though the string data type is indeed a reference type, the equality operators have been redefined to compare the values of string objects, not the object in memory to which they refer. static void StringEquality() { Console.WriteLine(“=> String equality:“); string s1 = “Hello!“; string s2 = “Yo!“; Console.WriteLine(“s1 = {0}“, s1); Console.WriteLine(“s2 = {0}“, s2); Console.WriteLine(); // Test these strings for equality. Console.WriteLine(“s1 == s2: {0}“, s1 == s2); Console.WriteLine(“s1 == Hello!: {0}“, s1 == “Hello!“); Console.WriteLine(“s1 == HELLO!: {0}“, s1 == “HELLO!“); Console.WriteLine(“s1 == hello!: {0}“, s1 == “hello!“); Console.WriteLine(“s1.Equals(s2): {0}“, s1.Equals(s2)); Console.WriteLine(“Yo.Equals(s2): {0}“, “Yo!“.Equals(s2)); Console.WriteLine(); } The C# equality operators perform a case-sensitive, character-by-character equality test on string objects. Therefore, “Hello!“ is not equal to “HELLO!“, which is also different from “hello!“. Also, keeping the connection between string and System.String in mind, notice that we are able to test for equality using the Equals() method of String as well as the baked-in equality operators. Finally, given that every string literal (such as “Yo“) is a valid System.String instance, we are able to access string-centric functionality from a fixed sequence of characters. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 100 Strings Are Immutable One of the interesting aspects of System.String is that after you assign a string object with its initial value, the character data cannot be changed. At first glance, this might seem like a flat-out lie, given that we are always reassigning strings to new values and because the System.String type defines a number of methods that appear to modify the character data in one way or another (such as uppercasing and lowercasing). However, if you look more closely at what is happening behind the scenes, you will notice the methods of the string type are, in fact, returning you a brand-new string object in a modified format. static void StringsAreImmutable() { // Set initial string value. string s1 = “This is my string.“; Console.WriteLine(“s1 = {0}“, s1); // Uppercase s1? string upperString = s1.ToUpper(); Console.WriteLine(“upperString = {0}“, upperString); // Nope! s1 is in the same format! Console.WriteLine(“s1 = {0}“, s1); } If you examine the relevant output that follows, you can verify that the original string object (s1) is not uppercased when calling ToUpper(). Rather, you are returned a copy of the string in a modified format. s1 = This is my string. upperString = THIS IS MY STRING. s1 = This is my string. The same law of immutability holds true when you use the C# assignment operator. To illustrate, implement the following StringsAreImmutable2() method: static void StringsAreImmutable2() { string s2 = “My other string“; s2 = “New string value“; } Now, compile your application and load the assembly into ildasm.exe (see Chapter 1). The following output shows what you would find if you were to generate CIL code for the StringsAreImmutable2() method: .method private hidebysig static void StringsAreImmutable2() cil managed { // Code size 14 (0xe) .maxstack 1 CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 101 .locals init ([0] string s2) IL_0000: nop IL_0001: ldstr “My other string“ IL_0006: stloc.0 IL_0007: ldstr “New string value“ IL_000c: stloc.0 IL_000d: ret } // end of method Program::StringAreImmutable2 Although we have yet to examine the low-level details of the CIL, note the numerous calls to the ldstr (load string) opcode. Simply put, the ldstr opcode of the CIL loads a new string object on the managed heap. The previous string object that contained the value “My other string“ will eventually be garbage collected. So, what exactly are we to gather from this insight? In a nutshell, the string class can be inefficient and result in bloated code if misused, especially when performing string concatenation. If you need to represent basic character data such as a U.S. Social Security number, first or last names, or simple bits of text used within your application, the string class is the perfect choice. However, if you are building an application that makes heavy use of frequently changing textual data (such as a word processing program), it would be a very bad idea to represent the word processing data using string objects, as you will most certainly (and often indirectly) end up making unnecessary copies of string data. So what is a programmer to do? Glad you asked. The System.Text.StringBuilder Type Given that the string type can be inefficient when used with reckless abandon, the .NET base class libraries provide the System.Text namespace. Within this (relatively small) namespace lives a class named StringBuilder. Like the System.String class, the StringBuilder defines methods that allow you to replace or format segments, for example. When you wish to use this type in your C# code files, your first step is to make sure the following namespace is imported into your code file (this should already be the case for a new Visual Studio project): // StringBuilder lives here! using System.Text; What is unique about the StringBuilder is that when you call members of this type, you are directly modifying the object’s internal character data (making it more efficient), not obtaining a copy of the data in a modified format. When you create an instance of the StringBuilder, you can supply the object’s initial startup values via one of many constructors. If you are new to the topic of constructors, simply understand that constructors allow you to create an object with an initial state when you apply the new keyword. Consider the following usage of StringBuilder: static void FunWithStringBuilder() { Console.WriteLine(“=> Using the StringBuilder:“); StringBuilder sb = new StringBuilder(“**** Fantastic Games ****“); sb.Append(“\n“); sb.AppendLine(“Half Life“); sb.AppendLine(“Morrowind“); sb.AppendLine(“Deus Ex“ + “2“); sb.AppendLine(“System Shock“); Console.WriteLine(sb.ToString()); CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 102 sb.Replace(“2“, “ Invisible War“); Console.WriteLine(sb.ToString()); Console.WriteLine(“sb has {0} chars.“, sb.Length); Console.WriteLine(); } Here, we have constructed a StringBuilder set to the initial value “**** Fantastic Games ****“. As you can see, we are appending to the internal buffer and are able to replace or remove characters at will. By default, a StringBuilder is only able to initially hold a string of 16 characters or fewer (but will expand automatically if necessary); however, this default starting value can be changed via an additional constructor argument. // Make a StringBuilder with an initial size of 256. StringBuilder sb = new StringBuilder(“**** Fantastic Games ****“, 256); If you append more characters than the specified limit, the StringBuilder object will copy its data into a new instance and grow the buffer by the specified limit.  Source Code The FunWithStrings project is located under the Chapter 3 subdirectory. Narrowing and Widening Data Type Conversions Now that you understand how to work with intrinsic C# data types, let’s examine the related topic of data type conversion. Assume you have a new Console Application project named TypeConversions that defines the following class: class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with type conversions *****“); // Add two shorts and print the result. short numb1 = 9, numb2 = 10; Console.WriteLine(“{0} + {1} = {2}“, numb1, numb2, Add(numb1, numb2)); Console.ReadLine(); } static int Add(int x, int y) { return x + y; } } Notice that the Add() method expects to be sent two int parameters. However, the Main() method is, in fact, sending in two short variables. While this might seem like a complete and total mismatch of data types, the program compiles and executes without error, returning the expected result of 19. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 103 The reason the compiler treats this code as syntactically sound is due to the fact that there is no possibility for loss of data. Given that the maximum value of a short (32,767) is well within the maximum range of an int (2,147,483,647), the compiler implicitly widens each short to an int. Formally speaking, widening is the term used to define an implicit upward cast that does not result in a loss of data.  Note Look up “Type Conversion Tables” in the .NET Framework 4.5 SDK documentation if you wish to see permissible widening (and narrowing, also see the following) conversions for each C# data type. Although this implicit widening worked in our favor for the previous example, other times this “feature” can be the source of compile-time errors. For example, assume that you have set values to numb1 and numb2 that (when added together) overflow the maximum value of a short. Also, assume you are storing the return value of the Add() method within a new local short variable, rather than directly printing the result to the console. static void Main(string[] args) { Console.WriteLine(“***** Fun with type conversions *****“); // Compiler error below! short numb1 = 30000, numb2 = 30000; short answer = Add(numb1, numb2); Console.WriteLine(“{0} + {1} = {2}“, numb1, numb2, answer); Console.ReadLine(); } In this case, the compiler reports the following error: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) The problem is that although the Add() method is capable of returning an int with the value 60,000 (as this fits within the range of a System.Int32), the value cannot be stored in a short, as it overflows the bounds of this data type. Formally speaking, the CLR was unable to apply a narrowing operation. As you can guess, narrowing is the logical opposite of widening, in that a larger value is stored within a smaller data type variable. It is important to point out that all narrowing conversions result in a compiler error, even when you can reason that the narrowing conversion should indeed succeed. For example, the following code also results in a compiler error: // Another compiler error! static void NarrowingAttempt() { byte myByte = 0; int myInt = 200; CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 104 myByte = myInt; Console.WriteLine(“Value of myByte: {0}“, myByte); } Here, the value contained within the int variable (myInt) is safely within the range of a byte; therefore, you would expect the narrowing operation to not result in a runtime error. However, given that C# is a language built with type safety in mind, we do indeed receive a compiler error. When you wish to inform the compiler that you are willing to deal with a possible loss of data due to a narrowing operation, you must apply an explicit cast using the C# casting operator (). Consider the following update to the Program type. class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with type conversions *****“); short numb1 = 30000, numb2 = 30000; // Explicitly cast the int into a short (and allow loss of data). short answer = (short)Add(numb1, numb2); Console.WriteLine(“{0} + {1} = {2}“, numb1, numb2, answer); NarrowingAttempt(); Console.ReadLine(); } static int Add(int x, int y) { return x + y; } static void NarrowingAttempt() { byte myByte = 0; int myInt = 200; // Explicitly cast the int into a byte (no loss of data). myByte = (byte)myInt; Console.WriteLine(“Value of myByte: {0}“, myByte); } } At this point, our code compiles; however, the result of our addition is completely incorrect: ***** Fun with type conversions ***** 30000 + 30000 = -5536 Value of myByte: 200 CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 105 As you have just witnessed, an explicit cast allows you to force the compiler to apply a narrowing conversion, even when doing so may result in a loss of data. In the case of the NarrowingAttempt() method, this was not a problem, as the value 200 can fit snuggly within the range of a byte. However, in the case of adding the two shorts within Main(), the end result is completely unacceptable (30,000 + 30,000 = –5536?). If you are building an application where loss of data is always unacceptable, C# provides the checked and unchecked keywords to ensure data loss does not escape undetected. The checked Keyword Let’s begin by learning the role of the checked keyword. Assume you have a new method within Program that attempts to add two bytes, each of which has been assigned a value that is safely below the maximum (255). If you were to add the values of these types (casting the returned int to a byte), you would assume that the result would be the exact sum of each member. static void ProcessBytes() { byte b1 = 100; byte b2 = 250; byte sum = (byte)Add(b1, b2); // sum should hold the value 350. However, we find the value 94! Console.WriteLine(“sum = {0}“, sum); } If you were to view the output of this application, you might be surprised to find that sum contains the value 94 (rather than the expected 350). The reason is simple. Given that a System.Byte can hold a value only between 0 and 255 (inclusive, for a grand total of 256 slots), sum now contains the overflow value (350 – 256 = 94). By default, if you take no corrective course of action, overflow/underflow conditions occur without error. To handle overflow or underflow conditions in your application, you have two options. Your first choice is to leverage your wits and programming skills to handle all overflow/underflow conditions manually. Of course, the problem with this technique is the simple fact that you are human, and even your best attempts might result in errors that have escaped your eyes. Thankfully, C# provides the checked keyword. When you wrap a statement (or a block of statements) within the scope of the checked keyword, the C# compiler emits additional CIL instructions that test for overflow conditions that may result when adding, multiplying, subtracting, or dividing two numerical data types. If an overflow has occurred, you will receive a runtime exception: System.OverflowException. Chapter 7 will examine all the details of structured exception handling and the use of the try and catch keywords. Without getting too hung up on the specifics at this point, observe the following update: static void ProcessBytes() { byte b1 = 100; byte b2 = 250; // This time, tell the compiler to add CIL code // to throw an exception if overflow/underflow // takes place. try { CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 106 byte sum = checked((byte)Add(b1, b2)); Console.WriteLine(“sum = {0}“, sum); } catch (OverflowException ex) { Console.WriteLine(ex.Message); } } Notice that the return value of Add() has been wrapped within the scope of the checked keyword. Because the sum is greater than a byte, we trigger a runtime exception. Notice the error message printed out via the Message property: Arithmetic operation resulted in an overflow. If you wish to force overflow checking to occur over a block of code statements, you can do so by defining a “checked scope” as follows: try { checked { byte sum = (byte)Add(b1, b2); Console.WriteLine(“sum = {0}“, sum); } } catch (OverflowException ex) { Console.WriteLine(ex.Message); } In either case, the code in question will be evaluated for possible overflow conditions automatically, which will trigger an overflow exception if encountered. Setting Project-Wide Overflow Checking If you are creating an application that should never allow silent overflow to occur, you might find yourself in the annoying position of wrapping numerous lines of code within the scope of the checked keyword. As an alternative, the C# compiler supports the /checked flag. When enabled, all of your arithmetic will be evaluated for overflow without the need to make use of the C# checked keyword. If overflow has been discovered, you will still receive a runtime exception. To enable this flag using Visual Studio, open your project’s property page and click the Advanced button on the Build tab. From the resulting dialog box, select the Check for arithmetic overflow/underflow check box (see Figure 3-3). CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 107 Figure 3-3. Enabling project-wide overflow/underflow data checking Enabling this setting can be very helpful when you’re creating a debug build. After all of the overflow exceptions have been squashed out of the code base, you’re free to disable the /checked flag for subsequent builds (which will increase the runtime performance of your application). The unchecked Keyword Now, assuming you have enabled this project-wide setting, what are you to do if you have a block of code where data loss is acceptable? Given that the /checked flag will evaluate all arithmetic logic, C# provides the unchecked keyword to disable the throwing of an overflow exception on a case-by-case basis. This keyword’s use is identical to that of the checked keyword in that you can specify a single statement or a block of statements. // Assuming /checked is enabled, // this block will not trigger // a runtime exception. unchecked { byte sum = (byte)(b1 + b2); Console.WriteLine(“sum = {0} “, sum); } So, to summarize the C# checked and unchecked keywords, remember that the default behavior of the .NET runtime is to ignore arithmetic overflow/underflow. When you want to selectively handle discrete statements, make use of the checked keyword. If you wish to trap overflow errors throughout your application, enable the /checked flag. Finally, the unchecked keyword may be used if you have a block of code where overflow is acceptable (and thus should not trigger a runtime exception). CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 108  Source Code The TypeConversions project is located under the Chapter 3 subdirectory. Understanding Implicitly Typed Local Variables Up until this point in the chapter, when we have been defining local variables, we’ve explicitly specified the underlying data type of each variable being declared. static void DeclareExplicitVars() { // Explicitly typed local variables // are declared as follows: // dataType variableName = initialValue; int myInt = 0; bool myBool = true; string myString = “Time, marches on...“; } While many (including yours truly) would argue that is it is always good practice to explicitly specify the data type of each variable, the C# language does provide for implicitly typing of local variables using the var keyword. The var keyword can be used in place of specifying a specific data type (such as int, bool, or string). When you do so, the compiler will automatically infer the underlying data type based on the initial value used to initialize the local data point. To illustrate the role of implicit typing, create a new Console Application project named ImplicitlyTypedLocalVars. Notice how the local variables within the previous method can now be declared as follows: static void DeclareImplicitVars() { // Implicitly typed local variables // are declared as follows: // var variableName = initialValue; var myInt = 0; var myBool = true; var myString = “Time, marches on...“; }  Note Strictly speaking, var is not a C# keyword. It is permissible to declare variables, parameters, and fields named “var” without compile-time errors. However, when the var token is used as a data type, it is contextually treated as a keyword by the compiler. In this case, the compiler is able to infer, given the initially assigned value, that myInt is, in fact, a System.Int32, myBool is a System.Boolean, and myString is indeed of type System.String. You can verify this by printing out the type name via reflection. As you will see in much more detail in Chapter 15, reflection is the act of determining the composition of a type at runtime. For example, using reflection, CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 109 you can determine the data type of an implicitly typed local variable. Update your method with the following code statements: static void DeclareImplicitVars() { // Implicitly typed local variables. var myInt = 0; var myBool = true; var myString = “Time, marches on...“; // Print out the underlying type. Console.WriteLine(“myInt is a: {0}“, myInt.GetType().Name); Console.WriteLine(“myBool is a: {0}“, myBool.GetType().Name); Console.WriteLine(“myString is a: {0}“, myString.GetType().Name); }  Note Be aware that you can use this implicit typing for any type including arrays, generic types (see Chapter 9), and your own custom types. You’ll see other examples of implicit typing over the course of this book. If you were to call the DeclareImplicitVars() method from within Main(), you’d find the output shown here. ***** Fun with Implicit Typing ***** myInt is a: Int32 myBool is a: Boolean myString is a: String Restrictions on Implicitly Typed Variables There are various restrictions regarding the use of the var keyword. First and foremost, implicit typing applies only to local variables in a method or property scope. It is illegal to use the var keyword to define return values, parameters, or field data of a custom type. For example, the following class definition will result in various compile-time errors: class ThisWillNeverCompile { // Error! var cannot be used as field data! private var myInt = 10; // Error! var cannot be used as a return value // or parameter type! public var MyMethod(var x, var y){} } CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 110 Also, local variables declared with the var keyword must be assigned an initial value at the exact time of declaration and cannot be assigned the initial value of null. This last restriction should make sense, given that the compiler cannot infer what sort of type in memory the variable would be pointing to based only on null. // Error! Must assign a value! var myData; // Error! Must assign value at exact time of declaration! var myInt; myInt = 0; // Error! Can't assign null as initial value! var myObj = null; It is permissible, however, to assign an inferred local variable to null after its initial assignment (provided it is a reference type). // OK, if SportsCar is a reference type! var myCar = new SportsCar(); myCar = null; Furthermore, it is permissible to assign the value of an implicitly typed local variable to the value of other variables, implicitly typed or not. // Also OK! var myInt = 0; var anotherInt = myInt; string myString = “Wake up!“; var myData = myString; Also, it is permissible to return an implicitly typed local variable to the caller, provided the method return type is the same underlying type as the var-defined data point. static int GetAnInt() { var retVal = 9; return retVal; } Implicit Typed Data Is Strongly Typed Data Be very aware that implicit typing of local variables results in strongly typed data. Therefore, use of the var keyword is not the same technique used with scripting languages (such as JavaScript or Perl) or the COM Variant data type, where a variable can hold values of different types over its lifetime in a program (often termed dynamic typing).  Note C# does allow for dynamic typing in C# using a keyword called—surprise, surprise—dynamic. You will learn about this aspect of the language in Chapter 16. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 111 Rather, type inference keeps the strongly typed aspect of the C# language and affects only the declaration of variables at compile time. After that, the data point is treated as if it were declared with that type; assigning a value of a different type into that variable will result in a compile-time error. static void ImplicitTypingIsStrongTyping() { // The compiler knows “s“ is a System.String. var s = “This variable can only hold string data!“; s = “This is fine...“; // Can invoke any member of the underlying type. string upper = s.ToUpper(); // Error! Can't assign numerical data to a string! s = 44; } Usefulness of Implicitly Typed Local Variables Now that you have seen the syntax used to declare implicitly typed local variables, I am sure you are wondering when to make use of this construct. First and foremost, using var to declare local variables simply for the sake of doing so really brings little to the table. Doing so can be confusing to others reading your code, as it becomes harder to quickly determine the underlying data type and, therefore, more difficult to understand the overall functionality of the variable. So, if you know you need an int, declare an int! However, as you will see beginning in Chapter 12, the LINQ technology set makes use of query expressions that can yield dynamically created result sets based on the format of the query itself. In these cases, implicit typing is extremely helpful, as we do not need to explicitly define the type that a query may return, which in some cases would be literally impossible to do. Without getting hung up on the following LINQ example code, see if you can figure out the underlying data type of subset: static void LinqQueryOverInts() { int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 }; // LINQ query! var subset = from i in numbers where i < 10 select i; Console.Write(“Values in subset: “); foreach (var i in subset) { Console.Write(“{0} “, i); } Console.WriteLine(); // Hmm...what type is subset? Console.WriteLine(“subset is a: {0}“, subset.GetType().Name); Console.WriteLine(“subset is defined in: {0}“, subset.GetType().Namespace); } You might be assuming that the subset data type is an array of integers. That seems to be the case, but in fact, it is a low level LINQ data type that you would never know about unless you have been doing CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 112 LINQ for a very long time, or open the compiled image in ildasm.exe. The good news is that when you are using LINQ, you seldom (if ever) care about the underlying type of the query’s return value; you will simply assign the value to an implicitly typed local variable. In fact, it could be argued that the only time one would make use of the var keyword is when defining data returned from a LINQ query. Remember, if you know you need an int, just declare an int! Overuse of implicit typing (via the var keyword) is considered by most developers to be poor style in production code.  Source Code The ImplicitlyTypedLocalVars project can be found under the Chapter 3 subdirectory. C# Iteration Constructs All programming languages provide ways to repeat blocks of code until a terminating condition has been met. Regardless of which language you have used in the past, I would guess the C# iteration statements should not raise too many eyebrows and should require little explanation. C# provides the following four iteration constructs: • for loop • foreach/in loop • while loop • do/while loop Let’s quickly examine each looping construct in turn, using a new Console Application project named IterationsAndDecisions.  Note I will keep this final section of the chapter short and to the point, as I am assuming you have experience using similar keywords (if, for, switch, etc.) in your current programming language. If you require more information, look up the topics “Iteration Statements (C# Reference),” “Jump Statements (C# Reference),” and “Selection Statements (C# Reference)” within the .NET Framework 4.5 SDK documentation. The for Loop When you need to iterate over a block of code a fixed number of times, the for statement provides a good deal of flexibility. In essence, you are able to specify how many times a block of code repeats itself, as well as the terminating condition. Without belaboring the point, here is a sample of the syntax. // A basic for loop. static void ForLoopExample() { // Note! “i“ is only visible within the scope of the for loop. CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 113 for(int i = 0; i < 4; i++) { Console.WriteLine(“Number is: {0} “, i); } // “i“ is not visible here. } All of your old C, C++, and Java tricks still hold when building a C# for statement. You can create complex terminating conditions, build endless loops, loop in reverse (via the -- operator) and make use of the goto, continue, and break jump keywords. The foreach Loop The C# foreach keyword allows you to iterate over all items in a container without the need to test for an upper limit. Unlike a for loop however, the foreach loop will only walk the container in a linear (n+1) fashion (thus, you cannot go backward through the container, skip every third element, or whatnot). However, when you simply need to walk a collection item-by-item, the foreach loop is the perfect choice. Here are two examples using foreach—one to traverse an array of strings and the other to traverse an array of integers. Notice that the data type before the in keyword represents the type of data in the container: // Iterate array items using foreach. static void ForEachLoopExample() { string[] carTypes = {“Ford“, “BMW“, “Yugo“, “Honda“ }; foreach (string c in carTypes) Console.WriteLine(c); int[] myInts = { 10, 20, 30, 40 }; foreach (int i in myInts) Console.WriteLine(i); } The item after the in keyword can be a simple array (seen here) or more specifically, any class implementing the IEnumerable interface. As you will see in Chapter 9, the .NET base class libraries ship with a number of collections that contain implementations of common abstract data types (ADTs). Any of these items (such as the generic List) can be used within a foreach loop. Use of Implicit Typing Within foreach Constructs It is also possible to make use of implicit typing within a foreach looping construct. As you would expect, the compiler will correctly infer the correct “type of type.” Recall the LINQ example method seen earlier in this chapter. Given that we don’t know the exact underlying data type of the subset variable, we can iterate over the result set using implicit typing: static void LinqQueryOverInts() { int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 }; // LINQ query! var subset = from i in numbers where i < 10 select i; CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 114 Console.Write(“Values in subset: “); foreach (var i in subset) { Console.Write(“{0} “, i); } } The while and do/while Looping Constructs The while looping construct is useful should you wish to execute a block of statements until some terminating condition has been reached. Within the scope of a while loop, you will need to ensure this terminating event is indeed established; otherwise, you will be stuck in an endless loop. In the following example, the message “In while loop“ will be continuously printed until the user terminates the loop by entering yes at the command prompt: static void WhileLoopExample() { string userIsDone = ““; // Test on a lower-class copy of the string. while(userIsDone.ToLower() != “yes“) { Console.WriteLine(“In while loop“); Console.Write(“Are you done? [yes] [no]: “); userIsDone = Console.ReadLine(); } } Closely related to the while loop is the do/while statement. Like a simple while loop, do/while is used when you need to perform some action an undetermined number of times. The difference is that do/while loops are guaranteed to execute the corresponding block of code at least once. In contrast, it is possible that a simple while loop may never execute if the terminating condition is false from the onset. static void DoWhileLoopExample() { string userIsDone = ““; do { Console.WriteLine(“In do/while loop“); Console.Write(“Are you done? [yes] [no]: “); userIsDone = Console.ReadLine(); }while(userIsDone.ToLower() != “yes“); // Note the semicolon! } Decision Constructs and the Relational/Equality Operators Now that you can iterate over a block of statements, the next related concept is how to control the flow of program execution. C# defines two simple constructs to alter the flow of your program, based on various contingencies: CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 115 • The if/else statement • The switch statement The if/else Statement First up is the if/else statement. Unlike in C and C++, the if/else statement in C# operates only on Boolean expressions, not ad hoc values such as –1 or 0. Equality and Relational Operators C# if/else statements typically involve the use of the C# operators shown in Table 3-7 in order to obtain a literal Boolean value. Table 3-7. C# Relational and Equality Operators C# Equality/Relational Operator Example Usage Meaning in Life == if(age == 30) Returns true only if each expression is the same. != if(“Foo“ != myStr) Returns true only if each expression is different. < > <= >= if(bonus < 2000) if(bonus > 2000) if(bonus <= 2000) if(bonus >= 2000) Returns true if expression A (bonus) is less than, greater than, less than or equal to, or greater than or equal to expression B (2000). Again, C and C++ programmers need to be aware that the old tricks of testing a condition for a value not equal to zero will not work in C#. Let’s say you want to see whether the string you are working with is longer than zero characters. You might be tempted to write: static void IfElseExample() { // This is illegal, given that Length returns an int, not a bool. string stringData = “My textual data“; if(stringData.Length) { Console.WriteLine(“string is greater than 0 characters“); } } If you wish to make use of the String.Length property to determine truth or falsity, you need to modify your conditional expression to resolve to a Boolean. // Legal, as this resolves to either true or false. if(stringData.Length > 0) { Console.WriteLine(“string is greater than 0 characters“); } CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 116 Conditional Operators An if statement may be composed of complex expressions as well and can contain else statements to perform more complex testing. The syntax is identical to C(++) and Java. To build complex expressions, C# offers an expected set of conditional logical operators, as shown in Table 3-8. Table 3-8. C# Conditional Operators Operator Example Meaning in Life && if(age == 30 && name == “Fred“) AND operator. Returns true if all expressions are true. || if(age == 30 || name == “Fred“) OR operator. Returns true if at least one expression is true. ! if(!myBool) NOT operator. Returns true if false, or false if true.  Note The && and || operators both “short circuit” when necessary. This means that after a complex expression has been determined to be false, the remaining subexpressions will not be checked. If you require all expressions to be tested regardless, you can use the related & and | operators. The switch Statement The other simple selection construct offered by C# is the switch statement. As in other C-based languages, the switch statement allows you to handle program flow based on a predefined set of choices. For example, the following Main() logic prints a specific string message based on one of two possible selections (the default case handles an invalid selection). // Switch on a numerical value. static void SwitchExample() { Console.WriteLine(“1 [C#], 2 [VB]“); Console.Write(“Please pick your language preference: “); string langChoice = Console.ReadLine(); int n = int.Parse(langChoice); switch (n) { case 1: Console.WriteLine(“Good choice, C# is a fine language.“); break; case 2: CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 117 Console.WriteLine(“VB: OOP, multithreading, and more!“); break; default: Console.WriteLine(“Well...good luck with that!“); break; } }  Note C# demands that each case (including default) that contains executable statements have a terminating break or goto to avoid fall-through. One nice feature of the C# switch statement is that you can evaluate string data in addition to numeric data. Here is an updated switch statement that does this very thing (notice we have no need to parse the user data into a numeric value with this approach). static void SwitchOnStringExample() { Console.WriteLine(“C# or VB“); Console.Write(“Please pick your language preference: “); string langChoice = Console.ReadLine(); switch (langChoice) { case “C#“: Console.WriteLine(“Good choice, C# is a fine language.“); break; case “VB“: Console.WriteLine(“VB: OOP, multithreading and more!“); break; default: Console.WriteLine(“Well...good luck with that!“); break; } } It is also possible to switch on an enumeration data type. As you will see in Chapter 4, the C# enum keyword allows you to define a custom set of name/value pairs. To whet your appetite, consider the following final helper function, which performs a switch test on the System.DayOfWeek enum. You’ll notice some syntax we have not yet examined, but focus on the issue of switching over the enum itself; the missing pieces will be filled in over the chapters to come: static void SwitchOnEnumExample() { Console.Write(“Enter your favorite day of the week: “); DayOfWeek favDay; try { CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 118 favDay = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), Console.ReadLine()); } catch (Exception) { Console.WriteLine(“Bad input!“); return; } switch (favDay) { case DayOfWeek.Friday: Console.WriteLine(“Yes, Friday rules!“); break; case DayOfWeek.Monday: Console.WriteLine(“Another day, another dollar“); break; case DayOfWeek.Saturday: Console.WriteLine(“Great day indeed.“); break; case DayOfWeek.Sunday: Console.WriteLine(“Football!!“); break; case DayOfWeek.Thursday: Console.WriteLine(“Almost Friday...“); break; case DayOfWeek.Tuesday: Console.WriteLine(“At least it is not Monday“); break; case DayOfWeek.Wednesday: Console.WriteLine(“A fine day.“); break; } }  Source Code The IterationsAndDecisions project is located under the Chapter 3 subdirectory. Summary The goal of this chapter was to expose you to numerous core aspects of the C# programming language. Here, we examined the commonplace constructs in any application you may be interested in building. After examining the role of an application object, you learned that every C# executable program must have a type defining a Main() method, which serves as the program’s entry point. Within the scope of Main(), you typically create any number of objects that work together to breathe life into your application. Next, we dove into the details of the built-in data types of C# and came to understand that each data type keyword (e.g., int) is really a shorthand notation for a full-blown type in the System namespace (System.Int32, in this case). Given this, each C# data type has a number of built-in members. Along the CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I 119 same vein, you also learned about the role of widening and narrowing, as well as the role of the checked and unchecked keywords. We wrapped up by checking out the role of implicit typing using the var keyword. As we discussed, the most useful place for implicit typing is when working with the LINQ programming model. Finally, we quickly examined the various iteration and decision constructs supported by C#. Now that you have an understanding of some of the basic nuts and bolts, the next chapter (Chapter 4) will complete our examination of core language features. After that, you will be well prepared to examine the object-oriented features of C# beginning in Chapter 5. C H A P T E R 4 121 Core C# Programming Constructs, Part II This chapter picks up where the Chapter 3 left off, and completes your investigation of the core aspects of the C# programming language. We begin by examining various details regarding the construction of C# methods, exploring the out, ref, and params keywords. Along the way, you will also examine the role of optional and named parameters. After you examine the topic of method overloading, the next task is to investigate the details behind manipulating arrays using the syntax of C# and get to know the functionality contained within the related System.Array class type. In addition, this chapter provides a discussion regarding the construction of enumeration and structure types, including a fairly detailed examination of the distinction between a value type and a reference type. We wrap this up by examining the role of nullable data types and the ? and ?? operators. After you have completed this chapter, you will be in a perfect position to learn the object-oriented capabilities of C#, beginning in Chapter 5. Methods and Parameter Modifiers To begin this chapter, let’s examine the details of defining methods. Just like the Main() method (see Chapter 3), your custom methods may or may not take parameters and may or may not return values to the caller. As you will see over the next several chapters, methods can be implemented within the scope of classes or structures (as well as prototyped within interface types) and may be decorated with various keywords (e.g., static, virtual, public, new) to qualify their behavior. At this point in the text, each of our methods has followed the following basic format: // Recall that static methods can be called directly // without creating a class instance. class Program { // static returnType MethodName(paramater list) { /* Implementation */ } static int Add(int x, int y){ return x + y; } } While the definition of a method in C# is quite straightforward, there are a handful of keywords that you can use to control how arguments are passed to the method in question. These are listed in Table 4-1. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 122 Table 4-1. C# Parameter Modifiers Parameter Modifier Meaning in Life (None) If a parameter is not marked with a parameter modifier, it is assumed to be passed by value, meaning the called method receives a copy of the original data. out Output parameters must be assigned by the method being called, and therefore, are passed by reference. If the called method fails to assign output parameters, you are issued a compiler error. ref The value is initially assigned by the caller and may be optionally reassigned by the called method (as the data is also passed by reference). No compiler error is generated if the called method fails to assign a ref parameter. params This parameter modifier allows you to send in a variable number of arguments as a single logical parameter. A method can have only a single params modifier, and it must be the final parameter of the method. In reality, you might not need to use the params modifier all too often; however, be aware that numerous methods within the base class libraries do make use of this C# language feature. To illustrate the use of these keywords, create a new Console Application project named FunWithMethods. Now, let’s walk through the role of each keyword. The Default by Value Parameter-Passing Behavior The default manner in which a parameter is sent into a function is by value. Simply put, if you do not mark an argument with a parameter modifier, a copy of the data is passed into the function. As explained at the end of this chapter, exactly what is copied will depend on whether the parameter is a value type or a reference type. For the time being, assume the following method within the Program class that operates on two numerical data types passed by value: // Arguments are passed by value by default. static int Add(int x, int y) { int ans = x + y; // Caller will not see these changes // as you are modifying a copy of the // original data. x = 10000; y = 88888; return ans; } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 123 Numerical data falls under the category of value types. Therefore, if you change the values of the parameters within the scope of the member, the caller is blissfully unaware, given that you are changing the values on a copy of the caller’s original data: static void Main(string[] args) { Console.WriteLine(“***** Fun with Methods *****\n“); // Pass two variables in by value. int x = 9, y = 10; Console.WriteLine(“Before call: X: {0}, Y: {1}“, x, y); Console.WriteLine(“Answer is: {0}“, Add(x, y)); Console.WriteLine(“After call: X: {0}, Y: {1}“, x, y); Console.ReadLine(); } As you would hope, the values of x and y remain identical before and after the call to Add(), as shown in the following output, as the data points were sent in by value. Thus, any changes on these parameters within the Add() method are not seen by the caller, as the Add() method is operating on a copy of the data. ***** Fun with Methods ***** Before call: X: 9, Y: 10 Answer is: 19 After call: X: 9, Y: 10 The out Modifier Next, you have the use of output parameters. Methods that have been defined to take output parameters (via the out keyword) are under obligation to assign them to an appropriate value before exiting the method scope (if you fail to do so, you will receive compiler errors). To illustrate, here is an alternative version of the Add() method that returns the sum of two integers using the C# out modifier (note the physical return value of this method is now void): // Output parameters must be assigned by the called method. static void Add(int x, int y, out int ans) { ans = x + y; } Calling a method with output parameters also requires the use of the out modifier. However, the local variables that are passed as output variables are not required to be assigned before passing them in as output arguments (if you do so, the original value is lost after the call). The reason the compiler allows you to send in seemingly unassigned data is due to the fact that the method being called must make an assignment. The following code is an example: CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 124 static void Main(string[] args) { Console.WriteLine(“***** Fun with Methods *****“); ... // No need to assign initial value to local variables // used as output parameters, provided the first time // you use them is as output arguments. int ans; Add(90, 90, out ans); Console.WriteLine(“90 + 90 = {0}“, ans); Console.ReadLine(); } The previous example is intended to be illustrative in nature; you really have no reason to return the value of your summation using an output parameter. However, the C# out modifier does serve a very useful purpose: it allows the caller to obtain multiple outputs from a single method invocation. // Returning multiple output parameters. static void FillTheseValues(out int a, out string b, out bool c) { a = 9; b = “Enjoy your string.“; c = true; } The caller would be able to invoke the FillTheseValues() method. Remember that you must use the out modifier when you invoke the method, as well as when you implement the method: static void Main(string[] args) { Console.WriteLine(“***** Fun with Methods *****“); ... int i; string str; bool b; FillTheseValues(out i, out str, out b); Console.WriteLine(“Int is: {0}“, i); Console.WriteLine(“String is: {0}“, str); Console.WriteLine(“Boolean is: {0}“, b); Console.ReadLine(); } Finally, always remember that a method that defines output parameters must assign the parameter to a valid value before exiting the method scope. Therefore, the following code will result in a compiler error, as the output parameter has not been assigned within the method scope: static void ThisWontCompile(out int a) { Console.WriteLine(“Error! Forgot to assign output arg!“); } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 125 The ref Modifier Now consider the use of the C# ref parameter modifier. Reference parameters are necessary when you wish to allow a method to operate on (and usually change the values of) various data points declared in the caller’s scope (such as a sorting or swapping routine). Note the distinction between output and reference parameters: • Output parameters do not need to be initialized before they passed to the method. The reason for this is that the method must assign output parameters before exiting. • Reference parameters must be initialized before they are passed to the method. The reason for this is that you are passing a reference to an existing variable. If you don’t assign it to an initial value, that would be the equivalent of operating on an unassigned local variable. Let’s check out the use of the ref keyword by way of a method that swaps two string variables (of course, any two data types could be used here, including int, bool, float, and so on): // Reference parameters. public static void SwapStrings(ref string s1, ref string s2) { string tempStr = s1; s1 = s2; s2 = tempStr; } This method can be called as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Methods *****“); ... string str1 = “Flip“; string str2 = “Flop“; Console.WriteLine(“Before: {0}, {1} “, str1, str2); SwapStrings(ref str1, ref str2); Console.WriteLine(“After: {0}, {1} “, str1, str2); Console.ReadLine(); } Here, the caller has assigned an initial value to local string data (str1 and str2). After the call to SwapStrings() returns, str1 now contains the value “Flop“, while str2 reports the value “Flip“: Before: Flip, Flop After: Flop, Flip CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 126  Note The C# ref keyword will be revisited later in this chapter in the section “Understanding Value Types and Reference Types.” As you will see, the behavior of this keyword changes just a bit depending on whether the argument is a value type or reference type. The params Modifier C# supports the use of parameter arrays using the params keyword. To understand this language feature, you must (as the name implies) understand how to manipulate C# arrays. If this is not the case, you might wish to return to this section after you read the section “Array Manipulation in C#,” later in this chapter. The params keyword allows you to pass into a method a variable number of identically typed parameters (or classes related by inheritance) as a single logical parameter. As well, arguments marked with the params keyword can be processed if the caller sends in a strongly typed array or a comma- delimited list of items. Yes, this can be confusing! To clear things up, assume you wish to create a function that allows the caller to pass in any number of arguments and return the calculated average. If you were to prototype this method to take an array of doubles, this would force the caller to first define the array, then fill the array, and finally pass it into the method. However, if you define CalculateAverage() to take a params of double[] data types, the caller can simply pass a comma- delimited list of doubles. The .NET runtime will automatically package the set of doubles into an array of type double behind the scenes: // Return average of “some number“ of doubles. static double CalculateAverage(params double[] values) { Console.WriteLine(“You sent me {0} doubles.“, values.Length); double sum = 0; if(values.Length == 0) return sum; for (int i = 0; i < values.Length; i++) sum += values[i]; return (sum / values.Length); } This method has been defined to take a parameter array of doubles. What this method is in fact saying is, “Send me any number of doubles (including zero) and I’ll compute the average.” Given this, you can call CalculateAverage() in any of the following ways: static void Main(string[] args) { Console.WriteLine(“***** Fun with Methods *****“); ... // Pass in a comma-delimited list of doubles... double average; average = CalculateAverage(4.0, 3.2, 5.7, 64.22, 87.2); Console.WriteLine(“Average of data is: {0}“, average); CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 127 // ...or pass an array of doubles. double[] data = { 4.0, 3.2, 5.7 }; average = CalculateAverage(data); Console.WriteLine(“Average of data is: {0}“, average); // Average of 0 is 0! Console.WriteLine(“Average of data is: {0}“, CalculateAverage()); Console.ReadLine(); } If you did not make use of the params modifier in the definition of CalculateAverage(), the first invocation of this method would result in a compiler error, as the compiler would be looking for a version of CalculateAverage() that took five double arguments.  Note To avoid any ambiguity, C# demands a method only support single params argument, which must be the final argument in the parameter list. As you might guess, this technique is nothing more than a convenience for the caller, given that the array is created by the CLR as necessary. By the time the array is within the scope of the method being called, you are able to treat it as a full-blown .NET array that contains all the functionality of the System.Array base class library type. Consider the following output: You sent me 5 doubles. Average of data is: 32.864 You sent me 3 doubles. Average of data is: 4.3 You sent me 0 doubles. Average of data is: 0 Defining Optional Parameters C# allows you to create methods that can take optional arguments. This technique allows the caller to invoke a single method while omitting arguments deemed unnecessary, provided the caller is happy with the specified defaults.  Note As you will see in Chapter 16, a key motivation for adding optional arguments to C# is to simplify interacting with COM objects. Several Microsoft object models (e.g., Microsoft Office) expose their functionality via COM objects, many of which were written long ago to make use of optional parameters, which earlier versions of C# did not support. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 128 To illustrate working with optional arguments, assume you have a method named EnterLogData(), which defines a single optional parameter: static void EnterLogData(string message, string owner = “Programmer“) { Console.Beep(); Console.WriteLine(“Error: {0}“, message); Console.WriteLine(“Owner of Error: {0}“, owner); } Here, the final string argument has been assigned the default value of “Programmer“, via an assignment within the parameter definition. Given this, we can call EnterLogData() from within Main() in two manners: static void Main(string[] args) { Console.WriteLine(“***** Fun with Methods *****“); ... EnterLogData(“Oh no! Grid can't find data“); EnterLogData(“Oh no! I can't find the payroll data“, “CFO“); Console.ReadLine(); } Because the first invocation of EnterLogData() did not specify a second string argument, we would find that the programmer is the one responsible for losing data for the grid, while the CFO misplaced the payroll data (as specified by the second argument in the second method call). One very important thing to be aware of, is that the value assigned to an optional parameter must be known at compile time, and cannot be resolved at runtime (if you attempt to do so, you’ll receive compile-time errors!). To illustrate, assume you wish to update EnterLogData() with the following extra optional parameter: // Error! The default value for an optional arg must be known // at compile time! static void EnterLogData(string message, string owner = “Programmer“, DateTime timeStamp = DateTime.Now) { Console.Beep(); Console.WriteLine(“Error: {0}“, message); Console.WriteLine(“Owner of Error: {0}“, owner); Console.WriteLine(“Time of Error: {0}“, timeStamp); } This will not compile, as the value of the Now property of the DateTime class is resolved at runtime, not compile time.  Note To avoid ambiguity, optional parameters must always be packed onto the end of a method signature. It is a compiler error to have optional parameters listed before nonoptional parameters. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 129 Invoking Methods Using Named Parameters Another language feature found in C# is support for named arguments. To be honest, at first glance, this language construct might appear to do little more than result in confusing code. And to continue being completely honest, this could be the case! Similar to optional arguments, including support for named parameters is largely motivated by the desire to simplify the process of working with the COM interoperability layer (again, see Chapter 16). Named arguments allow you to invoke a method by specifying parameter values in any order you choose. Thus, rather than passing parameters solely by position (as you will do in most cases), you can choose to specify each argument by name using a colon operator. To illustrate the use of named arguments, assume we have added the following method to the Program class: static void DisplayFancyMessage(ConsoleColor textColor, ConsoleColor backgroundColor, string message) { // Store old colors to restore after message is printed. ConsoleColor oldTextColor = Console.ForegroundColor; ConsoleColor oldbackgroundColor = Console.BackgroundColor; // Set new colors and print message. Console.ForegroundColor = textColor; Console.BackgroundColor = backgroundColor; Console.WriteLine(message); // Restore previous colors. Console.ForegroundColor = oldTextColor; Console.BackgroundColor = oldbackgroundColor; } Now, the way DisplayFancyMessage() was written, you would expect the caller to invoke this method by passing two ConsoleColor variables followed by a string type. However, using named arguments, the following calls are completely fine: static void Main(string[] args) { Console.WriteLine(“***** Fun with Methods *****“); ... DisplayFancyMessage(message: “Wow! Very Fancy indeed!“, textColor: ConsoleColor.DarkRed, backgroundColor: ConsoleColor.White); DisplayFancyMessage(backgroundColor: ConsoleColor.Green, message: “Testing...“, textColor: ConsoleColor.DarkBlue); Console.ReadLine(); } One minor gotcha regarding named arguments is that if you begin to invoke a method using positional parameters, they must be listed before any named parameters. In other words, named arguments must always be packed onto the end of a method call. The following code is an example: CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 130 // This is OK, as positional args are listed before named args. DisplayFancyMessage(ConsoleColor.Blue, message: “Testing...“, backgroundColor: ConsoleColor.White); // This is an ERROR, as positional args are listed after named args. DisplayFancyMessage(message: “Testing...“, backgroundColor: ConsoleColor.White, ConsoleColor.Blue); This restriction aside, you might still be wondering when you would ever want to use this language feature. After all, if you need to specify three arguments to a method, why bother flipping around their position? Well, as it turns out, if you have a method that defines optional arguments, this feature can actually be really helpful. Assume DisplayFancyMessage() has been rewritten to now support optional arguments, as you have assigned fitting defaults: static void DisplayFancyMessage(ConsoleColor textColor = ConsoleColor.Blue, ConsoleColor backgroundColor = ConsoleColor.White, string message = “Test Message“) { ... } Given that each argument has a default value, named arguments allow the caller to specify only the parameter(s) for which they do not wish to receive the defaults. Therefore, if the caller wants the value “Hello!“ to appear in blue text surrounded by a white background, they can simply specify: DisplayFancyMessage(message: “Hello!“); Or, if the caller wants to see “Test Message” print out with a green background containing blue text, they can invoke DisplayFancyMessage(): DisplayFancyMessage(backgroundColor: ConsoleColor.Green); As you can see, optional arguments and named parameters do tend to work hand in hand. To wrap up our examination of building C# methods, I need to address the topic of method overloading.  Source Code The FunWithMethods application is located under the Chapter 4 subdirectory. Understanding Method Overloading Like other modern object-oriented languages, C# allows a method to be overloaded. Simply put, when you define a set of identically named methods that differ by the number (or type) of parameters, the method in question is said to be overloaded. To understand why overloading is so useful, consider life as an old-school Visual Basic 6.0 (VB6) developer. Assume you are using VB6 to build a set of methods that return the sum of various incoming data types (Integers, Doubles, and so on). Given that VB6 does not support method overloading, you would be required to define a unique set of methods that essentially do the same thing (return the sum of the arguments): CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 131 ' VB6 code examples. Public Function AddInts(ByVal x As Integer, ByVal y As Integer) As Integer AddInts = x + y End Function Public Function AddDoubles(ByVal x As Double, ByVal y As Double) As Double AddDoubles = x + y End Function Public Function AddLongs(ByVal x As Long, ByVal y As Long) As Long AddLongs = x + y End Function Not only can code such as this become tough to maintain, but the caller must now be painfully aware of the name of each method. Using overloading, you are able to allow the caller to call a single method named Add(). Again, the key is to ensure that each version of the method has a distinct set of arguments (methods differing only by return type are not unique enough).  Note As explained in Chapter 9, it is possible to build generic methods that take the concept of overloading to the next level. Using generics, you can define type placeholders for a method implementation that are specified at the time you invoke the member in question. To check this out firsthand, create a new Console Application project named MethodOverloading. Now, consider the following class definition: // C# code. class Program { static void Main(string[] args) { } // Overloaded Add() method. static int Add(int x, int y) { return x + y; } static double Add(double x, double y) { return x + y; } static long Add(long x, long y) { return x + y; } } The caller can now simply invoke Add() with the required arguments and the compiler is happy to comply, given the fact that the compiler is able to resolve the correct implementation to invoke given the provided arguments: CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 132 static void Main(string[] args) { Console.WriteLine(“***** Fun with Method Overloading *****\n“); // Calls int version of Add() Console.WriteLine(Add(10, 10)); // Calls long version of Add() Console.WriteLine(Add(900000000000, 900000000000)); // Calls double version of Add() Console.WriteLine(Add(4.3, 4.4)); Console.ReadLine(); } The Visual Studio IDE provides assistance when calling overloaded methods to boot. When you type in the name of an overloaded method (such as our good friend Console.WriteLine()), IntelliSense will list each version of the method in question. Note that you are able to cycle through each version of an overloaded method using the up and down arrow keys shown in Figure 4-1. Figure 4-1. Visual Studio IntelliSense for overloaded methods  Source Code The MethodOverloading application is located under the Chapter 4 subdirectory. That wraps up our initial examination of building methods using the syntax of C#. Next, let’s check out how to build and manipulate arrays, enumerations, and structures. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 133 Understanding C# Arrays As I would guess you are already aware, an array is a set of data items, accessed using a numerical index. More specifically, an array is a set of contiguous data points of the same type (an array of ints, an array of strings, an array of SportsCars, and so on). Declaring, filling, and accessing an array with C# is quite straightforward. To illustrate, create a new Console Application project (named FunWithArrays) that contains a helper method named SimpleArrays(), invoked from within Main(): class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with Arrays *****“); SimpleArrays(); Console.ReadLine(); } static void SimpleArrays() { Console.WriteLine(“=> Simple Array Creation.“); // Create an array of ints containing 3 elements indexed 0, 1, 2 int[] myInts = new int[3]; // Create a 100 item string array, indexed 0 - 99 string[] booksOnDotNet = new string[100]; Console.WriteLine(); } } Look closely at the previous code comments. When declaring a C# array using this syntax, the number used in the array declaration represents the total number of items, not the upper bound. Also note that the lower bound of an array always begins at 0. Thus, when you write int[] myInts = new int[3], you end up with an array holding three elements, indexed at positions 0, 1, 2. After you have defined an array variable, you are then able to fill the elements index by index as shown in the updated SimpleArrays() method: static void SimpleArrays() { Console.WriteLine(“=> Simple Array Creation.“); // Create and fill an array of 3 Integers int[] myInts = new int[3]; myInts[0] = 100; myInts[1] = 200; myInts[2] = 300; // Now print each value. foreach(int i in myInts) Console.WriteLine(i); Console.WriteLine(); } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 134  Note Do be aware that if you declare an array, but do not explicitly fill each index, each item will be set to the default value of the data type (e.g., an array of bools will be set to false or an array of ints will be set to 0). C# Array Initialization Syntax In addition to filling an array element by element, you are also able to fill the items of an array using C# array initialization syntax. To do so, specify each array item within the scope of curly brackets ({}). This syntax can be helpful when you are creating an array of a known size and wish to quickly specify the initial values. For example, consider the following alternative array declarations: static void ArrayInitialization() { Console.WriteLine(“=> Array Initialization.“); // Array initialization syntax using the new keyword. string[] stringArray = new string[] { “one“, “two“, “three“ }; Console.WriteLine(“stringArray has {0} elements“, stringArray.Length); // Array initialization syntax without using the new keyword. bool[] boolArray = { false, false, true }; Console.WriteLine(“boolArray has {0} elements“, boolArray.Length); // Array initialization with new keyword and size. int[] intArray = new int[4] { 20, 22, 23, 0 }; Console.WriteLine(“intArray has {0} elements“, intArray.Length); Console.WriteLine(); } Notice that when you make use of this “curly bracket” syntax, you do not need to specify the size of the array (seen when constructing the stringArray variable), given that this will be inferred by the number of items within the scope of the curly brackets. Also notice that use of the new keyword is optional (shown when constructing the boolArray type). In the case of the intArray declaration, again recall the numeric value specified represents the number of elements in the array, not the value of the upper bound. If there is a mismatch between the declared size and the number of initializers (whether you have too many or too few initializers), you are issued a compile-time error. The following is an example: // OOPS! Mismatch of size and elements! int[] intArray = new int[2] { 20, 22, 23, 0 }; Implicitly Typed Local Arrays In Chapter 3, you learned about the topic of implicitly typed local variables. Recall that the var keyword allows you to define a variable, whose underlying type is determined by the compiler. In a similar vein, the var keyword can be used to define implicitly typed local arrays. Using this technique, you can allocate a new array variable without specifying the type contained within the array itself: CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 135 static void DeclareImplicitArrays() { Console.WriteLine(“=> Implicit Array Initialization.“); // a is really int[]. var a = new[] { 1, 10, 100, 1000 }; Console.WriteLine(“a is a: {0}“, a.ToString()); // b is really double[]. var b = new[] { 1, 1.5, 2, 2.5 }; Console.WriteLine(“b is a: {0}“, b.ToString()); // c is really string[]. var c = new[] { “hello“, null, “world“ }; Console.WriteLine(“c is a: {0}“, c.ToString()); Console.WriteLine(); } Of course, just as when you allocate an array using explicit C# syntax, the items in the array’s initialization list must be of the same underlying type (e.g., all ints, all strings, or all SportsCars). Unlike what you might be expecting, an implicitly typed local array does not default to System.Object; thus, the following generates a compile-time error: // Error! Mixed types! var d = new[] { 1, “one“, 2, “two“, false }; Defining an Array of Objects In most cases, when you define an array, you do so by specifying the explicit type of item that can be within the array variable. While this seems quite straightforward, there is one notable twist. As you will come to understand in Chapter 6, System.Object is the ultimate base class to each and every type (including fundamental data types) in the .NET type system. Given this fact, if you were to define an array of System.Object data types, the subitems could be anything at all. Consider the following ArrayOfObjects() method (which again can be invoked from Main() for testing): static void ArrayOfObjects() { Console.WriteLine(“=> Array of Objects.“); // An array of objects can be anything at all. object[] myObjects = new object[4]; myObjects[0] = 10; myObjects[1] = false; myObjects[2] = new DateTime(1969, 3, 24); myObjects[3] = “Form & Void“; foreach (object obj in myObjects) { // Print the type and value for each item in array. Console.WriteLine(“Type: {0}, Value: {1}“, obj.GetType(), obj); } Console.WriteLine(); } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 136 Here, as you are iterating over the contents of myObjects, you print out the underlying type of each item using the GetType() method of System.Object, as well as the value of the current item. Without going into too much detail regarding System.Object.GetType() at this point in the text, simply understand that this method can be used to obtain the fully qualified name of the item (Chapter 15 examines the topic of type information and reflection services in detail). The following output shows the result of calling ArrayOfObjects(). => Array of Objects. Type: System.Int32, Value: 10 Type: System.Boolean, Value: False Type: System.DateTime, Value: 3/24/1969 12:00:00 AM Type: System.String, Value: Form & Void Working with Multidimensional Arrays In addition to the single-dimension arrays you have seen thus far, C# also supports two varieties of multidimensional arrays. The first of these is termed a rectangular array, which is simply an array of multiple dimensions, where each row is of the same length. To declare and fill a multidimensional rectangular array, proceed as follows: static void RectMultidimensionalArray() { Console.WriteLine(“=> Rectangular multidimensional array.“); // A rectangular MD array. int[,] myMatrix; myMatrix = new int[6,6]; // Populate (6 * 6) array. for(int i = 0; i < 6; i++) for(int j = 0; j < 6; j++) myMatrix[i, j] = i * j; // Print (6 * 6) array. for(int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) Console.Write(myMatrix[i, j] + “\t“); Console.WriteLine(); } Console.WriteLine(); } The second type of multidimensional array is termed a jagged array. As the name implies, jagged arrays contain some number of inner arrays, each of which may have a different upper limit. For example: static void JaggedMultidimensionalArray() { Console.WriteLine(“=> Jagged multidimensional array.“); // A jagged MD array (i.e., an array of arrays). // Here we have an array of 5 different arrays. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 137 int[][] myJagArray = new int[5][]; // Create the jagged array. for (int i = 0; i < myJagArray.Length; i++) myJagArray[i] = new int[i + 7]; // Print each row (remember, each element is defaulted to zero!). for(int i = 0; i < 5; i++) { for(int j = 0; j < myJagArray[i].Length; j++) Console.Write(myJagArray[i][j] + “ “); Console.WriteLine(); } Console.WriteLine(); } Figure 4-2 shows the output of calling each of the RectMultidimensionalArray() and JaggedMultidimensionalArray() methods within Main(). Figure 4-2. Rectangular and jagged multidimensional arrays Arrays As Arguments or Return Values After you have created an array, you are free to pass it as an argument or receive it as a member return value. For example, the following PrintArray() method takes an incoming array of ints and prints each member to the console, while the GetStringArray() method populates an array of strings and returns it to the caller: static void PrintArray(int[] myInts) { for(int i = 0; i < myInts.Length; i++) Console.WriteLine(“Item {0} is {1}“, i, myInts[i]); } static string[] GetStringArray() { string[] theStrings = {“Hello“, “from“, “GetStringArray“}; return theStrings; } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 138 These methods may be invoked as you would expect: static void PassAndReceiveArrays() { Console.WriteLine(“=> Arrays as params and return values.“); // Pass array as parameter. int[] ages = {20, 22, 23, 0} ; PrintArray(ages); // Get array as return value. string[] strs = GetStringArray(); foreach(string s in strs) Console.WriteLine(s); Console.WriteLine(); } At this point, hopefully you feel comfortable with the process of defining, filling, and examining the contents of a C# array variable. To complete the picture, let’s now examine the role of the System.Array class. The System.Array Base Class Every array you create gathers much of its functionality from the System.Array class. Using these common members, you are able to operate on an array using a consistent object model. Table 4-2 gives a rundown of some of the more interesting members (be sure to check the .NET Framework 4.5 SDK documentation for full details). Table 4-2. Select Members of System.Array Member of Array Class Meaning in Life Clear() This static method sets a range of elements in the array to empty values (0 for numbers, null for object references, false for booleans). CopyTo() This method is used to copy elements from the source array into the destination array. Length This property returns the number of items within the array. Rank This property returns the number of dimensions of the current array. Reverse() This static method reverses the contents of a one-dimensional array. Sort() This static method sorts a one-dimensional array of intrinsic types. If the elements in the array implement the IComparer interface, you can also sort your custom types (see Chapter 9). Let’s see some of these members in action. The following helper method makes use of the static Reverse() and Clear() methods to pump out information about an array of string types to the console: CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 139 static void SystemArrayFunctionality() { Console.WriteLine(“=> Working with System.Array.“); // Initialize items at startup. string[] gothicBands = {“Tones on Tail“, “Bauhaus“, “Sisters of Mercy“}; // Print out names in declared order. Console.WriteLine(“-> Here is the array:“); for (int i = 0; i < gothicBands.Length; i++) { // Print a name. Console.Write(gothicBands[i] + “, “); } Console.WriteLine(“\n“); // Reverse them... Array.Reverse(gothicBands); Console.WriteLine(“-> The reversed array“); // ... and print them. for (int i = 0; i < gothicBands.Length; i++) { // Print a name. Console.Write(gothicBands[i] + “, “); } Console.WriteLine(“\n“); // Clear out all but the final member. Console.WriteLine(“-> Cleared out all but one...“); Array.Clear(gothicBands, 1, 2); for (int i = 0; i < gothicBands.Length; i++) { // Print a name. Console.Write(gothicBands[i] + “, “); } Console.WriteLine(); } If you invoke this method from within Main(), you will get the output shown here: => Working with System.Array. -> Here is the array: Tones on Tail, Bauhaus, Sisters of Mercy, -> The reversed array Sisters of Mercy, Bauhaus, Tones on Tail, -> Cleared out all but one... Sisters of Mercy, , , CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 140 Notice that many members of System.Array are defined as static members and are, therefore, called at the class level (for example, the Array.Sort() or Array.Reverse() methods). Methods such as these are passed in the array you wish to process. Other methods of System.Array (such as the Length property) are bound at the object level, thus you are able to invoke the member directly on the array.  Source Code The FunWithArrays application is located under the Chapter 4 subdirectory. Understanding the enum Type Recall from Chapter 1 that the .NET type system is composed of classes, structures, enumerations, interfaces, and delegates. To begin exploration of these types, let’s check out the role of the enumeration (or simply, enum) using a new Console Application project named FunWithEnums.  Note Do not confuse the term “enum” with “enumerator”; they are completely different concepts. An enum is a custom data type of name/value pairs. An enumerator is a class or structure that implements a .NET interface named IEnumerable. Typically, this interface is implemented on collection classes, as well as the System.Array class. As you will see later in Chapter 8, objects that support IEnumerable can work within the foreach loop. When building a system, it is often convenient to create a set of symbolic names that map to known numerical values. For example, if you are creating a payroll system, you might want to refer to the type of employees using constants such as vice president, manager, contractor, and grunt. C# supports the notion of custom enumerations for this very reason. For example, here is an enumeration named EmpType: // A custom enumeration. enum EmpType { Manager, // = 0 Grunt, // = 1 Contractor, // = 2 VicePresident // = 3 } The EmpType enumeration defines four named constants, corresponding to discrete numerical values. By default, the first element is set to the value zero (0), followed by an n + 1 progression. You are free to change the initial value as you see fit. For example, if it made sense to number the members of EmpType as 102 through 105, you could do so as follows: // Begin with 102. enum EmpType { Manager = 102, Grunt, // = 103 CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 141 Contractor, // = 104 VicePresident // = 105 } Enumerations do not necessarily need to follow a sequential ordering, and need not have unique values. If (for some reason or another) it makes sense to establish your EmpType as shown here, the compiler continues to be happy: // Elements of an enumeration need not be sequential! enum EmpType { Manager = 10, Grunt = 1, Contractor = 100, VicePresident = 9 } Controlling the Underlying Storage for an enum By default, the storage type used to hold the values of an enumeration is a System.Int32 (the C# int); however, you are free to change this to your liking. C# enumerations can be defined in a similar manner for any of the core system types (byte, short, int, or long). For example, if you want to set the underlying storage value of EmpType to be a byte rather than an int, you can write the following: // This time, EmpType maps to an underlying byte. enum EmpType : byte { Manager = 10, Grunt = 1, Contractor = 100, VicePresident = 9 } Changing the underlying type of an enumeration can be helpful if you are building a .NET application that will be deployed to a low-memory device (such as a Windows Phone 7 device) and need to conserve memory wherever possible. Of course, if you do establish your enumeration to use a byte as storage, each value must be within its range! For example, the following version of EmpType will result in a compiler error, as the value 999 cannot fit within the range of a byte: // Compile-time error! 999 is too big for a byte! enum EmpType : byte { Manager = 10, Grunt = 1, Contractor = 100, VicePresident = 999 } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 142 Declaring enum Variables Once you have established the range and storage type of your enumeration, you can use it in place of so- called “magic numbers.” Because enumerations are nothing more than a user-defined data type, you are able to use them as function return values, method parameters, local variables, and so forth. Assume you have a method named AskForBonus(), taking an EmpType variable as the sole parameter. Based on the value of the incoming parameter, you will print out a fitting response to the pay bonus request: class Program { static void Main(string[] args) { Console.WriteLine(“**** Fun with Enums *****“); // Make a contractor type. EmpType emp = EmpType.Contractor; AskForBonus(emp); Console.ReadLine(); } // Enums as parameters. static void AskForBonus(EmpType e) { switch (e) { case EmpType.Manager: Console.WriteLine(“How about stock options instead?“); break; case EmpType.Grunt: Console.WriteLine(“You have got to be kidding...“); break; case EmpType.Contractor: Console.WriteLine(“You already get enough cash...“); break; case EmpType.VicePresident: Console.WriteLine(“VERY GOOD, Sir!“); break; } } } Notice that when you are assigning a value to an enum variable, you must scope the enum name (EmpType) to the value (Grunt). Because enumerations are a fixed set of name/value pairs, it is illegal to set an enum variable to a value that is not defined directly by the enumerated type: static void ThisMethodWillNotCompile() { // Error! SalesManager is not in the EmpType enum! EmpType emp = EmpType.SalesManager; // Error! Forgot to scope Grunt value to EmpType enum! emp = Grunt; } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 143 The System.Enum Type The interesting thing about .NET enumerations is that they gain functionality from the System.Enum class type. This class defines a number of methods that allow you to interrogate and transform a given enumeration. One helpful method is the static Enum.GetUnderlyingType(), which as the name implies, returns the data type used to store the values of the enumerated type (System.Byte in the case of the current EmpType declaration). static void Main(string[] args) { Console.WriteLine(“**** Fun with Enums *****“); // Make a contractor type. EmpType emp = EmpType.Contractor; AskForBonus(emp); // Print storage for the enum. Console.WriteLine(“EmpType uses a {0} for storage“, Enum.GetUnderlyingType(emp.GetType())); Console.ReadLine(); } If you were to consult the Visual Studio object browser, you would be able to verify that the Enum.GetUnderlyingType() method requires you to pass in a System.Type as the first parameter. As fully examined in Chapter 15, Type represents the metadata description of a given .NET entity. One possible way to obtain metadata (as shown previously) is to use the GetType() method, which is common to all types in the .NET base class libraries. Another approach is to make use of the C# typeof operator. One benefit of doing so is that you do not need to have a variable of the entity you wish to obtain a metadata description of: // This time use typeof to extract a Type. Console.WriteLine(“EmpType uses a {0} for storage“, Enum.GetUnderlyingType(typeof(EmpType))); Dynamically Discovering an enum’s Name/Value Pairs Beyond the Enum.GetUnderlyingType() method, all C# enumerations support a method named ToString(), which returns the string name of the current enumeration’s value. The following code is an example: static void Main(string[] args) { Console.WriteLine(“**** Fun with Enums *****“); EmpType emp = EmpType.Contractor; AskForBonus(emp); // Prints out “emp is a Contractor“. Console.WriteLine(“emp is a {0}.“, emp.ToString()); Console.ReadLine(); } If you are interested in discovering the value of a given enumeration variable, rather than its name, you can simply cast the enum variable against the underlying storage type. The following is an example: CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 144 static void Main(string[] args) { Console.WriteLine(“**** Fun with Enums *****“); EmpType emp = EmpType.Contractor; ... // Prints out “Contractor = 100“. Console.WriteLine(“{0} = {1}“, emp.ToString(), (byte)emp); Console.ReadLine(); }  Note The static Enum.Format() method provides a finer level of formatting options by specifying a desired format flag. Consult the .NET Framework 4.5 SDK documentation for full details of the System.Enum.Format() method. System.Enum also defines another static method named GetValues(). This method returns an instance of System.Array. Each item in the array corresponds to a member of the specified enumeration. Consider the following method, which will print out each name/value pair within any enumeration you pass in as a parameter: // This method will print out the details of any enum. static void EvaluateEnum(System.Enum e) { Console.WriteLine(“=> Information about {0}“, e.GetType().Name); Console.WriteLine(“Underlying storage type: {0}“, Enum.GetUnderlyingType(e.GetType())); // Get all name/value pairs for incoming parameter. Array enumData = Enum.GetValues(e.GetType()); Console.WriteLine(“This enum has {0} members.“, enumData.Length); // Now show the string name and associated value, using the D format // flag (see Chapter 3). for(int i = 0; i < enumData.Length; i++) { Console.WriteLine(“Name: {0}, Value: {0:D}“, enumData.GetValue(i)); } Console.WriteLine(); } To test this new method, update your Main() method to create variables of several enumeration types declared in the System namespace (as well as an EmpType enumeration for good measure). The following code is an example: static void Main(string[] args) { Console.WriteLine(“**** Fun with Enums *****“); ... CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 145 EmpType e2 = EmpType.Contractor; // These types are enums in the System namespace. DayOfWeek day = DayOfWeek.Monday; ConsoleColor cc = ConsoleColor.Gray; EvaluateEnum(e2); EvaluateEnum(day); EvaluateEnum(cc); Console.ReadLine(); } The output is shown in Figure 4-3. Figure 4-3. Dynamically discovering name/value pairs of enumeration types As you will see over the course of this text, enumerations are used extensively throughout the .NET base class libraries. For example, ADO.NET makes use of numerous enumerations to represent the state of a database connection (e.g., opened or closed) or the state of a row in a DataTable (e.g., changed, new, or detached). Therefore, when you make use of any enumeration, always remember that you are able to interact with the name/value pairs using the members of System.Enum. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 146  Source Code The FunWithEnums project is located under the Chapter 4 subdirectory. Understanding the Structure Type Now that you understand the role of enumeration types, let’s examine the use of .NET structures (or simply structs). Structure types are well suited for modeling mathematical, geometrical, and other “atomic” entities in your application. A structure (like an enumeration) is a user-defined type; however, structures are not simply a collection of name/value pairs. Rather, structures are types that can contain any number of data fields and members that operate on these fields.  Note If you have a background in OOP, you can think of a structure as a “lightweight class type,” given that structures provide a way to define a type that supports encapsulation, but cannot be used to build a family of related types. When you need to build a family of related types through inheritance, you will need to make use of class types. On the surface, the process of defining and using structures is very simple, but as they say, the devil is in the details. To begin understanding the basics of structure types, create a new project named FunWithStructures. In C#, structures are defined using the struct keyword. Define a new structure named Point, which defines two member variables of type int and a set of methods to interact with said data: struct Point { // Fields of the structure. public int X; public int Y; // Add 1 to the (X, Y) position. public void Increment() { X++; Y++; } // Subtract 1 from the (X, Y) position. public void Decrement() { X--; Y--; } // Display the current position. public void Display() { Console.WriteLine(“X = {0}, Y = {1}“, X, Y); CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 147 } } Here, you have defined your two integer fields (X and Y) using the public keyword, which is an access control modifier (Chapter 5 furthers this discussion). Declaring data with the public keyword ensures the caller has direct access to the data from a given Point variable (via the dot operator).  Note It is typically considered bad style to define public data within a class or structure. Rather, you will want to define private data, which can be accessed and changed using public properties. These details will be examined in Chapter 5. Here is a Main() method that takes our Point type out for a test drive: static void Main(string[] args) { Console.WriteLine(“***** A First Look at Structures *****\n“); // Create an initial Point. Point myPoint; myPoint.X = 349; myPoint.Y = 76; myPoint.Display(); // Adjust the X and Y values. myPoint.Increment(); myPoint.Display(); Console.ReadLine(); } The output is as you would expect: ***** A First Look at Structures ***** X = 349, Y = 76 X = 350, Y = 77 Creating Structure Variables When you want to create a structure variable, you have a variety of options. Here, you simply create a Point variable and assign each piece of public field data before invoking its members. If you do not assign each piece of public field data (X and Y in our case) before making use of the structure, you will receive a compiler error: // Error! Did not assign Y value. Point p1; p1.X = 10; CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 148 p1.Display(); // OK! Both fields assigned before use. Point p2; p2.X = 10; p2.Y = 10; p2.Display(); As an alternative, we can create structure variables using the C# new keyword, which will invoke the structure’s default constructor. By definition, a default constructor does not take any arguments. The benefit of invoking the default constructor of a structure is that each piece of field data is automatically set to its default value: // Set all fields to default values // using the default constructor. Point p1 = new Point(); // Prints X=0,Y=0. p1.Display(); It is also possible to design a structure with a custom constructor. This allows you to specify the values of field data upon variable creation, rather than having to set each data member field by field. Chapter 5 will provide a detailed examination of constructors; however, to illustrate, update the Point structure with the following code: struct Point { // Fields of the structure. public int X; public int Y; // A custom constructor. public Point(int XPos, int YPos) { X = XPos; Y = YPos; } ... } With this, we could now create Point variables, as follows: // Call custom constructor. Point p2 = new Point(50, 60); // Prints X=50,Y=60. p2.Display(); As mentioned, working with structures on the surface is quite simple. However, to deepen your understanding of this type, you need to explore the distinction between a .NET value type and a .NET reference type. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 149  Source Code The FunWithStructures project is located under the Chapter 4 subdirectory. Understanding Value Types and Reference Types  Note The following discussion of value types and reference types assumes that you have a background in object-oriented programming. If this is not the case, you might want to skip to the final section in this chapter, “Understanding C# Nullable Types,” and return to this section after you have read Chapters 5 and 6. Unlike arrays, strings, or enumerations, C# structures do not have an identically named representation in the .NET library (that is, there is no System.Structure class), but are implicitly derived from System.ValueType. Simply put, the role of System.ValueType is to ensure that the derived type (e.g., any structure) is allocated on the stack, rather than the garbage-collected heap. Simply put, data allocated on the stack can be created and destroyed very quickly, as its lifetime is determined by the defining scope. Heap allocated data, on the other hand, is monitored by the .NET garbage collector, and has a lifetime that is determined by a large number of factors, which will be examined in Chapter 13. Functionally, the only purpose of System.ValueType is to override the virtual methods defined by System.Object to use value-based, versus reference-based, semantics. As you might know, overriding is the process of changing the implementation of a virtual (or possibly abstract) method defined within a base class. The base class of ValueType is System.Object. In fact, the instance methods defined by System.ValueType are identical to those of System.Object: // Structures and enumerations implicitly extend System.ValueType. public abstract class ValueType : object { public virtual bool Equals(object obj); public virtual int GetHashCode(); public Type GetType(); public virtual string ToString(); } Given the fact that value types are using value-based semantics, the lifetime of a structure (which includes all numerical data types [int, float], as well as any enum or structure) is very predictable. When a structure variable falls out of the defining scope, it is removed from memory immediately: // Local structures are popped off // the stack when a method returns. static void LocalValueTypes() { // Recall! “int“ is really a System.Int32 structure. int i = 0; // Recall! Point is a structure type. Point p = new Point(); } // “i“ and “p“ popped off the stack here! CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 150 Value Types, References Types, and the Assignment Operator When you assign one value type to another, a member-by-member copy of the field data is achieved. In the case of a simple data type such as System.Int32, the only member to copy is the numerical value. However, in the case of your Point, the X and Y values are copied into the new structure variable. To illustrate, create a new Console Application project named ValueAndReferenceTypes, then copy your previous Point definition into your new namespace. Next, add the following method to your Program type: // Assigning two intrinsic value types results in // two independent variables on the stack. static void ValueTypeAssignment() { Console.WriteLine(“Assigning value types\n“); Point p1 = new Point(10, 10); Point p2 = p1; // Print both points. p1.Display(); p2.Display(); // Change p1.X and print again. p2.X is not changed. p1.X = 100; Console.WriteLine(“\n=> Changed p1.X\n“); p1.Display(); p2.Display(); } Here, you have created a variable of type Point (named p1) that is then assigned to another Point (p2). Because Point is a value type, you have two copies of the MyPoint type on the stack, each of which can be independently manipulated. Therefore, when you change the value of p1.X, the value of p2.X is unaffected: Assigning value types X = 10, Y = 10 X = 10, Y = 10 => Changed p1.X X = 100, Y = 10 X = 10, Y = 10 In stark contrast to value types, when you apply the assignment operator to reference types (meaning all class instances), you are redirecting what the reference variable points to in memory. To illustrate, create a new class type named PointRef that has the exact same members as the Point structures, beyond renaming the constructor to match the class name: // Classes are always reference types. class PointRef { // Same members as the Point structure... CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 151 // Be sure to change your constructor name to PointRef! public PointRef(int XPos, int YPos) { X = XPos; Y = YPos; } } Now, make use of your PointRef type within the following new method. Note that beyond using the PointRef class, rather than the Point structure, the code is identical to the ValueTypeAssignment() method. static void ReferenceTypeAssignment() { Console.WriteLine(“Assigning reference types\n“); PointRef p1 = new PointRef(10, 10); PointRef p2 = p1; // Print both point refs. p1.Display(); p2.Display(); // Change p1.X and print again. p1.X = 100; Console.WriteLine(“\n=> Changed p1.X\n“); p1.Display(); p2.Display(); } In this case, you have two references pointing to the same object on the managed heap. Therefore, when you change the value of X using the p1 reference, p2.X reports the same value. Assuming you have called this new method within Main(), your output should look like the following: Assigning reference types X = 10, Y = 10 X = 10, Y = 10 => Changed p1.X X = 100, Y = 10 X = 100, Y = 10 Value Types Containing Reference Types Now that you have a better feeling for the basic differences between value types and reference types, let’s examine a more complex example. Assume you have the following reference (class) type that maintains an informational string that can be set using a custom constructor: class ShapeInfo { public string infoString; CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 152 public ShapeInfo(string info) { infoString = info; } } Now assume that you want to contain a variable of this class type within a value type named Rectangle. To allow the caller to set the value of the inner ShapeInfo member variable, you also provide a custom constructor. Here is the complete definition of the Rectangle type: struct Rectangle { // The Rectangle structure contains a reference type member. public ShapeInfo rectInfo; public int rectTop, rectLeft, rectBottom, rectRight; public Rectangle(string info, int top, int left, int bottom, int right) { rectInfo = new ShapeInfo(info); rectTop = top; rectBottom = bottom; rectLeft = left; rectRight = right; } public void Display() { Console.WriteLine(“String = {0}, Top = {1}, Bottom = {2}, “ + “Left = {3}, Right = {4}“, rectInfo.infoString, rectTop, rectBottom, rectLeft, rectRight); } } At this point, you have contained a reference type within a value type. The million dollar question now becomes, what happens if you assign one Rectangle variable to another? Given what you already know about value types, you would be correct in assuming that the integer data (which is indeed a structure- System.Int32) should be an independent entity for each Rectangle variable. But what about the internal reference type? Will the object’s state be fully copied, or will the reference to that object be copied? To answer this question, define the following method and invoke it from Main(): static void ValueTypeContainingRefType() { // Create the first Rectangle. Console.WriteLine(“-> Creating r1“); Rectangle r1 = new Rectangle(“First Rect“, 10, 10, 50, 50); // Now assign a new Rectangle to r1. Console.WriteLine(“-> Assigning r2 to r1“); Rectangle r2 = r1; // Change some values of r2. Console.WriteLine(“-> Changing values of r2“); r2.rectInfo.infoString = “This is new info!“; r2.rectBottom = 4444; CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 153 // Print values of both rectangles. r1.Display(); r2.Display(); } The output can be seen in the following: -> Creating r1 -> Assigning r2 to r1 -> Changing values of r2 String = This is new info!, Top = 10, Bottom = 50, Left = 10, Right = 50 String = This is new info!, Top = 10, Bottom = 4444, Left = 10, Right = 50 As you can see, when you change the value of the informational string using the r2 reference, the r1 reference displays the same value. By default, when a value type contains other reference types, assignment results in a copy of the references. In this way, you have two independent structures, each of which contains a reference pointing to the same object in memory (i.e., a shallow copy). When you want to perform a deep copy, where the state of internal references is fully copied into a new object, one approach is to implement the ICloneable interface (as you will do in Chapter 8).  Source Code The ValueAndReferenceTypes project is located under the Chapter 4 subdirectory. Passing Reference Types by Value Reference types or value types can, obviously, be passed as parameters to methods. However, passing a reference type (e.g., a class) by reference is quite different from passing it by value. To understand the distinction, assume you have a simple Person class defined in a new Console Application project named RefTypeValTypeParams, defined as follows: class Person { public string personName; public int personAge; // Constructors. public Person(string name, int age) { personName = name; personAge = age; } public Person(){} public void Display() { Console.WriteLine(“Name: {0}, Age: {1}“, personName, personAge); } CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 154 } Now, what if you create a method that allows the caller to send in the Person object by value (note the lack of parameter modifiers, such as out or ref): static void SendAPersonByValue(Person p) { // Change the age of “p“? p.personAge = 99; // Will the caller see this reassignment? p = new Person(“Nikki“, 99); } Notice how the SendAPersonByValue() method attempts to reassign the incoming Person reference to a new Person object, as well as change some state data. Now let’s test this method using the following Main() method: static void Main(string[] args) { // Passing ref-types by value. Console.WriteLine(“***** Passing Person object by value *****“); Person fred = new Person(“Fred“, 12); Console.WriteLine(“\nBefore by value call, Person is:“); fred.Display(); SendAPersonByValue(fred); Console.WriteLine(“\nAfter by value call, Person is:“); fred.Display(); Console.ReadLine(); } The following is the output of this call: ***** Passing Person object by value ***** Before by value call, Person is: Name: Fred, Age: 12 After by value call, Person is: Name: Fred, Age: 99 As you can see, the value of personAge has been modified. This behavior seems to fly in the face of what it means to pass a parameter “by value.” Given that you were able to change the state of the incoming Person, what was copied? The answer: a copy of the reference to the caller’s object. Therefore, as the SendAPersonByValue() method is pointing to the same object as the caller, it is possible to alter the object’s state data. What is not possible is to reassign what the reference is pointing to. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 155 Passing Reference Types by Reference Now assume you have a SendAPersonByReference() method, which passes a reference type by reference (note the ref parameter modifier): static void SendAPersonByReference(ref Person p) { // Change some data of “p“. p.personAge = 555; // “p“ is now pointing to a new object on the heap! p = new Person(“Nikki“, 999); } As you might expect, this allows complete flexibility of how the callee is able to manipulate the incoming parameter. Not only can the callee change the state of the object, but if it so chooses, it may also reassign the reference to a new Person object. Now ponder the following updated Main() method: static void Main(string[] args) { // Passing ref-types by ref. Console.WriteLine(“***** Passing Person object by reference *****“); ... Person mel = new Person(“Mel“, 23); Console.WriteLine(“Before by ref call, Person is:“); mel.Display(); SendAPersonByReference(ref mel); Console.WriteLine(“After by ref call, Person is:“); mel.Display(); Console.ReadLine(); } Notice the following output: ***** Passing Person object by reference ***** Before by ref call, Person is: Name: Mel, Age: 23 After by ref call, Person is: Name: Nikki, Age: 999 As you can see, an object named Mel returns after the call as an object named Nikki, as the method was able to change what the incoming reference pointed to in memory. The golden rule to keep in mind when passing reference types is the following: • If a reference type is passed by reference, the callee may change the values of the object’s state data, as well as the object it is referencing. • If a reference type is passed by value, the callee may change the values of the object’s state data, but not the object it is referencing. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 156  Source Code The RefTypeValTypeParams project is located under the Chapter 4 subdirectory. Final Details Regarding Value Types and Reference Types To wrap up this topic, consider the information in Table 4-3, which summarizes the core distinctions between value types and reference types. Table 4-3. Value Types and Reference Types Comparison Intriguing Question Value Type Reference Type Where are objects allocated? Allocated on the stack. Allocated on the managed heap. How is a variable represented? Value type variables are local copies. Reference type variables are pointing to the memory occupied by the allocated instance. What is the base type? Implicitly extends System.ValueType. Can derive from any other type (except System. ValueType), as long as that type is not “sealed” (more details on this in Chapter 6). Can this type function as a base to other types? No. Value types are always sealed and cannot be inherited from. Yes. If the type is not sealed, it may function as a base to other types. What is the default parameter passing behavior? Variables are passed by value (i.e., a copy of the variable is passed into the called function). For value types, the object is copied-by-value. For reference types, the reference is copied-by-value. Can this type override System.Object.Finalize()? No. Value types are never placed onto the heap and, therefore, do not need to be finalized. Yes, indirectly (more details on this in Chapter 13). Can I define constructors for this type? Yes, but the default constructor is reserved (i.e., your custom constructors must all have arguments). But, of course! When do variables of this type die? When they fall out of the defining scope. When the object is garbage collected. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 157 Despite their differences, value types and reference types both have the ability to implement interfaces and may support any number of fields, methods, overloaded operators, constants, properties, and events. Understanding C# Nullable Types To wrap up this chapter, let’s examine the role of nullable data type using a final Console Application named NullableTypes. As you know, C# data types have a fixed range and are represented as a type in the System namespace. For example, the System.Boolean data type can be assigned a value from the set {true, false}. Now, recall that all of the numerical data types (as well as the Boolean data type) are value types. Value types can never be assigned the value of null, as that is used to establish an empty object reference: static void Main(string[] args) { // Compiler errors! // Value types cannot be set to null! bool myBool = null; int myInt = null; // OK! Strings are reference types. string myString = null; } C# supports the concept of nullable data types. Simply put, a nullable type can represent all the values of its underlying type, plus the value null. Thus, if we declare a nullable bool, it could be assigned a value from the set {true, false, null}. This can be extremely helpful when working with relational databases, given that it is quite common to encounter undefined columns in database tables. Without the concept of a nullable data type, there is no convenient manner in C# to represent a numerical data point with no value. To define a nullable variable type, the question mark symbol (?) is suffixed to the underlying data type. Do note that this syntax is only legal when applied to value types. If you attempt to create a nullable reference type (including strings), you are issued a compile-time error. Like a nonnullable variable, local nullable variables must be assigned an initial value before you can use them: static void LocalNullableVariables() { // Define some local nullable variables. int? nullableInt = 10; double? nullableDouble = 3.14; bool? nullableBool = null; char? nullableChar = 'a'; int?[] arrayOfNullableInts = new int?[10]; // Error! Strings are reference types! // string? s = “oops“; } In C#, the ? suffix notation is a shorthand for creating an instance of the generic System.Nullable structure type. Although we will not examine generics until Chapter 9, it is important to understand that the System.Nullable type provides a set of members that all nullable types can make use of. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 158 For example, you are able to programmatically discover whether the nullable variable indeed has been assigned a null value using the HasValue property or the != operator. The assigned value of a nullable type may be obtained directly or via the Value property. In fact, given that the ? suffix is just a shorthand for using Nullable, you could implement your LocalNullableVariables() method as follows: static void LocalNullableVariablesUsingNullable() { // Define some local nullable types using Nullable. Nullable nullableInt = 10; Nullable nullableDouble = 3.14; Nullable nullableBool = null; Nullable nullableChar = 'a'; Nullable[] arrayOfNullableInts = new int?[10]; } Working with Nullable Types As stated, nullable data types can be particularly useful when you are interacting with databases, given that columns in a data table may be intentionally empty (e.g., undefined). To illustrate, assume the following class, which simulates the process of accessing a database that has a table containing two columns that may be null. Note that the GetIntFromDatabase() method is not assigning a value to the nullable integer member variable, while GetBoolFromDatabase() is assigning a valid value to the bool? member: class DatabaseReader { // Nullable data field. public int? numericValue = null; public bool? boolValue = true; // Note the nullable return type. public int? GetIntFromDatabase() { return numericValue; } // Note the nullable return type. public bool? GetBoolFromDatabase() { return boolValue; } } Now, assume the following Main() method, which invokes each member of the DatabaseReader class, and discovers the assigned values using the HasValue and Value members, as well as using the C# equality operator (not equal, to be exact): static void Main(string[] args) { Console.WriteLine(“***** Fun with Nullable Data *****\n“); DatabaseReader dr = new DatabaseReader(); // Get int from “database“. int? i = dr.GetIntFromDatabase(); if (i.HasValue) Console.WriteLine(“Value of 'i' is: {0}“, i.Value); CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 159 else Console.WriteLine(“Value of 'i' is undefined.“); // Get bool from “database“. bool? b = dr.GetBoolFromDatabase(); if (b != null) Console.WriteLine(“Value of 'b' is: {0}“, b.Value); else Console.WriteLine(“Value of 'b' is undefined.“); Console.ReadLine(); } The ?? Operator The final aspect to be aware of with nullable types is that they can make use of the C# ?? operator. This operator allows you to assign a value to a nullable type if the retrieved value is in fact null. For this example, assume you want to assign a local nullable integer to 100 if the value returned from GetIntFromDatabase() is null (of course, this method is programmed to always return null, but I am sure you get the general idea): static void Main(string[] args) { Console.WriteLine(“***** Fun with Nullable Data *****\n“); DatabaseReader dr = new DatabaseReader(); ... // If the value from GetIntFromDatabase() is null, // assign local variable to 100. int myData = dr.GetIntFromDatabase() ?? 100; Console.WriteLine(“Value of myData: {0}“, myData); Console.ReadLine(); } The benefit of using the ?? operator is that it provides a more compact version of a traditional if/else condition. However, if you want, you could have authored the following functionally equivalent code to ensure that if a value comes back as null, it will indeed be set to the value 100: // Long-hand notation not using ?? syntax. int? moreData = dr.GetIntFromDatabase(); if (!moreData.HasValue) moreData = 100; Console.WriteLine(“Value of moreData: {0}“, moreData); With this, our initial investigation of the C# programming language is complete! In Chapter 5, you will begin to dig into the details of object-oriented development.  Source Code The NullableTypes application is located under the Chapter 4 subdirectory. CHAPTER 4  CORE C# PROGRAMMING CONSTRUCTS, PART II 160 Summary This chapter began with an examination of several C# keywords that allow you to build custom methods. Recall that by default, parameters are passed by value; however, you may pass a parameter by reference if you mark it with ref or out. You also learned about the role of optional or named parameters and how to define and invoke methods taking parameter arrays. After we investigated the topic of method overloading, the bulk of this chapter examined several details regarding how arrays, enumerations, and structures are defined in C# and represented within the .NET base class libraries. Along the way, you examined several details regarding value types and reference types, including how they respond when passing them as parameters to methods, and how to interact with nullable data types using the ? and ?? operators. P A R T I I I 1 Object-Oriented Programming with C# C H A P T E R 5 163 Understanding Encapsulation In the Chapters 3 and 4, you investigated a number of core syntactical constructs that are commonplace to any .NET application you might be developing. Here, you will begin your examination of the object- oriented capabilities of C#. The first order of business is to examine the process of building well-defined class types that support any number of constructors. After you understand the basics of defining classes and allocating objects, the remainder of this chapter will examine the role of encapsulation. Along the way, you will learn how to define class properties, and come to understand the role of static members, object initialization syntax, read-only fields, constant data, and partial classes. Introducing the C# Class Type As far as the .NET platform is concerned, the most fundamental programming construct is the class type. Formally, a class is a user-defined type that is composed of field data (often called member variables) and members that operate on this data (such as constructors, properties, methods, events, and so forth). Collectively, the set of field data represents the “state” of a class instance (otherwise known as an object). The power of object-oriented languages, such as C#, is that by grouping data and related functionality in a unified class definition, you are able to model your software after entities in the real world. To get the ball rolling, create a new C# Console Application named SimpleClassExample. Next, insert a new class file (named Car.cs) into your project using the Project  Add Class... menu selection. Choose the Class icon from the resulting dialog box, as shown in Figure 5-1, and click the Add button. A class is defined in C# using the class keyword. Here is the simplest possible declaration: class Car { } After you have defined a class type, you will need to consider the set of member variables that will be used to represent its state. For example, you might decide that cars maintain an int data type to represent the current speed and a string data type to represent the car’s friendly pet name. Given these initial design notes, update your Car class as follows: class Car { // The 'state' of the Car. public string petName; public int currSpeed; } CHAPTER 5  UNDERSTANDING ENCAPSULATION 164 Figure 5-1. Inserting a new C# class type Notice that these member variables are declared using the public access modifier. Public members of a class are directly accessible once an object of this type has been created. Recall the term “object” is used to describe an instance of a given class type created using the new keyword.  Note Field data of a class should seldom (if ever) be defined as public. To preserve the integrity of your state data, it is a far better design to define data as private (or possibly protected) and allow controlled access to the data via properties (as shown later in this chapter). However, to keep this first example as simple as possible, public data fits the bill. After you have defined the set of member variables that represent the state of the class, the next design step is to establish the members that model its behavior. For this example, the Car class will define one method named SpeedUp() and another named PrintState(). Update your class as so: class Car { // The 'state' of the Car. public string petName; public int currSpeed; // The functionality of the Car. public void PrintState() { Console.WriteLine(“{0} is going {1} MPH.“, petName, currSpeed); } CHAPTER 5  UNDERSTANDING ENCAPSULATION 165 public void SpeedUp(int delta) { currSpeed += delta; } } PrintState() is more or less a diagnostic function that will simply dump the current state of a given Car object to the command window. SpeedUp() will increase the speed of the Car by the amount specified by the incoming int parameter. Now, update your Main() method in the Program class with the following code: static void Main(string[] args) { Console.WriteLine(“***** Fun with Class Types *****\n“); // Allocate and configure a Car object. Car myCar = new Car(); myCar.petName = “Henry“; myCar.currSpeed = 10; // Speed up the car a few times and print out the // new state. for (int i = 0; i <= 10; i++) { myCar.SpeedUp(5); myCar.PrintState(); } Console.ReadLine(); } After you run your program, you will see that the Car variable (myCar) maintains its current state throughout the life of the application, as shown in the following code: ***** Fun with Class Types ***** Henry is going 15 MPH. Henry is going 20 MPH. Henry is going 25 MPH. Henry is going 30 MPH. Henry is going 35 MPH. Henry is going 40 MPH. Henry is going 45 MPH. Henry is going 50 MPH. Henry is going 55 MPH. Henry is going 60 MPH. Henry is going 65 MPH. CHAPTER 5  UNDERSTANDING ENCAPSULATION 166 Allocating Objects with the new Keyword As shown in the previous code example, objects must be allocated into memory using the new keyword. If you do not make use of the new keyword and attempt to make use of your class variable in a subsequent code statement, you will receive a compiler error. For example, the following Main() method will not compile: static void Main(string[] args) { Console.WriteLine(“***** Fun with Class Types *****\n“); // Error! Forgot to use 'new' to create object! Car myCar; myCar.petName = “Fred“; } To correctly create an object using the new keyword, you may define and allocate a Car object on a single line of code: static void Main(string[] args) { Console.WriteLine(“***** Fun with Class Types *****\n“); Car myCar = new Car(); myCar.petName = “Fred“; } As an alternative, if you wish to define and allocate a class instance on separate lines of code, you may do so as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Class Types *****\n“); Car myCar; myCar = new Car(); myCar.petName = “Fred“; } Here, the first code statement simply declares a reference to a yet-to-be-determined Car object. It is not until you assign a reference to an object that this reference points to a valid object in memory. In any case, at this point we have a trivial class that defines a few points of data and some basic operations. To enhance the functionality of the current Car class, we need to understand the role of constructors. Understanding Constructors Given that objects have state (represented by the values of an object’s member variables), a programmer will typically want to assign relevant values to the object’s field data before use. Currently, the Car class demands that the petName and currSpeed fields be assigned on a field-by-field basis. For the current example, this is not too problematic, given that we have only two public data points. However, it is not uncommon for a class to have dozens of fields to contend with. Clearly, it would be undesirable to author 20 initialization statements to set 20 points of data! Thankfully, C# supports the use of constructors, which allow the state of an object to be established at the time of creation. A constructor is a special method of a class that is called indirectly when creating CHAPTER 5  UNDERSTANDING ENCAPSULATION 167 an object using the new keyword. However, unlike a “normal” method, constructors never have a return value (not even void) and are always named identically to the class they are constructing. The Role of the Default Constructor Every C# class is provided with a freebee default constructor that you may redefine if need be. By definition, a default constructor never takes arguments. After allocating the new object into memory, the default constructor ensures that all field data of the class is set to an appropriate default value (see Chapter 3 for information regarding the default values of C# data types). If you are not satisfied with these default assignments, you may redefine the default constructor to suit your needs. To illustrate, update your C# Car class as follows: class Car { // The 'state' of the Car. public string petName; public int currSpeed; // A custom default constructor. public Car() { petName = “Chuck“; currSpeed = 10; } ... } In this case, we are forcing all Car objects to begin life named Chuck at a rate of 10 mph. With this, you are able to create a Car object set to these default values as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Class Types *****\n“); // Invoking the default constructor. Car chuck = new Car(); // Prints “Chuck is going 10 MPH.“ chuck.PrintState(); ... } Defining Custom Constructors Typically, classes define additional constructors beyond the default. In doing so, you provide the object user with a simple and consistent way to initialize the state of an object directly at the time of creation. Ponder the following update to the Car class, which now supports a total of three constructors: class Car { // The 'state' of the Car. public string petName; CHAPTER 5  UNDERSTANDING ENCAPSULATION 168 public int currSpeed; // A custom default constructor. public Car() { petName = “Chuck“; currSpeed = 10; } // Here, currSpeed will receive the // default value of an int (zero). public Car(string pn) { petName = pn; } // Let caller set the full state of the Car. public Car(string pn, int cs) { petName = pn; currSpeed = cs; } ... } Keep in mind that what makes one constructor different from another (in the eyes of the C# compiler) is the number of and type of constructor arguments. Recall from Chapter 4, when you define a method of the same name that differs by the number or type of arguments, you have overloaded the method. Thus, the Car class has overloaded the constructor to provide a number of ways to create an object at the time of declaration. In any case, you are now able to create Car objects using any of the public constructors. For example: static void Main(string[] args) { Console.WriteLine(“***** Fun with Class Types *****\n“); // Make a Car called Chuck going 10 MPH. Car chuck = new Car(); chuck.PrintState(); // Make a Car called Mary going 0 MPH. Car mary = new Car(“Mary“); mary.PrintState(); // Make a Car called Daisy going 75 MPH. Car daisy = new Car(“Daisy“, 75); daisy.PrintState(); ... } CHAPTER 5  UNDERSTANDING ENCAPSULATION 169 The Default Constructor Revisited As you have just learned, all classes are provided with a free default constructor. Thus, if you insert a new class into your current project named Motorcycle, defined like so: class Motorcycle { public void PopAWheely() { Console.WriteLine(“Yeeeeeee Haaaaaeewww!“); } } you are able to create an instance of the Motorcycle type via the default constructor out of the box: static void Main(string[] args) { Console.WriteLine(“***** Fun with Class Types *****\n“); Motorcycle mc = new Motorcycle(); mc.PopAWheely(); ... } However, as soon as you define a custom constructor with any number of parameters, the default constructor is silently removed from the class and is no longer available! Think of it this way: if you do not define a custom constructor, the C# compiler grants you a default in order to allow the object user to allocate an instance of your type with field data set to the correct default values. However, when you define a unique constructor, the compiler assumes you have taken matters into your own hands. Therefore, if you wish to allow the object user to create an instance of your type with the default constructor, as well as your custom constructor, you must explicitly redefine the default. To this end, understand that in a vast majority of cases, the implementation of the default constructor of a class is intentionally empty, as all you require is the ability to create an object with default values. Consider the following update to the Motorcycle class: class Motorcycle { public int driverIntensity; public void PopAWheely() { for (int i = 0; i <= driverIntensity; i++) { Console.WriteLine(“Yeeeeeee Haaaaaeewww!“); } } // Put back the default constructor, which will // set all data members to default vaules. public Motorcycle() {} // Our custom constructor. public Motorcycle(int intensity) { CHAPTER 5  UNDERSTANDING ENCAPSULATION 170 driverIntensity = intensity; } }  Note Now that you better understand the role of class constructors, here is a nice short cut. The Visual Studio IDE provides the ctor code snippet. When you type “ctor” and press the Tab key twice, the IDE will automatically define a custom default constructor! You can then add custom parameters and implementation logic. Give it a try. The Role of the this Keyword C# supplies a this keyword that provides access to the current class instance. One possible use of the this keyword is to resolve scope ambiguity, which can arise when an incoming parameter is named identically to a data field of the class. Of course, ideally you would simply adopt a naming convention that does not result in such ambiguity; however, to illustrate this use of the this keyword, update your Motorcycle class with a new string field (named name) to represent the driver’s name. Next, add a method named SetDriverName() implemented as follows: class Motorcycle { public int driverIntensity; // New members to represent the name of the driver. public string name; public void SetDriverName(string name) { name = name; } ... } Although this code will compile just fine, Visual Studio will display a warning message informing you that you have assigned a variable back to itself! To illustrate, update Main() to call SetDriverName() and then print out the value of the name field. You might be surprised to find that the value of the name field is an empty string! // Make a Motorcycle with a rider named Tiny? Motorcycle c = new Motorcycle(5); c.SetDriverName(“Tiny“); c.PopAWheely(); Console.WriteLine(“Rider name is {0}“, c.name); // Prints an empty name value! The problem is that the implementation of SetDriverName() is assigning the incoming parameter back to itself given that the compiler assumes name is referring to the variable currently in the method scope rather than the name field at the class scope. To inform the compiler that you wish to set the current object’s name data field to the incoming name parameter, simply use this to resolve the ambiguity: CHAPTER 5  UNDERSTANDING ENCAPSULATION 171 public void SetDriverName(string name) { this.name = name; } Do understand that if there is no ambiguity, you are not required to make use of the this keyword when a class wishes to access its own data fields or members, as this is implied. For example, if we rename the string data member from name to driverName (which will also require you to update your Main() method) the use of this is optional as there is no longer a scope ambiguity: class Motorcycle { public int driverIntensity; public string driverName; public void SetDriverName(string name) { // These two statements are functionally the same. driverName = name; this.driverName = name; } ... } Even though there is little to be gained when using this in unambiguous situations, you might still find this keyword useful when implementing class members, as IDEs such as SharpDevelop and Visual Studio will enable IntelliSense when this is specified. This can be very helpful when you have forgotten the name of a class member and want to quickly recall the definition. Consider Figure 5-2. Figure 5-2. The IntelliSense of this CHAPTER 5  UNDERSTANDING ENCAPSULATION 172 Chaining Constructor Calls Using this Another use of the this keyword is to design a class using a technique termed constructor chaining. This design pattern is helpful when you have a class that defines multiple constructors. Given the fact that constructors often validate the incoming arguments to enforce various business rules, it can be quite common to find redundant validation logic within a class’s constructor set. Consider the following updated Motorcycle: class Motorcycle { public int driverIntensity; public string driverName; public Motorcycle() { } // Redundent constructor logic! public Motorcycle(int intensity) { if (intensity > 10) { intensity = 10; } driverIntensity = intensity; } public Motorcycle(int intensity, string name) { if (intensity > 10) { intensity = 10; } driverIntensity = intensity; driverName = name; } ... } Here (perhaps in an attempt to ensure the safety of the rider) each constructor is ensuring that the intensity level is never greater than 10. While this is all well and good, you do have redundant code statements in two constructors. This is less than ideal, as you are now required to update code in multiple locations if your rules change (for example, if the intensity should not be greater than 5). One way to improve the current situation is to define a method in the Motorcycle class that will validate the incoming argument(s). If you were to do so, each constructor could make a call to this method before making the field assignment(s). While this approach does allow you to isolate the code you need to update when the business rules change, you are now dealing with the following redundancy: class Motorcycle { public int driverIntensity; public string driverName; CHAPTER 5  UNDERSTANDING ENCAPSULATION 173 // Constructors. public Motorcycle() { } public Motorcycle(int intensity) { SetIntensity(intensity); } public Motorcycle(int intensity, string name) { SetIntensity(intensity); driverName = name; } public void SetIntensity(int intensity) { if (intensity > 10) { intensity = 10; } driverIntensity = intensity; } ... } A cleaner approach is to designate the constructor that takes the greatest number of arguments as the “master constructor” and have its implementation perform the required validation logic. The remaining constructors can make use of the this keyword to forward the incoming arguments to the master constructor and provide any additional parameters as necessary. In this way, you only need to worry about maintaining a single constructor for the entire class, while the remaining constructors are basically empty. Here is the final iteration of the Motorcycle class (with one additional constructor for the sake of illustration). When chaining constructors, note how the this keyword is “dangling” off the constructor’s declaration (via a colon operator) outside the scope of the constructor itself: class Motorcycle { public int driverIntensity; public string driverName; // Constructor chaining. public Motorcycle() {} public Motorcycle(int intensity) : this(intensity, ““) {} public Motorcycle(string name) : this(0, name) {} // This is the 'master' constructor that does all the real work. public Motorcycle(int intensity, string name) { if (intensity > 10) { CHAPTER 5  UNDERSTANDING ENCAPSULATION 174 intensity = 10; } driverIntensity = intensity; driverName = name; } ... } Understand that using the this keyword to chain constructor calls is never mandatory. However, when you make use of this technique, you do tend to end up with a more maintainable and concise class definition. Again, using this technique you can simplify your programming tasks, as the real work is delegated to a single constructor (typically the constructor that has the most parameters), while the other constructors simply “pass the buck.”  Note Recall from Chapter 4 that C# supports optional parameters. If you make use of optional parameters in your class constructors, you can achieve the same benefits as constructor chaining, with considerably less code. You will see how to do so in just a moment. Observing Constructor Flow On a final note, do know that once a constructor passes arguments to the designated master constructor (and that constructor has processed the data), the constructor invoked originally by the caller will finish executing any remaining code statements. To clarify, update each of the constructors of the Motorcycle class with a fitting call to Console.WriteLine(): class Motorcycle { public int driverIntensity; public string driverName; // Constructor chaining. public Motorcycle() { Console.WriteLine(“In default ctor“); } public Motorcycle(int intensity) : this(intensity, ““) { Console.WriteLine(“In ctor taking an int“); } public Motorcycle(string name) : this(0, name) { Console.WriteLine(“In ctor taking a string“); } CHAPTER 5  UNDERSTANDING ENCAPSULATION 175 // This is the 'master' constructor that does all the real work. public Motorcycle(int intensity, string name) { Console.WriteLine(“In master ctor “); if (intensity > 10) { intensity = 10; } driverIntensity = intensity; driverName = name; } ... } Now, ensure your Main() method exercises a Motorcycle object as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with class Types *****\n“); // Make a Motorcycle. Motorcycle c = new Motorcycle(5); c.SetDriverName(“Tiny“); c.PopAWheely(); Console.WriteLine(“Rider name is {0}“, c.driverName); Console.ReadLine(); } With this, ponder the output from the previous Main() method: ***** Fun with class Types ***** In master ctor In ctor taking an int Yeeeeeee Haaaaaeewww! Yeeeeeee Haaaaaeewww! Yeeeeeee Haaaaaeewww! Yeeeeeee Haaaaaeewww! Yeeeeeee Haaaaaeewww! Yeeeeeee Haaaaaeewww! Rider name is Tiny As you can see, the flow of constructor logic is as follows: • You create your object by invoking the constructor requiring a single int. • This constructor forwards the supplied data to the master constructor and provides any additional start-up arguments not specified by the caller. • The master constructor assigns the incoming data to the object’s field data. CHAPTER 5  UNDERSTANDING ENCAPSULATION 176 • Control is returned to the constructor originally called, and executes any remaining code statements. The nice thing about using constructor chaining, is that this programming pattern will work with any version of the C# language and .NET platform. However, if you are targeting .NET 4.0 and higher, you can further simplify your programming tasks by making use of optional arguments as an alternative to traditional constructor chaining. Revisiting Optional Arguments In Chapter 4, you learned about optional and named arguments. Recall that optional arguments allow you to define supplied default values to incoming arguments. If the caller is happy with these defaults, they are not required to specify a unique value, however they may do so to provide the object with custom data. Consider the following version of Motorcycle, which now provides a number of ways to construct objects using a single constructor definition: class Motorcycle { // Single constructor using optional args. public Motorcycle(int intensity = 0, string name = ““) { if (intensity > 10) { intensity = 10; } driverIntensity = intensity; driverName = name; } ... } With this one constructor, you are now able to create a new Motorcycle object using zero, one, or two arguments. Recall that named argument syntax allows you to essentially skip over acceptable default settings (see Chapter 3). static void MakeSomeBikes() { // driverName = ““, driverIntensity = 0 Motorcycle m1 = new Motorcycle(); Console.WriteLine(“Name= {0}, Intensity= {1}“, m1.driverName, m1.driverIntensity); // driverName = “Tiny“, driverIntensity = 0 Motorcycle m2 = new Motorcycle(name:“Tiny“); Console.WriteLine(“Name= {0}, Intensity= {1}“, m2.driverName, m2.driverIntensity); // driverName = ““, driverIntensity = 7 Motorcycle m3 = new Motorcycle(7); Console.WriteLine(“Name= {0}, Intensity= {1}“, m3.driverName, m3.driverIntensity); } hCHAPTER 5  UNDERSTANDING ENCAPSULATION 177 While the use of optional/named arguments is a very slick way to streamline how you define the set of constructors used by a given class, do always remember that this syntax is only valid under .NET 4.0 or higher. If you need to build classes that can run under any version of the .NET platform, it is best to stick to classical constructor chaining techniques. In any case, at this point you are able to define a class with field data (a.k.a., member variables) and various operations such as methods and constructors. Next up, let’s formalize the role of the static keyword.  Source Code The SimpleClassExample project is included under the Chapter 5 subdirectory. Understanding the static Keyword A C# class may define any number of static members, which are declared using the static keyword. When you do so, the member in question must be invoked directly from the class level, rather than from an object reference variable. To illustrate the distinction, consider your good friend System.Console. As you have seen, you do not invoke the WriteLine() method from the object level: // Error! WriteLine() is not an object level method! Console c = new Console(); c.WriteLine(“I can't be printed...“); but instead simply prefix the class name to the static WriteLine() member: // Correct! WriteLine() is a static method. Console.WriteLine(“Much better! Thanks...“); Simply put, static members are items that are deemed (by the class designer) to be so commonplace that there is no need to create an instance of the class before invoking the member. While any class can define static members, they are quite commonly found within “utility classes.” By definition, a utility class is a class that does not maintain any object-level state and is not created with the new keyword. Rather, a utility class exposes all functionality as class-level (a.k.a., static) members. For example, if you were to use the Visual Studio object browser (via the View  Object Browser menu item) to view the System namespace of mscorlib.dll, you would see that all of the members of the Console, Math, Environment, and GC classes (among others) expose all of their functionality via static members. These are but a few utility classes found within the .NET base class libraries. Again, be aware that static members are not only found in utility classes; they can be part of any class definition at all. Just remember that static members promote a given item to the class level rather than the object level. As you will see over the next few sections, the static keyword can be applied to the following: • Data of a class • Methods of a class • Properties of a class • A constructor • The entire class definition CHAPTER 5  UNDERSTANDING ENCAPSULATION 178 Let’s see each of our options, beginning with the concept of static data.  Note You will examine the role of static properties later in this chapter, during our examination of properties themselves. Defining Static Field Data Most of the time when designing a class, you define data as instance-level data; said another way, as nonstatic data. When you define instance-level data, you know that every time you create a new object, the object maintains its own independent copy of the data. In contrast, when you define static data of a class, the memory is shared by all objects of that category. To see the distinction, create a new Console Application project named StaticDataAndMembers. Now, insert a new class into your project named SavingsAccount. Begin by defining a point of instance- level data (to model the current balance) and a custom constructor to set the initial balance: // A simple savings account class. class SavingsAccount { // Instance-level data. public double currBalance; public SavingsAccount(double balance) { currBalance = balance; } } When you create SavingsAccount objects, memory for the currBalance field is allocated for each object. Thus, you could create five different SavingsAccount objects, each with their own unique balance. Furthermore, if you change the balance on one account, the other objects are not affected. Static data, on the other hand, is allocated once and shared among all objects of the same class category. Add a static point of data named currInterestRate to the SavingsAccount class, which is set to a default value of 0.04: // A simple savings account class. class SavingsAccount { // Instance-level data. public double currBalance; // A static point of data. public static double currInterestRate = 0.04; public SavingsAccount(double balance) { currBalance = balance; } } CHAPTER 5  UNDERSTANDING ENCAPSULATION 179 If you were to create three instances of SavingsAccount in Main() as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Static Data *****\n“); SavingsAccount s1 = new SavingsAccount(50); SavingsAccount s2 = new SavingsAccount(100); SavingsAccount s3 = new SavingsAccount(10000.75); Console.ReadLine(); } the in-memory data allocation would look something like Figure 5-3. Figure 5-3. Static data is allocated once and shared among all instances of the class Here, our assumption is that all saving accounts should have the same interest rate. Because static data is shared by all objects of the same category, if you were to change it in any way, all objects will “see” the new value the next time they access the static data, as they are all essentially looking at the same memory location. To understand how to change (or obtain) static data, we need to consider the role of static methods. Defining Static Methods Let’s update the SavingsAccount class to define two static methods. The first static method (GetInterestRate()) will return the current interest rate, while the second static method (SetInterestRate()) will allow you to change the interest rate: // A simple savings account class. class SavingsAccount { // Instance-level data. public double currBalance; // A static point of data. public static double currInterestRate = 0.04; public SavingsAccount(double balance) { currBalance = balance; } CHAPTER 5  UNDERSTANDING ENCAPSULATION 180 // Static members to get/set interest rate. public static void SetInterestRate(double newRate) { currInterestRate = newRate; } public static double GetInterestRate() { return currInterestRate; } } Now, observe the following usage: static void Main(string[] args) { Console.WriteLine(“***** Fun with Static Data *****\n“); SavingsAccount s1 = new SavingsAccount(50); SavingsAccount s2 = new SavingsAccount(100); // Print the current interest rate. Console.WriteLine(“Interest Rate is: {0}“, SavingsAccount.GetInterestRate()); // Make new object, this does NOT 'reset' the interest rate. SavingsAccount s3 = new SavingsAccount(10000.75); Console.WriteLine(“Interest Rate is: {0}“, SavingsAccount.GetInterestRate()); Console.ReadLine(); } The output of the previous Main() is seen here: ***** Fun with Static Data ***** Interest Rate is: 0.04 Interest Rate is: 0.04 As you can see, when you create new instances of the SavingsAccount class, the value of the static data is not reset, as the CLR will allocate the static data into memory exactly one time. After that point, all objects of type SavingsAccount operate on the same value for the static currInterestRate field. When designing any C# class, one of your design challenges is to determine which pieces of data should be defined as static members, and which should not. While there are no hard and fast rules, remember that a static data field is shared by all objects of that type. Therefore, if you are defining a point of data that all objects should share between them, static is the way to go. Consider what would happen if the interest rate variable were not defined using the static keyword. This would mean every SavingsAccount object would have its own copy of the currInterestRate field. Now, assume you created 100 SavingsAccount objects, and need to change the interest rate. That would require you to call the SetInterestRate() method 100 times! Clearly, this would not be a very useful way to model “shared data.” Again, static data is perfect when you have a value that should be common to all objects of that category. CHAPTER 5  UNDERSTANDING ENCAPSULATION 181  Note It is a compiler error for a static member to reference nonstatic members in its implementation. On a related note, it is an error to use the this operator in a static member as “this” implies an object! Defining Static Constructors A typical constructor is used to set the value of an object’s instance-level data at the time of creation. However, what would happen if you attempted to assign the value of a static point of data in a typical constructor? You might be surprised to find that the value is reset each time you create a new object! To illustrate, assume you have updated the SavingsAccount class constructor as follows (also note we are no longer assigning the currInterestRate field inline): class SavingsAccount { public double currBalance; public static double currInterestRate; // Notice that our constructor is setting // the static currInterestRate value. public SavingsAccount(double balance) { currInterestRate = 0.04; // This is static data! currBalance = balance; } ... } Now, assume you have authored the following code in Main(): static void Main( string[] args ) { Console.WriteLine(“***** Fun with Static Data *****\n“); // Make an account. SavingsAccount s1 = new SavingsAccount(50); // Print the current interest rate. Console.WriteLine(“Interest Rate is: {0}“, SavingsAccount.GetInterestRate()); // Try to change the interest rate via property. SavingsAccount.SetInterestRate(0.08); // Make a second account. SavingsAccount s2 = new SavingsAccount(100); // Should print 0.08...right?? Console.WriteLine(“Interest Rate is: {0}“, SavingsAccount.GetInterestRate()); Console.ReadLine(); } CHAPTER 5  UNDERSTANDING ENCAPSULATION 182 If you execute the previous Main() method, you would see that that the currInterestRate variable is reset each time you create a new SavingsAccount object, and it is always set to 0.04. Clearly, setting the value of static data in a normal instance-level constructor sort of defeats the whole purpose. Every time you make a new object, the class-level data is reset! One approach to setting a static field is to use member initialization syntax, as you did originally: class SavingsAccount { public double currBalance; // A static point of data. public static double currInterestRate = 0.04; ... } This approach will ensure the static field is assigned only once, regardless of how many objects you create. However, what if the value for your static data needed to be obtained at runtime? For example, in a typical banking application, the value of an interest rate variable would be read from a database or external file. To perform such tasks requires a method scope such as a constructor to execute the code statements. For this very reason, C# allows you to define a static constructor, which allows you to safely set the values of your static data. Consider the following update to our class: class SavingsAccount { public double currBalance; public static double currInterestRate; public SavingsAccount(double balance) { currBalance = balance; } // A static constructor! static SavingsAccount() { Console.WriteLine(“In static ctor!“); currInterestRate = 0.04; } ... } Simply put, a static constructor is a special constructor that is an ideal place to initialize the values of static data when the value is not known at compile time (e.g., you need to read in the value from an external file, a database, generate a random number, or whatnot). If you were to rerun the previous Main() method, you would find the output you expect. Note that the message “In static ctor!” only prints out one time, as the CLR calls all static constructors before first use (and never calls them again for that instance of the application): CHAPTER 5  UNDERSTANDING ENCAPSULATION 183 ***** Fun with Static Data ***** In static ctor! Interest Rate is: 0.04 Interest Rate is: 0.08 Here are a few points of interest regarding static constructors: • A given class may define only a single static constructor. In other words, the static constructor cannot be overloaded. • A static constructor does not take an access modifier and cannot take any parameters. • A static constructor executes exactly one time, regardless of how many objects of the type are created. • The runtime invokes the static constructor when it creates an instance of the class or before accessing the first static member invoked by the caller. • The static constructor executes before any instance-level constructors. Given this modification, when you create new SavingsAccount objects, the value of the static data is preserved, as the static member is set only one time within the static constructor, regardless of the number of objects created.  Source Code The StaticDataAndMembers project is included under the Chapter 5 subdirectory. Defining Static Classes It is also possible to apply the static keyword directly on the class level. When a class has been defined as static, it is not creatable using the new keyword, and it can contain only members or data fields marked with the static keyword. If this is not the case, you receive compiler errors.  Note Recall that a class (or structure) that only exposes static functionality is often termed a utility class. When designing a utility class, it is good practice to apply the static keyword to the class definition. At first glance, this might seem like a fairly odd feature, given that a class that cannot be created does not appear all that helpful. However, if you create a class that contains nothing but static members and/or constant data, the class has no need to be allocated in the first place! To illustrate, create a new Console Application named SimpleUtilityClass. Next, define the following class: CHAPTER 5  UNDERSTANDING ENCAPSULATION 184 // Static classes can only // contain static members! static class TimeUtilClass { public static void PrintTime() { Console.WriteLine(DateTime.Now.ToShortTimeString()); } public static void PrintDate() { Console.WriteLine(DateTime.Today.ToShortDateString()); } } Given that this class has been defined with the static keyword, you cannot create an instance of TimeUtilClass using the new keyword. Rather, all functionality is exposed from the class level: static void Main(string[] args) { Console.WriteLine(“***** Fun with Static Classes *****\n“); // This is just fine. TimeUtilClass.PrintDate(); TimeUtilClass.PrintTime(); // Compiler error! Can't create static classes! TimeUtilClass u = new TimeUtilClass (); Console.ReadLine(); } At this point in the chapter, you hopefully feel comfortable defining simple class types containing constructors, fields, and various static (and nonstatic) members. Now that you have the basics of class construction under your belt, you can formally investigate the three pillars of object-oriented programming.  Source Code The SimpleUtilityClass project is located under the Chapter 5 subdirectory. Defining the Pillars of OOP All object-oriented languages (C#, Java, C++, Visual Basic, etc.) must contend with three core principals, often called the pillars of object-oriented programming (OOP): • Encapsulation: How does this language hide an object’s internal implementation details and preserve data integrity? • Inheritance: How does this language promote code reuse? • Polymorphism: How does this language let you treat related objects in a similar way? CHAPTER 5  UNDERSTANDING ENCAPSULATION 185 Before digging into the syntactic details of each pillar, it is important that you understand the basic role of each. Here is an overview of each pillar, which will be examined in full detail over the remainder of this chapter and the next. The Role of Encapsulation The first pillar of OOP is called encapsulation. This trait boils down to the language’s ability to hide unnecessary implementation details from the object user. For example, assume you are using a class named DatabaseReader, which has two primary methods named Open() and Close(): // Assume this class encapsulates the details of opening and closing a database. DatabaseReader dbReader = new DatabaseReader(); dbReader.Open(@“C:\AutoLot.mdf“); // Do something with data file and close the file. dbReader.Close(); The fictitious DatabaseReader class encapsulates the inner details of locating, loading, manipulating, and closing a data file. Programmers love encapsulation, as this pillar of OOP keeps coding tasks simpler. There is no need to worry about the numerous lines of code that are working behind the scenes to carry out the work of the DatabaseReader class. All you do is create an instance and send the appropriate messages (e.g., “Open the file named AutoLot.mdf located on my C drive”). Closely related to the notion of encapsulating programming logic is the idea of data protection. Ideally, an object’s state data should be specified using the private (or possibly protected) keyword. In this way, the outside world must ask politely in order to change or obtain the underlying value. This is a good thing, as publicly declared data points can easily become corrupted (hopefully by accident rather than intent!). You will formally examine this aspect of encapsulation in just a bit. The Role of Inheritance The next pillar of OOP, inheritance, boils down to the language’s ability to allow you to build new class definitions based on existing class definitions. In essence, inheritance allows you to extend the behavior of a base (or parent) class by inheriting core functionality into the derived subclass (also called a child class). Figure 5-4 shows a simple example. You can read the diagram in Figure 5-4 as “A Hexagon is-a Shape that is-an Object.” When you have classes related by this form of inheritance, you establish “is-a” relationships between types. The “is-a” relationship is termed inheritance. Here, you can assume that Shape defines some number of members that are common to all descendents (maybe a value to represent the color to draw the shape, and other values to represent the height and width). Given that the Hexagon class extends Shape, it inherits the core functionality defined by Shape and Object, as well as defines additional hexagon-related details of its own (whatever those may be).  Note Under the .NET platform, System.Object is always the topmost parent in any class hierarchy, which defines some general functionality for all types (fully described in Chapter 6). CHAPTER 5  UNDERSTANDING ENCAPSULATION 186 Figure 5-4. The “is-a” relationship There is another form of code reuse in the world of OOP: the containment/delegation model also known as the “has-a” relationship or aggregation. This form of reuse is not used to establish parent/child relationships. Rather, the “has-a” relationship allows one class to define a member variable of another class and expose its functionality (if required) to the object user indirectly. For example, assume you are again modeling an automobile. You might want to express the idea that a car “has-a” radio. It would be illogical to attempt to derive the Car class from a Radio, or vice versa (a Car “is-a” Radio? I think not!). Rather, you have two independent classes working together, where the Car class creates and exposes the Radio’s functionality: class Radio { public void Power(bool turnOn) { Console.WriteLine(“Radio on: {0}“, turnOn); } } class Car { // Car 'has-a' Radio. private Radio myRadio = new Radio(); public void TurnOnRadio(bool onOff) { // Delegate call to inner object. myRadio.Power(onOff); } } Notice that the object user has no clue that the Car class is making use of an inner Radio object. static void Main(string[] args) { // Call is forwarded to Radio internally. CHAPTER 5  UNDERSTANDING ENCAPSULATION 187 Car viper = new Car(); viper.TurnOnRadio(false); } The Role of Polymorphism The final pillar of OOP is polymorphism. This trait captures a language’s ability to treat related objects in a similar manner. Specifically, this tenant of an object-oriented language allows a base class to define a set of members (formally termed the polymorphic interface) that are available to all descendents. A class’s polymorphic interface is constructed using any number of virtual or abstract members (see Chapter 6 for full details). In a nutshell, a virtual member is a member in a base class that defines a default implementation that may be changed (or more formally speaking, overridden) by a derived class. In contrast, an abstract method is a member in a base class that does not provide a default implementation, but does provide a signature. When a class derives from a base class defining an abstract method, it must be overridden by a derived type. In either case, when derived types override the members defined by a base class, they are essentially redefining how they respond to the same request. To preview polymorphism, let’s provide some details behind the shapes hierarchy shown in Figure 5-5. Assume that the Shape class has defined a virtual method named Draw() that takes no parameters. Given the fact that every shape needs to render itself in a unique manner, subclasses such as Hexagon and Circle are free to override this method to their own liking (see Figure 5-5). Figure 5-5. Classical polymorphism After a polymorphic interface has been designed, you can begin to make various assumptions in your code. For example, given that Hexagon and Circle derive from a common parent (Shape), an array of Shape types could contain anything deriving from this base class. Furthermore, given that Shape defines a CHAPTER 5  UNDERSTANDING ENCAPSULATION 188 polymorphic interface to all derived types (the Draw() method in this example), you can assume each member in the array has this functionality. Consider the following Main() method, which instructs an array of Shape-derived types to render themselves using the Draw() method: class Program { static void Main(string[] args) { Shape[] myShapes = new Shape[3]; myShapes[0] = new Hexagon(); myShapes[1] = new Circle(); myShapes[2] = new Hexagon(); foreach (Shape s in myShapes) { // Use the polymorphic interface! s.Draw(); } Console.ReadLine(); } } This wraps up our brisk overview of the pillars of OOP. Now that you have the theory in your mind, the remainder of this chapter explores further details of how encapsulation is handled under C#. Chapter 6 will tackle the details of inheritance and polymorphism. C# Access Modifiers When working with encapsulation, you must always take into account which aspects of a type are visible to various parts of your application. Specifically, types (classes, interfaces, structures, enumerations, and delegates) as well as their members (properties, methods, constructors, and fields) are defined using a specific keyword to control how “visible” the item is to other parts of your application. Although C# defines numerous keywords to control access, they differ on where they can be successfully applied (type or member). Table 5-1 documents the role of each access modifier and where it may be applied. Table 5-1. C# Access Modifiers C# Access Modifier May Be Applied To Meaning in Life public Types or type members Public items have no access restrictions. A public member can be accessed from an object, as well as any derived class. A public type can be accessed from other external assemblies. private Type members or nested types Private items can only be accessed by the class (or structure) that defines the item. CHAPTER 5  UNDERSTANDING ENCAPSULATION 189 protected Type members or nested types Protected items can be used by the class which defines it, and any child class. However, protected items cannot be accessed from the outside world using the C# dot operator. internal Types or type members Internal items are accessible only within the current assembly. Therefore, if you define a set of internal types within a .NET class library, other assemblies are not able to make use of them. protected internal Type members or nested types When the protected and internal keywords are combined on an item, the item is accessible within the defining assembly, the defining class, and by derived classes. In this chapter, you are only concerned with the public and private keywords. Later chapters will examine the role of the internal and protected internal modifiers (useful when you build .NET code libraries) and the protected modifier (useful when you are creating class hierarchies). The Default Access Modifiers By default, type members are implicitly private while types are implicitly internal. Thus, the following class definition is automatically set to internal, while the type’s default constructor is automatically set to private: // An internal class with a private default constructor. class Radio { Radio(){} } To allow other parts of a program to invoke members of an object, you must mark them as publicly accessible. As well, if you wish to expose the Radio to external assemblies (again, useful when building .NET code libraries; see Chapter 14), you will need to add the public modifier. // A public class with a public default constructor. public class Radio { public Radio(){} } Access Modifiers and Nested Types As mentioned in Table 5-1, the private, protected, and protected internal access modifiers can be applied to a nested type. Chapter 6 will examine nesting in detail. What you need to know at this point, however, is that a nested type is a type declared directly within the scope of class or structure. By way of CHAPTER 5  UNDERSTANDING ENCAPSULATION 190 example, here is a private enumeration (named CarColor) nested within a public class (named SportsCar): public class SportsCar { // OK! Nested types can be marked private. private enum CarColor { Red, Green, Blue } } Here, it is permissible to apply the private access modifier on the nested type. However, nonnested types (such as the SportsCar) can only be defined with the public or internal modifiers. Therefore, the following class definition is illegal: // Error! Nonnested types cannot be marked private! private class SportsCar {} The First Pillar: C#’s Encapsulation Services The concept of encapsulation revolves around the notion that an object’s internal data should not be directly accessible from an object instance. Rather, class data is defined as private. If the caller wants to alter the state of an object, the user does so indirectly using public members. To illustrate the need for encapsulation services, assume you have created the following class definition: // A class with a single public field. class Book { public int numberOfPages; } The problem with public data is that the data itself has no ability to “understand” whether the current value to which they are assigned is valid with regard to the current business rules of the system. As you know, the upper range of a C# int is quite large (2,147,483,647). Therefore, the compiler allows the following assignment: // Humm. That is one heck of a mini-novel! static void Main(string[] args) { Book miniNovel = new Book(); miniNovel.numberOfPages = 30000000; } Although you have not overflowed the boundaries of an int data type, it should be clear that a mini- novel with a page count of 30,000,000 pages is a bit unreasonable. As you can see, public fields do not provide a way to trap logical upper (or lower) limits. If your current system has a business rule that states a book must be between 1 and 1,000 pages, you are at a loss to enforce this programmatically. Because of this, public fields typically have no place in a production-level class definition. CHAPTER 5  UNDERSTANDING ENCAPSULATION 191  Note To be more specific, members of a class that represent an object’s state should not be marked as public. As you will see later in this chapter, public constants and public read-only fields are a-okay. Encapsulation provides a way to preserve the integrity of an object’s state data. Rather than defining public fields (which can easily foster data corruption), you should get in the habit of defining private data, which is indirectly manipulated using one of two main techniques: • Define a pair of public accessor (get) and mutator (set) methods. • Define a public .NET property. Whichever technique you choose, the point is that a well-encapsulated class should protect its data and hide the details of how it operates from the prying eyes of the outside world. This is often termed black box programming. The beauty of this approach is that an object is free to change how a given method is implemented under the hood. It does this without breaking any existing code making use of it, provided that the parameters and return values of the method remain constant. Encapsulation Using Traditional Accessors and Mutators Over the remaining pages in this chapter, you will be building a fairly complete class that models a general employee. To get the ball rolling, create a new Console Application named EmployeeApp and insert a new class file (named Employee.cs) using the Project Add class menu item. Update the Employee class with the following fields, methods, and constructors: class Employee { // Field data. private string empName; private int empID; private float currPay; // Constructors. public Employee() {} public Employee(string name, int id, float pay) { empName = name; empID = id; currPay = pay; } // Methods. public void GiveBonus(float amount) { currPay += amount; } public void DisplayStats() { Console.WriteLine(“Name: {0}“, empName); CHAPTER 5  UNDERSTANDING ENCAPSULATION 192 Console.WriteLine(“ID: {0}“, empID); Console.WriteLine(“Pay: {0}“, currPay); } } Notice that the fields of the Employee class are currently defined using the private keyword. Given this, the empName, empID, and currPay fields are not directly accessible from an object variable. Therefore, the following logic in Main() would result in compiler errors: static void Main(string[] args) { // Error! Cannot directly access private members // from an object! Employee emp = new Employee(); emp.empName = “Marv“; } If you want the outside world to interact with a worker’s full name, a traditional approach (which is very common in Java) is to define an accessor (get method) and a mutator (set method). The role of a “get” method is to return to the caller the current value of the underlying state data. A “set” method allows the caller to change the current value of the underlying state data, as long as the defined business rules are met. To illustrate, let’s encapsulate the empName field. To do so, add the following public methods to the Employee class. Notice that the SetName() method performs a test on the incoming data, to ensure the string is 15 characters or less. If it is not, an error prints to the console and returns without making a change to the empName field:  Note If this were a production level class, you would also make to check the character length for an employee’s name within your constructor logic. Ignore this detail for the time being, as you will clean up this code in just a bit when you examine .NET property syntax. class Employee { // Field data. private string empName; ... // Accessor (get method). public string GetName() { return empName; } // Mutator (set method). public void SetName(string name) { // Do a check on incoming value // before making assignment. CHAPTER 5  UNDERSTANDING ENCAPSULATION 193 if (name.Length > 15) Console.WriteLine(“Error! Name must be less than 16 characters!“); else empName = name; } } This technique requires two uniquely named methods to operate on a single data point. To test your new methods, update your Main() method as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Encapsulation *****\n“); Employee emp = new Employee(“Marvin“, 456, 30000); emp.GiveBonus(1000); emp.DisplayStats(); // Use the get/set methods to interact with the object's name. emp.SetName(“Marv“); Console.WriteLine(“Employee is named: {0}“, emp.GetName()); Console.ReadLine(); } Because of the code in your SetName() method, if you attempted to specify more than 16 characters (see the following), you would find the hard-coded error message printed to the console: static void Main(string[] args) { Console.WriteLine(“***** Fun with Encapsulation *****\n“); ... // Longer than 16 characters! Error will print to console. Employee emp2 = new Employee(); emp2.SetName(“Xena the warrior princess“); Console.ReadLine(); } So far, so good. You have encapsulated the private empName field using two public methods named GetName() and SetName(). If you were to further encapsulate the data in the Employee class, you would need to add various additional methods (such as GetID(), SetID(), GetCurrentPay(), SetCurrentPay()). Each of the mutator methods could have within it various lines of code to check for additional business rules. While this could certainly be done, the C# language has a useful alternative notation to encapsulate class data. Encapsulation Using .NET Properties Although you can encapsulate a piece of field data using traditional get and set methods, .NET languages prefer to enforce data encapsulation state data using properties. First of all, understand that properties are just a simplification for “real” accessor and mutator methods. Therefore, as a class designer, you are still able to perform any internal logic necessary before making the value assignment (e.g., uppercase the value, scrub the value for illegal characters, check the bounds of a numerical value, and so on). CHAPTER 5  UNDERSTANDING ENCAPSULATION 194 Here is the updated Employee class, now enforcing encapsulation of each field using property syntax rather than traditional get and set methods: class Employee { // Field data. private string empName; private int empID; private float currPay; // Properties! public string Name { get { return empName; } set { if (value.Length > 15) Console.WriteLine(“Error! Name must be less than 16 characters!“); else empName = value; } } // We could add additional business rules to the sets of these properties; // however, there is no need to do so for this example. public int ID { get { return empID; } set { empID = value; } } public float Pay { get { return currPay; } set { currPay = value; } } ... } A C# property is composed by defining a get scope (accessor) and set scope (mutator) directly within the property itself. Notice that the property specifies the type of data it is encapsulating by what appears to be a return value. Also take note that, unlike a method, properties do not make use of parentheses (not even empty parentheses) when being defined. Consider the commentary on your current ID property: // The 'int' represents the type of data this property encapsulates. // The data type must be identical to the related field (empID). public int ID // Note lack of parentheses. { get { return empID; } set { empID = value; } } CHAPTER 5  UNDERSTANDING ENCAPSULATION 195 Within a “set” scope of a property, you use a token named value, which is used to represent the incoming value used to assign the property by the caller. This token is not a true C# keyword, but is what is known as a contextual keyword. When the token value is within the set scope of the property, it always represents the value being assigned by the caller, and it will always be the same underlying data type as the property itself. Thus, notice how the Name property can still test the range of the string as so: public string Name { get { return empName; } set { // Here, value is really a string. if (value.Length > 15) Console.WriteLine(“Error! Name must be less than 16 characters!“); else empName = value; } } After you have these properties in place, it appears to the caller that it is getting and setting a public point of data; however, the correct get and set block is called behind the scenes to preserve encapsulation: static void Main(string[] args) { Console.WriteLine(“***** Fun with Encapsulation *****\n“); Employee emp = new Employee(“Marvin“, 456, 30000); emp.GiveBonus(1000); emp.DisplayStats(); // Set and get the Name property. emp.Name = “Marv“; Console.WriteLine(“Employee is named: {0}“, emp.Name); Console.ReadLine(); } Properties (as opposed to accessors and mutators) also make your types easier to manipulate, in that properties are able to respond to the intrinsic operators of C#. To illustrate, assume that the Employee class type has an internal private member variable representing the age of the employee. Here is the relevant update (notice the use of constructor chaining): class Employee { ... // New field and property. private int empAge; public int Age { get { return empAge; } set { empAge = value; } } // Updated constructors. public Employee() {} CHAPTER 5  UNDERSTANDING ENCAPSULATION 196 public Employee(string name, int id, float pay) :this(name, 0, id, pay){} public Employee(string name, int age, int id, float pay) { empName = name; empID = id; empAge = age; currPay = pay; } // Updated DisplayStats() method now accounts for age. public void DisplayStats() { Console.WriteLine(“Name: {0}“, empName); Console.WriteLine(“ID: {0}“, empID); Console.WriteLine(“Age: {0}“, empAge); Console.WriteLine(“Pay: {0}“, currPay); } } Now assume you have created an Employee object named joe. On his birthday, you want to increment the age by one. Using traditional accessor and mutator methods, you would need to write code such as the following: Employee joe = new Employee(); joe.SetAge(joe.GetAge() + 1); However, if you encapsulate empAge using a property named Age, you are able to simply write Employee joe = new Employee(); joe.Age++; Using Properties Within a Class Definition Properties, specifically the “set” portion of a property, are common places to package up the business rules of your class. Currently, the Employee class has a Name property that ensures the name is no more than 15 characters. The remaining properties (ID, Pay, and Age) could also be updated with any relevant logic. While this is well and good, also consider what a class constructor typically does internally. It will take the incoming parameters, check for valid data, and then make assignments to the internal private fields. Currently, your master constructor does not test the incoming string data for a valid range, so you could update this member as so: public Employee(string name, int age, int id, float pay) { // Humm, this seems like a problem... if (name.Length > 15) Console.WriteLine(“Error! Name must be less than 16 characters!“); else empName = name; empID = id; empAge = age; CHAPTER 5  UNDERSTANDING ENCAPSULATION 197 currPay = pay; } I am sure you can see the problem with this approach. The Name property and your master constructor are performing the same error checking! If you were also making checks on the other data points, you would have a good deal of duplicate code. To streamline your code, and isolate all of your error checking to a central location, you will do well if you always use properties within your class whenever you need to get or set the values. Consider the following updated constructor: public Employee(string name, int age, int id, float pay) { // Better! Use properties when setting class data. // This reduces the amount of duplicate error checks. Name = name; Age = age; ID = id; Pay = pay; } Beyond updating constructors to use properties when assigning values, it is good practice to use properties throughout a class implementation, to ensure your business rules are always enforced. In many cases, the only time when you directly make reference to the underlying private piece of data is within the property itself. With this in mind, here is your updated Employee class: class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; // Constructors. public Employee() { } public Employee(string name, int id, float pay) :this(name, 0, id, pay){} public Employee(string name, int age, int id, float pay) { Name = name; Age = age; ID = id; Pay = pay; } // Methods. public void GiveBonus(float amount) { Pay += amount; } public void DisplayStats() { Console.WriteLine(“Name: {0}“, Name); Console.WriteLine(“ID: {0}“, ID); Console.WriteLine(“Age: {0}“, Age); CHAPTER 5  UNDERSTANDING ENCAPSULATION 198 Console.WriteLine(“Pay: {0}“, Pay); } // Properties as before... ... } Read-Only and Write-Only Properties When encapsulating data, you might want to configure a read-only property. To do so, simply omit the set block. Likewise, if you want to have a write-only property, omit the get block. For example, assume you wanted a new property named SocialSecurityNumber, which encapsulates a private string variable named empSSN. If you want to make this a read-only property, you could write: public string SocialSecurityNumber { get { return empSS; } } Now assume our class constructor has a new parameter to let the caller set the SSN of the object. Since the SocialSecurityNumber property is read only, we cannot set the value as so: public Employee(string name, int age, int id, float pay, string ssn) { Name = name; Age = age; ID = id; Pay = pay; // OOPS! This is no longer possible if the property is read // only. SocialSecurityNumber = ssn; } Unless we are willing to redesign the property as read/write, your only choice would be to use the underlying empSSN member variable within your constructor logic as so: public Employee(string name, int age, int id, float pay, string ssn) { ... empSSN = ssn; } To wrap up the story thus far, recall that C# prefers properties to encapsulate data. These syntactic entities are used for the same purpose as traditional accessor (get)/mutator (set) methods. The benefit of properties is that the users of your objects are able to manipulate the internal data point using a single named item.  Source Code The EmployeeApp project can be found under the Chapter 5 subdirectory. CHAPTER 5  UNDERSTANDING ENCAPSULATION 199 Revisiting the static Keyword: Defining Static Properties Earlier in this chapter, you examined the role of the static keyword. Now that you understand the use of C# property syntax, we can formalize static properties. In the StaticDataAndMembers project, our SavingsAccount class had two public static properties to get and set the interest rate. However, it would be more standard to wrap this data point in a property. Thus, if you would rather not have two methods to get and set the interest rate, you could instead define this following class property (note the use of the static keyword): // A simple savings account class. class SavingsAccount { // Instance-level data. public double currBalance; // A static point of data. private static double currInterestRate = 0.04; // A static property. public static double InterestRate { get { return currInterestRate; } set { currInterestRate = value; } } ... } If you want to use this property in place of the previous static methods, you could update your Main() method as so: // Print the current interest rate via property. Console.WriteLine(“Interest Rate is: {0}“, SavingsAccount.InterestRate); Understanding Automatic Properties When you are building properties to encapsulate your data, it is common to find that the set scopes have code to enforce business rules of your program. However, in some cases you may not need any implementation logic beyond simply getting and setting the value. This means you can end up with a lot of code looking like the following: // A Car type using standard property // syntax. class Car { private string carName = ““; public string PetName { get { return carName; } set { carName = value; } } } CHAPTER 5  UNDERSTANDING ENCAPSULATION 200 In these cases, it can become rather verbose to define private backing fields and simple property definitions multiple times. By way of an example, if you are modeling a class that requires 15 private points of field data, you end up authoring 15 related properties that are little more than thin wrappers for encapsulation services. To streamline the process of providing simple encapsulation of field data, you may use automatic property syntax. As the name implies, this feature will offload the work of defining a private backing field and the related C# property member to the compiler using a new bit of syntax. To illustrate, create a new Console Application named AutoProps. Now, consider the reworking of the Car class, which uses this syntax to quickly create three properties: class Car { // Automatic properties! public string PetName { get; set; } public int Speed { get; set; } public string Color { get; set; } }  Note Visual Studio provides the prop code snippet. If you type “prop” and press the Tab key twice, the IDE will generate starter code for a new automatic property! You can then use the Tab key to cycle through each part of the definition to fill in the details. Give it a try! When defining automatic properties, you simply specify the access modifier, underlying data type, property name, and empty get/set scopes. At compile time, your type will be provided with an autogenerated private backing field and a fitting implementation of the get/set logic.  Note The name of the autogenerated private backing field is not visible within your C# code base. The only way to see it is to make use of a tool such as ildasm.exe. Unlike traditional C# properties, however, it is not possible to build read-only or write-only automatic properties. While you might think you can just omit the get; or set; within your property declaration as follows: // Read-only property? Error! public int MyReadOnlyProp { get; } // Write only property? Error! public int MyWriteOnlyProp { set; } this will result in a compiler error. When you are defining an automatic property, it must support both read and write functionality. Recall the following: // Automatic properties must be read and write. public string PetName { get; set; } CHAPTER 5  UNDERSTANDING ENCAPSULATION 201 Interacting with Automatic Properties Because the compiler will define the private backing field at compile time, the class defining automatic properties will always need to use property syntax to get and set the underlying value. This is important to note because many programmers make direct use of the private fields within a class definition, which is not possible in this case. For example, if the Car class were to provide a DisplayStats() method, it would need to implement this method using the property name: class Car { // Automatic properties! public string PetName { get; set; } public int Speed { get; set; } public string Color { get; set; } public void DisplayStats() { Console.WriteLine(“Car Name: {0}“, PetName); Console.WriteLine(“Speed: {0}“, Speed); Console.WriteLine(“Color: {0}“, Color); } } When you are using an object defined with automatic properties, you will be able to assign and obtain the values using the expected property syntax: static void Main(string[] args) { Console.WriteLine(“***** Fun with Automatic Properties *****\n“); Car c = new Car(); c.PetName = “Frank“; c.Speed = 55; c.Color = “Red“; Console.WriteLine(“Your car is named {0}? That's odd...“, c.PetName); c.DisplayStats(); Console.ReadLine(); } Regarding Automatic Properties and Default Values When you use automatic properties to encapsulate numerical or Boolean data, you are able to use the autogenerated type properties straightaway within your code base, as the hidden backing fields will be assigned a safe default value that can be used directly. However, be very aware that if you use automatic property syntax to wrap another class variable, the hidden private reference type will also be set to a default value of null. Consider the following new class named Garage, which makes use of two automatic properties (of course, a real garage class would maintain a collection of Car objects; however, we will ignore that detail for the time being): CHAPTER 5  UNDERSTANDING ENCAPSULATION 202 class Garage { // The hidden int backing field is set to zero! public int NumberOfCars { get; set; } // The hidden Car backing field is set to null! public Car MyAuto { get; set; } } Given C#’s default values for field data, you would be able to print out the value of NumberOfCars as is (as it is automatically assigned the value of zero), but if you directly invoke MyAuto, you will receive a “null reference exception” at runtime, as the Car member variable used in the background has not been assigned to a new object: static void Main(string[] args) { ... Garage g = new Garage(); // OK, prints default value of zero. Console.WriteLine(“Number of Cars: {0}“, g.NumberOfCars); // Runtime error! Backing field is currently null! Console.WriteLine(g.MyAuto.PetName); Console.ReadLine(); } Given that the private backing fields are created at compile time, you will be unable to make use of C# field initialization syntax to allocate the reference type directly with the new keyword. Therefore, this work will need to be done with class constructors to ensure the object comes to life in a safe manner. For example: class Garage { // The hidden backing field is set to zero! public int NumberOfCars { get; set; } // The hidden backing field is set to null! public Car MyAuto { get; set; } // Must use constructors to override default // values assigned to hidden backing fields. public Garage() { MyAuto = new Car(); NumberOfCars = 1; } public Garage(Car car, int number) { MyAuto = car; NumberOfCars = number; } } CHAPTER 5  UNDERSTANDING ENCAPSULATION 203 With this update, you could now place a Car object into the Garage object as so: static void Main(string[] args) { Console.WriteLine(“***** Fun with Automatic Properties *****\n“); // Make a car. Car c = new Car(); c.PetName = “Frank“; c.Speed = 55; c.Color = “Red“; c.DisplayStats(); // Put car in the garage. Garage g = new Garage(); g.MyAuto = c; Console.WriteLine(“Number of Cars in garage: {0}“, g.NumberOfCars); Console.WriteLine(“Your car is named: {0}“, g.MyAuto.PetName); Console.ReadLine(); } As you most likely agree, this is a very nice feature of the C# programming language, as you can define a number of properties for a class using a streamlined syntax. Be aware, of course, that if you are building a property that requires additional code beyond getting and setting the underlying private field (such as data validation logic, writing to an event log, communicating with a database, etc.), you will be required to define a “normal” .NET property type by hand. C# automatic properties never do more than provide simple encapsulation for an underlying piece of (compiler-generated) private data.  Source Code The AutoProps project can be found under the Chapter 5 subdirectory. Understanding Object Initialization Syntax As seen throughout this chapter, a constructor allows you specify start-up values when creating a new object. On a related note, properties allow you to get and set underlying data in a safe manner. When you are working with other people’s classes, including the classes found within the .NET base class library, it is not too uncommon to discover that there is not a single constructor that allows you to set every piece of underlying state data. Given this point, a programmer is typically forced to pick the best constructor possible, after which he or she makes assignments using a handful of provided properties. To help streamline the process of getting an object up and running, C# offers object initializer syntax. Using this technique, it is possible to create a new object variable and assign a slew of properties and/or public fields in a few lines of code. Syntactically, an object initializer consists of a comma- delimited list of specified values, enclosed by the { and } tokens. Each member in the initialization list maps to the name of a public field or public property of the object being initialized. To see this syntax in action, create a new Console Application named ObjectInitializers. Now, consider a simple class named Point, created using automatic properties (which is not mandatory for this example, but helps us write some very concise code): CHAPTER 5  UNDERSTANDING ENCAPSULATION 204 class Point { public int X { get; set; } public int Y { get; set; } public Point(int xVal, int yVal) { X = xVal; Y = yVal; } public Point() { } public void DisplayStats() { Console.WriteLine(“[{0}, {1}]“, X, Y); } } Now consider how we can make Point objects using any of the following approaches: static void Main(string[] args) { Console.WriteLine(“***** Fun with Object Init Syntax *****\n“); // Make a Point by setting each property manually. Point firstPoint = new Point(); firstPoint.X = 10; firstPoint.Y = 10; firstPoint.DisplayStats(); // Or make a Point via a custom constructor. Point anotherPoint = new Point(20, 20); anotherPoint.DisplayStats(); // Or make a Point using object init syntax. Point finalPoint = new Point { X = 30, Y = 30 }; finalPoint.DisplayStats(); Console.ReadLine(); } The final Point variable is not making use of a custom constructor (as one might do traditionally), but is rather setting values to the public X and Y properties. Behind the scenes, the type’s default constructor is invoked, followed by setting the values to the specified properties. To this end, object initialization syntax is just shorthand notations for the syntax used to create a class variable using a default constructor, and setting the state data property by property. Calling Custom Constructors with Initialization Syntax The previous examples initialized Point types by implicitly calling the default constructor on the type: // Here, the default constructor is called implicitly. Point finalPoint = new Point { X = 30, Y = 30 }; CHAPTER 5  UNDERSTANDING ENCAPSULATION 205 If you want to be very clear about this, it is permissible to explicitly call the default constructor as follows: // Here, the default constructor is called explicitly. Point finalPoint = new Point() { X = 30, Y = 30 }; Do be aware that when you are constructing a type using the new initialization syntax, you are able to invoke any constructor defined by the class. Our Point type currently defines a two-argument constructor to set the (x, y) position. Therefore, the following Point declaration results in an X value of 100 and a Y value of 100, regardless of the fact that our constructor arguments specified the values 10 and 16: // Calling a custom constructor. Point pt = new Point(10, 16) { X = 100, Y = 100 }; Given the current definition of your Point type, calling the custom constructor while using initialization syntax is not terribly useful (and more than a bit verbose). However, if your Point type provides a new constructor that allows the caller to establish a color (via a custom enum named PointColor), the combination of custom constructors and object initialization syntax becomes clear. Assume you have updated Point as follows: public enum PointColor { LightBlue, BloodRed, Gold } class Point { public int X { get; set; } public int Y { get; set; } public PointColor Color{ get; set; } public Point(int xVal, int yVal) { X = xVal; Y = yVal; Color = PointColor.Gold; } public Point(PointColor ptColor) { Color = ptColor; } public Point() : this(PointColor.BloodRed){ } public void DisplayStats() { Console.WriteLine(“[{0}, {1}]“, X, Y); Console.WriteLine(“Point is {0}“, Color); } } With this new constructor, you can now create a gold point (positioned at 90, 20) as follows: CHAPTER 5  UNDERSTANDING ENCAPSULATION 206 // Calling a more interesting custom constructor with init syntax. Point goldPoint = new Point(PointColor.Gold){ X = 90, Y = 20 }; goldPoint.DisplayStats(); Initializing Inner Types As briefly mentioned earlier in this chapter (and fully examined in Chapter 6), the “has-a” relationship allows us to compose new classes by defining member variables of existing classes. For example, assume you now have a Rectangle class, which makes use of the Point type to represent its upper-left/bottom- right coordinates. Since automatic properties set all internal class variables to null, you will implement this new class using “traditional” property syntax: class Rectangle { private Point topLeft = new Point(); private Point bottomRight = new Point(); public Point TopLeft { get { return topLeft; } set { topLeft = value; } } public Point BottomRight { get { return bottomRight; } set { bottomRight = value; } } public void DisplayStats() { Console.WriteLine(“[TopLeft: {0}, {1}, {2} BottomRight: {3}, {4}, {5}]“, topLeft.X, topLeft.Y, topLeft.Color, bottomRight.X, bottomRight.Y, bottomRight.Color); } } Using object initialization syntax, you could create a new Rectangle variable and set the inner Points as follows: // Create and initialize a Rectangle. Rectangle myRect = new Rectangle { TopLeft = new Point { X = 10, Y = 10 }, BottomRight = new Point { X = 200, Y = 200} }; Again, the benefit of object initialization syntax is that it basically decreases the number of keystrokes (assuming there is not a suitable constructor). Here is the traditional approach to establishing a similar Rectangle: // Old-school approach. Rectangle r = new Rectangle(); Point p1 = new Point(); CHAPTER 5  UNDERSTANDING ENCAPSULATION 207 p1.X = 10; p1.Y = 10; r.TopLeft = p1; Point p2 = new Point(); p2.X = 200; p2.Y = 200; r.BottomRight = p2; While you might feel object initialization syntax can take a bit of getting used to, once you get comfortable with the code, you’ll be quite pleased at how quickly you can establish the state of a new object with minimal fuss and bother. To wrap up this chapter, allow me to close with three bite-sized topics that will round out your understanding of building well-encapsulated classes: constant data, read-only fields, and partial class definitions.  Source Code The ObjectInitilizers project can be found under the Chapter 5 subdirectory. Working with Constant Field Data C# offers the const keyword to define constant data, which can never change after the initial assignment. As you might guess, this can be helpful when you are defining a set of known values for use in your applications that are logically connected to a given class or structure. Assume you are building a utility class named MyMathClass that needs to define a value for the value PI (which you will assume to be 3.14). Begin by creating a new Console Application project named ConstData. Given that you would not want to allow other developers to change this value in code, PI could be modeled with the following constant: namespace ConstData { class MyMathClass { public const double PI = 3.14; } class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with Const *****\n“); Console.WriteLine(“The value of PI is: {0}“, MyMathClass.PI); // Error! Can't change a constant! // MyMathClass.PI = 3.1444; Console.ReadLine(); } } } CHAPTER 5  UNDERSTANDING ENCAPSULATION 208 Notice that you are referencing the constant data defined by MyMathClass using a class name prefix (i.e., MyMathClass.PI). This is due to the fact that constant fields of a class are implicitly static. However, it is permissible to define and access a local constant variable within a type member. By way of example: static void LocalConstStringVariable() { // A local constant data point can be directly accessed. const string fixedStr = “Fixed string Data“; Console.WriteLine(fixedStr); // Error! fixedStr = “This will not work!“; } Regardless of where you define a constant piece of data, the one point to always remember is that the initial value assigned to the constant must be specified at the time you define the constant. Thus, if you were to modify your MyMathClass in such a way that the value of PI is assigned in a class constructor as follows: class MyMathClass { // Try to set PI in ctor? public const double PI; public MyMathClass() { // Error! PI = 3.14; } } you would receive a compile-time error. The reason for this restriction has to do with the fact the value of constant data must be known at compile time. Constructors, as you know, are invoked at runtime. Understanding Read-Only Fields Closely related to constant data is the notion of read-only field data (which should not be confused with a read-only property). Like a constant, a read-only field cannot be changed after the initial assignment. However, unlike a constant, the value assigned to a read-only field can be determined at runtime and, therefore, can legally be assigned within the scope of a constructor, but nowhere else. This can be very helpful when you don’t know the value of a field until runtime, perhaps because you need to read an external file to obtain the value, but wish to ensure that the value will not change after that point. For the sake of illustration, assume the following update to MyMathClass: class MyMathClass { // Read-only fields can be assigned in ctors, // but nowhere else. public readonly double PI; public MyMathClass () { PI = 3.14; CHAPTER 5  UNDERSTANDING ENCAPSULATION 209 } } Again, any attempt to make assignments to a field marked readonly outside the scope of a constructor results in a compiler error: class MyMathClass { public readonly double PI; public MyMathClass () { PI = 3.14; } // Error! public void ChangePI() { PI = 3.14444;} } Static Read-Only Fields Unlike a constant field, read-only fields are not implicitly static. Thus, if you want to expose PI from the class level, you must explicitly make use of the static keyword. If you know the value of a static read- only field at compile time, the initial assignment looks very similar to that of a constant (however in this case, it would be easier to simply use the const keyword in the first place, as we are assigning the data field at the time of declaration): class MyMathClass { public static readonly double PI = 3.14; } class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with Const *****“); Console.WriteLine(“The value of PI is: {0}“, MyMathClass.PI); Console.ReadLine(); } } However, if the value of a static read-only field is not known until runtime, you must make use of a static constructor as described earlier in this chapter: class MyMathClass { public static readonly double PI; static MyMathClass() { PI = 3.14; } } CHAPTER 5  UNDERSTANDING ENCAPSULATION 210  Source Code The ConstData project is included under the Chapter 5 subdirectory. Understanding Partial Types Last but not least, it is important to understand the role of the C# partial keyword. A production-level class could very easily consist of hundreds and hundreds of lines of code. As well, given that a typical class is defined within a single *.cs file, you could end up with a very long file indeed. When you are creating your classes, it is often the case that much of the code can be basically ignored after it is accounted for. For example, field data, properties, and constructors tend to remain as-is during production, while methods tend to be modified quite often. If you want, you can partition a single class across multiple C# files, to isolate the boilerplate code from more readily useful members. To illustrate, open up the EmployeeApp project you created previously in this chapter into Visual Studio, and after you have done so, open the Employee.cs file for editing. Currently, this single file contains code of all aspects of the class: class Employee { // Field Data // Constructors // Methods // Properties } Using partial classes, you could choose to move the constructors and field data into a brand new file named Employee.Internal.cs (please note, the name of the file is irrelevant; here I just tacked on the work internal to represent the guts of the class). The first step is to add the partial keyword to the current class definition and cut the code to be placed into the new file: // Employee.cs partial class Employee { // Methods // Properties } Next, assuming you have inserted a new class file into your project, you can move the data fields and constructors to the new file using a simple cut/paste operation. In addition, you must add the partial keyword to this aspect of the class definition. For example: // Employee.Internal.cs partial class Employee { // Field data // Constructors } CHAPTER 5  UNDERSTANDING ENCAPSULATION 211  Note Remember that every aspect of a partial class definition must be marked with the partial keyword! After you compile the modified project, you should see no difference whatsoever. The whole idea of a partial class is only realized during design time. After the application has been compiled, there is just a single, unified class within the assembly. The only requirement when defining partial types is that the type’s name (Employee in this case) is identical and defined within the same .NET namespace. To be honest, you will most likely not need to make use of partial class definitions too often. However, Visual Studio uses them in the background all the time. Later in this book, when you start to look into GUI application development using Windows Presentation Foundation or ASP.NET, you’ll see that Visual Studio isolates designer-generated code into a partial class, leaving you to focus on your application-specific programming logic.  Source Code The EmployeeAppPartial project can be found under the Chapter 5 subdirectory. Summary The point of this chapter was to introduce you to the role of the C# class type. As you have seen, classes can take any number of constructors that enable the object user to establish the state of the object upon creation. This chapter also illustrated several class design techniques (and related keywords). Recall that the this keyword can be used to obtain access to the current object, the static keyword allows you to define fields and members that are bound at the class (not object) level, and the const keyword (and readonly modifier) allows you to define a point of data that can never change after the initial assignment. The bulk of this chapter dug into the details of the first pillar of OOP: encapsulation. Here you learned about the access modifiers of C# and the role of type properties, object initialization syntax, and partial classes. With this behind you, you are now able to turn to the next chapter where you will learn to build a family of related classes using inheritance and polymorphism. C H A P T E R 6 213 Understanding Inheritance and Polymorphism Chapter 5 examined the first pillar of OOP: encapsulation. At that time, you learned how to build a single well-defined class type with constructors and various members (constructors, fields, properties, methods, constants, and read-only fields). This chapter will focus on the remaining two pillars of OOP: inheritance and polymorphism. First, you will learn how to build families of related classes using inheritance. As you will see, this form of code reuse allows you to define common functionality in a parent class that can be leveraged, and possibly altered, by child classes. Along the way, you will learn how to establish a polymorphic interface into class hierarchies using virtual and abstract members, as well as the role of explicit casting. We’ll wrap up by examining the role of the ultimate parent class in the .NET base class libraries: System.Object. The Basic Mechanics of Inheritance Recall from Chapter 5 that inheritance is the aspect of OOP that facilitates code reuse. Specifically speaking, code reuse comes in two flavors: inheritance (the “is-a” relationship) and the containment/delegation model (the “has-a” relationship). Let’s begin this chapter by examining the classical inheritance model of the “is-a” relationship. When you establish “is-a” relationships between classes, you are building a dependency between two or more class types. The basic idea behind classical inheritance is that new classes can be created using existing classes as a starting point. To begin with a very simple example, create a new Console Application project named BasicInheritance. Now assume you have designed a class named Car that models some basic details of an automobile: // A simple base class. class Car { public readonly int maxSpeed; private int currSpeed; public Car(int max) { maxSpeed = max; } public Car() { maxSpeed = 55; } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 214 public int Speed { get { return currSpeed; } set { currSpeed = value; if (currSpeed > maxSpeed) { currSpeed = maxSpeed; } } } } Notice that the Car class is making use of encapsulation services to control access to the private currSpeed field using a public property named Speed. At this point, you can exercise your Car type as follows: static void Main(string[] args) { Console.WriteLine(“***** Basic Inheritance *****\n“); // Make a Car object and set max speed. Car myCar = new Car(80); // Set the current speed, and print it. myCar.Speed = 50; Console.WriteLine(“My car is going {0} MPH“, myCar.Speed); Console.ReadLine(); } Specifying the Parent Class of an Existing Class Now assume you want to build a new class named MiniVan. Like a basic Car, you want to define the MiniVan class to support data for a maximum speed, current speed, and a property named Speed to allow the object user to modify the object’s state. Clearly, the Car and MiniVan classes are related; in fact, it can be said that a MiniVan “is-a” type of Car. The “is-a” relationship (formally termed classical inheritance) allows you to build new class definitions that extend the functionality of an existing class. The existing class that will serve as the basis for the new class is termed a base or parent class. The role of a base class is to define all the common data and members for the classes that extend it. The extending classes are formally termed derived or child classes. In C#, you make use of the colon operator on the class definition to establish an “is-a” relationship between classes. Assume you have authored the following new MiniVan class: // MiniVan “is-a“ Car. class MiniVan : Car { } Currently, this new class has not defined any members whatsoever. So, what have you gained by extending your MiniVan from the Car base class? Simply put, MiniVan objects now have access to each public member defined within the parent class. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 215  Note Although constructors are typically defined as public, a derived class never inherits the constructors of a parent class. Constructors are only used to construct the class that they are defined within. Given the relation between these two class types, you could now make use of the MiniVan class like so: static void Main(string[] args) { Console.WriteLine(“***** Basic Inheritance *****\n“); ... // Now make a MiniVan object. MiniVan myVan = new MiniVan(); myVan.Speed = 10; Console.WriteLine(“My van is going {0} MPH“, myVan.Speed); Console.ReadLine(); } Again, notice that although you have not added any members to the MiniVan class, you have direct access to the public Speed property of your parent class, and have thus reused code. This is a far better approach than creating a MiniVan class that has the exact same members as Car, such as a Speed property. If you did duplicate code between these two classes, you would need to now maintain two bodies of code, which is certainly a poor use of your time. Always remember, that inheritance preserves encapsulation; therefore, the following code results in a compiler error, as private members can never be accessed from an object reference. static void Main(string[] args) { Console.WriteLine(“***** Basic Inheritance *****\n“); ... // Make a MiniVan object. MiniVan myVan = new MiniVan(); myVan.Speed = 10; Console.WriteLine(“My van is going {0} MPH“, myVan.Speed); // Error! Can't access private members! myVan.currSpeed = 55; Console.ReadLine(); } On a related note, if the MiniVan defined its own set of members, it would still not be able to access any private member of the Car base class. Remember, private members can only be accessed by the class that defines it. For example, the following method in MiniVan would result in a compiler error: // MiniVan derives from Car. class MiniVan : Car { public void TestMethod() { CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 216 // OK! Can access public members // of a parent within a derived type. Speed = 10; // Error! Cannot access private // members of parent within a derived type. currSpeed = 10; } } Regarding Multiple Base Classes Speaking of base classes, it is important to keep in mind that C# demands that a given class have exactly one direct base class. It is not possible to create a class type that directly derives from two or more base classes (this technique [which is supported in unmanaged C++] is known as multiple inheritance, or simply MI). If you attempted to create a class that specifies two direct parent classes, as shown in the following code, you will receive compiler errors. // Illegal! C# does not allow // multiple inheritance for classes! class WontWork : BaseClassOne, BaseClassTwo {} As you will see in Chapter 8, the .NET platform does allow a given class, or structure, to implement any number of discrete interfaces. In this way, a C# type can exhibit a number of behaviors while avoiding the complexities associated with MI. On a related note, while a class can have only one direct base class, it is permissible for an interface to directly derive from multiple interfaces. Using this technique, you can build sophisticated interface hierarchies that model complex behaviors (again, see Chapter 8). The sealed Keyword C# supplies another keyword, sealed, that prevents inheritance from occurring. When you mark a class as sealed, the compiler will not allow you to derive from this type. For example, assume you have decided that it makes no sense to further extend the MiniVan class: // The MiniVan class cannot be extended! sealed class MiniVan : Car { } If you (or a teammate) were to attempt to derive from this class, you would receive a compile-time error: // Error! Cannot extend // a class marked with the sealed keyword! class DeluxeMiniVan : MiniVan {} Most often, sealing a class makes the best sense when you are designing a utility class. For example, the System namespace defines numerous sealed classes. You can verify this for yourself by opening up CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 217 the Visual Studio Object Browser (via the View menu) and selecting the String class within the System namespace of the mscorlib.dll assembly. Notice in Figure 6-1 the use of the sealed keyword seen within the Summary window. Figure 6-1. The base class libraries define numerous sealed types, such as System.String Thus, just like the MiniVan, if you attempted to build a new class that extends System.String, you will receive a compile-time error: // Another error! Cannot extend // a class marked as sealed! class MyString : String {}  Note In Chapter 4, you learned that C# structures are always implicitly sealed (see Table 4-3). Therefore, you can never derive one structure from another structure, a class from a structure, or a structure from a class. Structures can only be used to model stand-alone, atomic, user-defined data types. If you want to leverage the is- a relationship, you must use classes. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 218 As you would guess, there are many more details to inheritance that you will come to know during the remainder of this chapter. For now, simply keep in mind that the colon operator allows you to establish base/derived class relationships, while the sealed keyword prevents subsequent inheritance from occurring. Revising Visual Studio Class Diagrams In Chapter 2, I briefly mentioned that Visual Studio allows you to establish base/derived class relationships visually at design time. To leverage this aspect of the IDE, your first step is to include a new class diagram file into your current project. To do so, access the Project Add New Item menu option and select the Class Diagram icon (in Figure 6-2, I renamed the file from ClassDiagram1.cd to Cars.cd). Figure 6-2. Inserting a new class diagram After you click the Add button, you will be presented with a blank designer surface. To add types to a class designer, simply drag each file from the Solution Explorer window onto the surface. Also recall that if you delete an item from the visual designer (simply by selecting it and pressing the Delete key), this will not destroy the associated source code, but simply removes the item off the designer surface. The current class hierarchy is shown in Figure 6-3. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 219 Figure 6-3. The visual designer of Visual Studio  Note As a shortcut, if you wish to automatically add all of your project’s current types to a designer surface, select the Project node within the Solution Explorer and then click the View Class Diagram button in the upper right of the Solution Explorer window. Beyond simply displaying the relationships of the types within your current application, recall from Chapter 2 that you can also create brand new types and populate their members using the Class Designer toolbox and Class Details window. If you want to make use of these visual tools during the remainder of the book, feel free. However, always make sure you analyze the generated code so you have a solid understanding of what these tools have done on your behalf.  Source Code The BasicInheritance project is located under the Chapter 6 subdirectory. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 220 The Second Pillar of OOP: The Details of Inheritance Now that you have seen the basic syntax of inheritance, let’s create a more complex example and get to know the numerous details of building class hierarchies. To do so, you will be reusing the Employee class you designed in Chapter 5. To begin, create a brand new C# Console Application named Employees. Next, activate the Project  Add Existing Item menu option and navigate to the location of your Employee.cs and Employee.Internals.cs files you created in the EmployeeApp example of Chapter 5. Select each of them (via a Ctrl+click) and click the Add button. Visual Studio responds by copying each file into the current project. Before you start to build some derived classes, you have one detail to attend to. Because the original Employee class was created in a project named EmployeeApp, the class has been wrapped within an identically named .NET namespace. Chapter 14 will examine namespaces in detail; however, for simplicity, rename the current namespace (in both file locations) to Employees in order to match your new project name: // Be sure to change the namespace name in both C# files! namespace Employees { partial class Employee {...} }  Note As a sanity check, compile and run your new project by pressing Ctrl+F5. The program will not do anything at this point; however, this will ensure you do not have any compiler errors. Your goal is to create a family of classes that model various types of employees in a company. Assume that you want to leverage the functionality of the Employee class to create two new classes (SalesPerson and Manager). The new SalesPerson class “is-an” Employee (as is a Manager). Remember that under the classical inheritance model, base classes (such as Employee) are used to define general characteristics that are common to all descendents. Subclasses (such as SalesPerson and Manager) extend this general functionality while adding more specific functionality. For your example, you will assume that the Manager class extends Employee by recording the number of stock options, while the SalesPerson class maintains the number of sales made. Insert a new class file (Manager.cs) that defines the Manager class with the following automatic property: // Managers need to know their number of stock options. class Manager : Employee { public int StockOptions { get; set; } } Next, add another new class file (SalesPerson.cs) that defines the SalesPerson class with a fitting automatic property: // Salespeople need to know their number of sales. class SalesPerson : Employee { public int SalesNumber { get; set; } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 221 } Now that you have established an “is-a” relationship, SalesPerson and Manager have automatically inherited all public members of the Employee base class. To illustrate, update your Main() method as follows: // Create a subclass object and access base class functionality. static void Main(string[] args) { Console.WriteLine(“***** The Employee Class Hierarchy *****\n“); SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = “Fred“; fred.SalesNumber = 50; Console.ReadLine(); } Controlling Base Class Creation with the base Keyword Currently, SalesPerson and Manager can only be created using the freebee default constructor (see Chapter 5). With this in mind, assume you have added a new six-argument constructor to the Manager type, which is invoked as follows: static void Main(string[] args) { ... // Assume Manager has a constructor matching this signature: // (string fullName, int age, int empID, // float currPay, string ssn, int numbOfOpts) Manager chucky = new Manager(“Chucky“, 50, 92, 100000, “333-23-2322“, 9000); Console.ReadLine(); } If you look at the parameter list, you can clearly see that most of these arguments should be stored in the member variables defined by the Employee base class. To do so, you might implement this custom constructor on the Manager class as follows: public Manager(string fullName, int age, int empID, float currPay, string ssn, int numbOfOpts) { // This property is defined by the Manager class. StockOptions = numbOfOpts; // Assign incoming parameters using the // inherited properties of the parent class. ID = empID; Age = age; Name = fullName; Pay = currPay; // OOPS! This would be a compiler error, // if the SSN property were read-only! SocialSecurityNumber = ssn; CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 222 } The first issue with this approach is that if you defined any property as read-only (for example, the SocialSecurityNumber property), you are unable to assign the incoming string parameter to this field, as seen in the final code statement of this custom constructor. The second issue is that you have indirectly created a rather inefficient constructor, given the fact that under C#, unless you say otherwise, the default constructor of a base class is called automatically before the logic of the derived constructor is executed. After this point, the current implementation accesses numerous public properties of the Employee base class to establish its state. Thus, you have really made seven hits (five inherited properties and two constructor calls) during the creation of a Manager object! To help optimize the creation of a derived class, you will do well to implement your subclass constructors to explicitly call an appropriate custom base class constructor, rather than the default. In this way, you are able to reduce the number of calls to inherited initialization members (which saves processing time). Let’s retrofit the custom constructor of the Manager type to do this very thing using the base keyword: public Manager(string fullName, int age, int empID, float currPay, string ssn, int numbOfOpts) : base(fullName, age, empID, currPay, ssn) { // This property is defined by the Manager class. StockOptions = numbOfOpts; } Here, the base keyword is hanging off the constructor signature (much like the syntax used to chain constructors on a single class using the this keyword, as was discussed in Chapter 5), which always indicates a derived constructor is passing data to the immediate parent constructor. In this situation, you are explicitly calling the five-parameter constructor defined by Employee and saving yourself unnecessary calls during the creation of the child class. The custom SalesPerson constructor looks almost identical: // As a general rule, all subclasses should explicitly call an appropriate // base class constructor. public SalesPerson(string fullName, int age, int empID, float currPay, string ssn, int numbOfSales) : base(fullName, age, empID, currPay, ssn) { // This belongs with us! SalesNumber = numbOfSales; }  Note You may use the base keyword whenever a subclass wishes to access a public or protected member defined by a parent class. Use of this keyword is not limited to constructor logic. You will see examples using base in this manner during the examination of polymorphism, later in this chapter. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 223 Finally, recall that once you add a custom constructor to a class definition, the default constructor is silently removed. Therefore, be sure to redefine the default constructor for the SalesPerson and Manager types. For example: // Add back the default ctor // in the Manager class as well. public SalesPerson() {} Keeping Family Secrets: The protected Keyword As you already know, public items are directly accessible from anywhere, while private items can only be accessed by the class that has defined them. Recall from Chapter 5 that C# takes the lead of many other modern object languages and provides an additional keyword to define member accessibility: protected. When a base class defines protected data or protected members, it establishes a set of items that can be accessed directly by any descendent. If you want to allow the SalesPerson and Manager child classes to directly access the data sector defined by Employee, you can update the original Employee class definition as follows: // Protected state data. partial class Employee { // Derived classes can now directly access this information. protected string empName; protected int empID; protected float currPay; protected int empAge; protected string empSSN; ... } The benefit of defining protected members in a base class is that derived types no longer have to access the data indirectly using public methods or properties. The possible downfall, of course, is that when a derived type has direct access to its parent’s internal data, it is very possible to accidentally bypass existing business rules found within public properties. When you define protected members, you are creating a level of trust between the parent and child class, as the compiler will not catch any violation of your type’s business rules. Finally, understand that as far as the object user is concerned, protected data is regarded as private (as the user is “outside” of the family). Therefore, the following is illegal: static void Main(string[] args) { // Error! Can't access protected data from object instance. Employee emp = new Employee(); emp.empName = “Fred“; } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 224  Note Although protected field data can break encapsulation, it is quite safe (and useful) to define protected methods. When building class hierarchies, it is very common to define a set of methods that are only for use by derived types and are not intended for use by the outside world. Adding a Sealed Class Recall that a sealed class cannot be extended by other classes. As mentioned, this technique is most often used when you are designing a utility class. However, when building class hierarchies, you might find that a certain branch in the inheritance chain should be “capped off,” as it makes no sense to further extend the linage. For example, assume you have added yet another class to your program (PTSalesPerson) that extends the existing SalesPerson type. Figure 6-4 shows the current update. Figure 6-4. The PTSalesPerson class PTSalesPerson is a class representing, of course, a part-time salesperson. For the sake of argument, let’s say that you want to ensure that no other developer is able to subclass from PTSalesPerson. (After all, how much more part-time can you get than “part-time”?) Again, to prevent others from extending a class, make use of the sealed keyword: sealed class PTSalesPerson : SalesPerson { public PTSalesPerson(string fullName, int age, int empID, float currPay, string ssn, int numbOfSales) :base (fullName, age, empID, currPay, ssn, numbOfSales) { } // Assume other members here... } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 225 Programming for Containment/Delegation Recall that code reuse comes in two flavors. You have just explored the classical “is-a” relationship. Before you examine the third pillar of OOP (polymorphism), let’s examine the “has-a” relationship (also known as the containment/delegation model or aggregation). Assume you have created a new class that models an employee benefits package, as follows: // This new type will function as a contained class. class BenefitPackage { // Assume we have other members that represent // dental/health benefits, and so on. public double ComputePayDeduction() { return 125.0; } } Obviously, it would be rather odd to establish an “is-a” relationship between the BenefitPackage class and the employee types. (Employee “is-a” BenefitPackage? I don’t think so.) However, it should be clear that some sort of relationship between the two could be established. In short, you would like to express the idea that each employee “has-a” BenefitPackage. To do so, you can update the Employee class definition as follows: // Employees now have benefits. partial class Employee { // Contain a BenefitPackage object. protected BenefitPackage empBenefits = new BenefitPackage(); ... } At this point, you have successfully contained another object. However, to expose the functionality of the contained object to the outside world requires delegation. Delegation is simply the act of adding public members to the containing class that make use of the contained object’s functionality. For example, you could update the Employee class to expose the contained empBenefits object using a custom property, as well as make use of its functionality internally using a new method named GetBenefitCost(): public partial class Employee { // Contain a BenefitPackage object. protected BenefitPackage empBenefits = new BenefitPackage(); // Expose certain benefit behaviors of object. public double GetBenefitCost() { return empBenefits.ComputePayDeduction(); } // Expose object through a custom property. public BenefitPackage Benefits { get { return empBenefits; } set { empBenefits = value; } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 226 } ... } In the following updated Main() method, notice how you can interact with the internal BenefitsPackage type defined by the Employee type: static void Main(string[] args) { Console.WriteLine(“***** The Employee Class Hierarchy *****\n“); ... Manager chucky = new Manager(“Chucky“, 50, 92, 100000, “333-23-2322“, 9000); double cost = chucky.GetBenefitCost(); Console.ReadLine(); } Understanding Nested Type Definitions Chapter 5 briefly mentioned the concept of nested types, which is a spin on the “has-a” relationship you have just examined. In C# (as well as other .NET languages), it is possible to define a type (enum, class, interface, struct, or delegate) directly within the scope of a class or structure. When you have done so, the nested (or “inner”) type is considered a member of the nesting (or “outer”) class, and in the eyes of the runtime can be manipulated like any other member (fields, properties, methods, and events). The syntax used to nest a type is quite straightforward: public class OuterClass { // A public nested type can be used by anybody. public class PublicInnerClass {} // A private nested type can only be used by members // of the containing class. private class PrivateInnerClass {} } Although the syntax is fairly clear, understanding why you would want to do this might not be readily apparent. To understand this technique, ponder the following traits of nesting a type: • Nested types allow you to gain complete control over the access level of the inner type, as they may be declared privately (recall that non-nested classes cannot be declared using the private keyword). • Because a nested type is a member of the containing class, it can access private members of the containing class. • Often, a nested type is only useful as a helper for the outer class, and is not intended for use by the outside world. When a type nests another class type, it can create member variables of the type, just as it would for any point of data. However, if you want to make use of a nested type from outside of the containing type, you must qualify it by the scope of the nesting type. Consider the following code: CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 227 static void Main(string[] args) { // Create and use the public inner class. OK! OuterClass.PublicInnerClass inner; inner = new OuterClass.PublicInnerClass(); // Compiler Error! Cannot access the private class. OuterClass.PrivateInnerClass inner2; inner2 = new OuterClass.PrivateInnerClass(); } To make use of this concept within the employees example, assume you have now nested the BenefitPackage directly within the Employee class type: partial class Employee { public class BenefitPackage { // Assume we have other members that represent // dental/health benefits, and so on. public double ComputePayDeduction() { return 125.0; } } ... } The nesting process can be as “deep” as you require. For example, assume you want to create an enumeration named BenefitPackageLevel, which documents the various benefit levels an employee may choose. To programmatically enforce the tight connection between Employee, BenefitPackage, and BenefitPackageLevel, you could nest the enumeration as follows: // Employee nests BenefitPackage. public partial class Employee { // BenefitPackage nests BenefitPackageLevel. public class BenefitPackage { public enum BenefitPackageLevel { Standard, Gold, Platinum } public double ComputePayDeduction() { return 125.0; } } ... } Because of the nesting relationships, note how you are required to make use of this enumeration: CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 228 static void Main(string[] args) { ... // Define my benefit level. Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; Console.ReadLine(); } Excellent! At this point, you have been exposed to a number of keywords (and concepts) that allow you to build hierarchies of related types via classical inheritance, containment, and nested types. If the details aren’t crystal clear right now, don’t sweat it. You will be building a number of additional hierarchies over the remainder of this book. Next up, let’s examine the final pillar of OOP: polymorphism. The Third Pillar of OOP: C#’s Polymorphic Support Recall that the Employee base class defined a method named GiveBonus(), which was originally implemented as follows: public partial class Employee { public void GiveBonus(float amount) { Pay += amount; } ... } Because this method has been defined with the public keyword, you can now give bonuses to salespeople and managers (as well as part-time salespeople): static void Main(string[] args) { Console.WriteLine(“***** The Employee Class Hierarchy *****\n“); // Give each employee a bonus? Manager chucky = new Manager(“Chucky“, 50, 92, 100000, “333-23-2322“, 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson(“Fran“, 43, 93, 3000, “932-32-3232“, 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadLine(); } The problem with the current design is that the publicly inherited GiveBonus() method operates identically for all subclasses. Ideally, the bonus of a salesperson or part-time salesperson should take into account the number of sales. Perhaps managers should gain additional stock options in conjunction with a monetary bump in salary. Given this, you are suddenly faced with an interesting question: “How can related types respond differently to the same request?” Again, glad you asked! CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 229 The virtual and override Keywords Polymorphism provides a way for a subclass to define its own version of a method defined by its base class, using the process termed method overriding. To retrofit your current design, you need to understand the meaning of the virtual and override keywords. If a base class wants to define a method that may be (but does not have to be) overridden by a subclass, it must mark the method with the virtual keyword: partial class Employee { // This method can now be “overridden“ by a derived class. public virtual void GiveBonus(float amount) { Pay += amount; } ... }  Note Methods that have been marked with the virtual keyword are (not surprisingly) termed virtual methods. When a subclass wants to change the implementation details of a virtual method, it does so using the override keyword. For example, the SalesPerson and Manager could override GiveBonus() as follows (assume that PTSalesPerson will not override GiveBonus() and, therefore, simply inherits the version defined by SalesPerson): class SalesPerson : Employee { ... // A salesperson's bonus is influenced by the number of sales. public override void GiveBonus(float amount) { int salesBonus = 0; if (SalesNumber >= 0 && SalesNumber <= 100) salesBonus = 10; else { if (SalesNumber >= 101 && SalesNumber <= 200) salesBonus = 15; else salesBonus = 20; } base.GiveBonus(amount * salesBonus); } } class Manager : Employee { ... CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 230 public override void GiveBonus(float amount) { base.GiveBonus(amount); Random r = new Random(); StockOptions += r.Next(500); } } Notice how each overridden method is free to leverage the default behavior using the base keyword. In this way, you have no need to completely reimplement the logic behind GiveBonus(), but can reuse (and possibly extend) the default behavior of the parent class. Also assume that the current DisplayStats() method of the Employee class has been declared virtually. By doing so, each subclass can override this method to account for displaying the number of sales (for salespeople) and current stock options (for managers). For example, consider the Manager’s version of the DisplayStats() method (the SalesPerson class would implement DisplayStats() in a similar manner to show the number of sales): public override void DisplayStats() { base.DisplayStats(); Console.WriteLine(“Number of Stock Options: {0}“, StockOptions); } Now that each subclass can interpret what these virtual methods means to itself, each object instance behaves as a more independent entity: static void Main(string[] args) { Console.WriteLine(“***** The Employee Class Hierarchy *****\n“); // A better bonus system! Manager chucky = new Manager(“Chucky“, 50, 92, 100000, “333-23-2322“, 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson(“Fran“, 43, 93, 3000, “932-32-3232“, 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadLine(); } The following output shows a possible test run of your application thus far: CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 231 ***** The Employee Class Hierarchy ***** Name: Chucky ID: 92 Age: 50 Pay: 100300 SSN: 333-23-2322 Number of Stock Options: 9337 Name: Fran ID: 93 Age: 43 Pay: 5000 SSN: 932-32-3232 Number of Sales: 31 Overriding Virtual Members Using the Visual Studio IDE As you might have already noticed, when you are overriding a member, you must recall the type of each and every parameter—not to mention the method name and parameter passing conventions (ref, out, and params). Visual Studio has a very helpful feature that you can make use of when overriding a virtual member. If you type the word “override” within the scope of a class type (then hit the spacebar), IntelliSense will automatically display a list of all the overridable members defined in your parent classes, as you see in Figure 6-5. Figure 6-5. Quickly viewing overridable methods à la Visual Studio CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 232 When you select a member and hit the Enter key, the IDE responds by automatically filling in the method stub on your behalf. Note that you also receive a code statement that calls your parent’s version of the virtual member (you are free to delete this line if it is not required). For example, if you used this technique when overriding the DisplayStats() method, you might find the following autogenerated code: public override void DisplayStats() { base.DisplayStats(); } Sealing Virtual Members Recall that the sealed keyword can be applied to a class type to prevent other types from extending its behavior via inheritance. As you might remember, you sealed PTSalesPerson as you assumed it made no sense for other developers to extend this line of inheritance any further. On a related note, sometimes you might not wish to seal an entire class, but simply want to prevent derived types from overriding particular virtual methods. For example, assume you do not want part- time salespeople to obtain customized bonuses. To prevent the PTSalesPerson class from overriding the virtual GiveBonus() method, you could effectively seal this method in the SalesPerson class as follows: // SalesPerson has sealed the GiveBonus() method! class SalesPerson : Employee { ... public override sealed void GiveBonus(float amount) { ... } } Here, SalesPerson has indeed overridden the virtual GiveBonus() method defined in the Employee class; however, it has explicitly marked it as sealed. Thus, if you attempted to override this method in the PTSalesPerson class, you receive compile-time errors, as shown in the following code: sealed class PTSalesPerson : SalesPerson { public PTSalesPerson(string fullName, int age, int empID, float currPay, string ssn, int numbOfSales) :base (fullName, age, empID, currPay, ssn, numbOfSales) { } // Compiler error! Can't override this method // in the PTSalesPerson class, as it was sealed. public override void GiveBonus(float amount) { } } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 233 Understanding Abstract Classes Currently, the Employee base class has been designed to supply various data members for its descendents, as well as supply two virtual methods (GiveBonus() and DisplayStats()) that may be overridden by a given descendent. While this is all well and good, there is a rather odd byproduct of the current design; you can directly create instances of the Employee base class: // What exactly does this mean? Employee X = new Employee(); In this example, the only real purpose of the Employee base class is to define common members for all subclasses. In all likelihood, you did not intend anyone to create a direct instance of this class, reason being that the Employee type itself is too general of a concept. For example, if I were to walk up to you and say, “I’m an employee!” I would bet your very first question to me would be, “What kind of employee are you?” Are you a consultant, trainer, admin assistant, copyeditor, or White House aide? Given that many base classes tend to be rather nebulous entities, a far better design for this example is to prevent the ability to directly create a new Employee object in code. In C#, you can enforce this programmatically by using the abstract keyword in the class definition, thus creating an abstract base class: // Update the Employee class as abstract // to prevent direct instantiation. abstract partial class Employee { ... } With this, if you now attempt to create an instance of the Employee class, you are issued a compile- time error: // Error! Cannot create an instance of an abstract class! Employee X = new Employee(); At first glance, it might seem very strange to define a class that you cannot directly create. Recall however that base classes (abstract or not) are very useful, in that they contain all of the common data and functionality of derived types. Using this form of abstraction, we are able to model that the “idea” of an employee is completely valid; it is just not a concrete entity. Also understand that although we cannot directly create an abstract class, it is still assembled in memory when derived classes are created. Thus, it is perfectly fine (and common) for abstract classes to define any number of constructors that are called indirectly when derived classes are allocated. At this point, you have constructed a fairly interesting employee hierarchy. You will add a bit more functionality to this application later in this chapter when examining C# casting rules. Until then, Figure 6-6 illustrates the crux of your current design. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 234 Figure 6-6. The Employee hierarchy  Source Code The Employees project is included under the Chapter 6 subdirectory. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 235 Understanding the Polymorphic Interface When a class has been defined as an abstract base class (via the abstract keyword), it may define any number of abstract members. Abstract members can be used whenever you wish to define a member that does not supply a default implementation, but must be accounted for by each derived class. By doing so, you enforce a polymorphic interface on each descendent, leaving them to contend with the task of providing the details behind your abstract methods. Simply put, an abstract base class’s polymorphic interface simply refers to its set of virtual and abstract methods. This is much more interesting than first meets the eye, as this trait of OOP allows you to build easily extendable and flexible software applications. To illustrate, you will be implementing (and slightly modifying) the hierarchy of shapes briefly examined in Chapter 5 during the overview of the pillars of OOP. To begin, create a new C# Console Application project named Shapes. In Figure 6-7, notice that the Hexagon and Circle types each extend the Shape base class. Like any base class, Shape defines a number of members (a PetName property and Draw() method, in this case) that are common to all descendents. Figure 6-7. The shapes hierarchy Much like the employee hierarchy, you should be able to tell that you don’t want to allow the object user to create an instance of Shape directly, as it is too abstract of a concept. Again, to prevent the direct creation of the Shape type, you could define it as an abstract class. As well, given that you wish the derived types to respond uniquely to the Draw() method, let’s mark it as virtual and define a default implementation: CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 236 // The abstract base class of the hierarchy. abstract class Shape { public Shape(string name = “NoName“) { PetName = name; } public string PetName { get; set; } // A single virtual method. public virtual void Draw() { Console.WriteLine(“Inside Shape.Draw()“); } } Notice that the virtual Draw() method provides a default implementation that simply prints out a message that informs you that you are calling the Draw() method within the Shape base class. Now recall that when a method is marked with the virtual keyword, the method provides a default implementation that all derived types automatically inherit. If a child class so chooses, it may override the method but does not have to. Given this, consider the following implementation of the Circle and Hexagon types: // Circle DOES NOT override Draw(). class Circle : Shape { public Circle() {} public Circle(string name) : base(name){} } // Hexagon DOES override Draw(). class Hexagon : Shape { public Hexagon() {} public Hexagon(string name) : base(name){} public override void Draw() { Console.WriteLine(“Drawing {0} the Hexagon“, PetName); } } The usefulness of abstract methods becomes crystal clear when you once again remember that subclasses are never required to override virtual methods (as in the case of Circle). Therefore, if you create an instance of the Hexagon and Circle types, you’d find that the Hexagon understands how to “draw” itself correctly or at least print out an appropriate message to the console. The Circle, however, is more than a bit confused: static void Main(string[] args) { Console.WriteLine(“***** Fun with Polymorphism *****\n“); Hexagon hex = new Hexagon(“Beth“); hex.Draw(); CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 237 Circle cir = new Circle(“Cindy“); // Calls base class implementation! cir.Draw(); Console.ReadLine(); } Now consider the following output of the previous Main() method: ***** Fun with Polymorphism ***** Drawing Beth the Hexagon Inside Shape.Draw() Clearly, this is not a very intelligent design for the current hierarchy. To force each child class to override the Draw() method, you can define Draw() as an abstract method of the Shape class, which by definition means you provide no default implementation whatsoever. To mark a method as abstract in C#, you use the abstract keyword. Notice that abstract members do not provide any implementation whatsoever: abstract class Shape { // Force all child classes to define how to be rendered. public abstract void Draw(); ... }  Note Abstract methods can only be defined in abstract classes. If you attempt to do otherwise, you will be issued a compiler error. Methods marked with abstract are pure protocol. They simply define the name, return type (if any), and parameter set (if required). Here, the abstract Shape class informs the derived types “I have a method named Draw() that takes no arguments and returns nothing. If you derive from me, you figure out the details.” Given this, you are now obligated to override the Draw() method in the Circle class. If you do not, Circle is also assumed to be a noncreatable abstract type that must be adorned with the abstract keyword (which is obviously not very useful in this example). Here is the code update: // If we did not implement the abstract Draw() method, Circle would also be // considered abstract, and would have to be marked abstract! class Circle : Shape { public Circle() {} public Circle(string name) : base(name) {} public override void Draw() { Console.WriteLine(“Drawing {0} the Circle“, PetName); CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 238 } } The short answer is that you can now make the assumption that anything deriving from Shape does indeed have a unique version of the Draw() method. To illustrate the full story of polymorphism, consider the following code: static void Main(string[] args) { Console.WriteLine(“***** Fun with Polymorphism *****\n“); // Make an array of Shape-compatible objects. Shape[] myShapes = {new Hexagon(), new Circle(), new Hexagon(“Mick“), new Circle(“Beth“), new Hexagon(“Linda“)}; // Loop over each item and interact with the // polymorphic interface. foreach (Shape s in myShapes) { s.Draw(); } Console.ReadLine(); } Here is the output from the modified Main() method: ***** Fun with Polymorphism ***** Drawing NoName the Hexagon Drawing NoName the Circle Drawing Mick the Hexagon Drawing Beth the Circle Drawing Linda the Hexagon This Main() method illustrates polymorphism at its finest. Although it is not possible to directly create an instance of an abstract base class (the Shape), you are able to freely store references to any subclass with an abstract base variable. Therefore, when you are creating an array of Shapes, the array can hold any object deriving from the Shape base class (if you attempt to place Shape-incompatible objects into the array, you receive a compiler error). Given that all items in the myShapes array do indeed derive from Shape, you know they all support the same “polymorphic interface” (or said more plainly, they all have a Draw() method). As you iterate over the array of Shape references, it is at runtime that the underlying type is determined. At this point, the correct version of the Draw() method is invoked in memory. This technique also makes it very simple to safely extend the current hierarchy. For example, assume you derived more classes from the abstract Shape base class (Triangle, Square, etc.). Due to the polymorphic interface, the code within your foreach loop would not have to change in the slightest, as the compiler enforces that only Shape-compatible types are placed within the myShapes array. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 239 Understanding Member Shadowing C# provides a facility that is the logical opposite of method overriding, termed shadowing. Formally speaking, if a derived class defines a member that is identical to a member defined in a base class, the derived class has shadowed the parent’s version. In the real world, the possibility of this occurring is the greatest when you are subclassing from a class you (or your team) did not create yourselves (for example, if you purchase a third-party .NET software package). For the sake of illustration, assume you receive a class named ThreeDCircle from a coworker (or classmate) that defines a subroutine named Draw() taking no arguments: class ThreeDCircle { public void Draw() { Console.WriteLine(“Drawing a 3D Circle“); } } You figure that a ThreeDCircle “is-a” Circle, so you derive from your existing Circle type: class ThreeDCircle : Circle { public void Draw() { Console.WriteLine(“Drawing a 3D Circle“); } } After you recompile, you find the following warning: 'Shapes.ThreeDCircle.Draw()' hides inherited member 'Shapes.Circle.Draw()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. The problem is that you have a derived class (ThreeDCircle) that contains a method that is identical to an inherited method. To address this issue, you have two options. You could simply update the parent’s version of Draw() using the override keyword (as suggested by the compiler). With this approach, the ThreeDCircle type is able to extend the parent’s default behavior as required. However, if you don’t have access to the code defining the base class (again, as would be the case in many third- party libraries), you would be unable to modify the Draw() method as a virtual member, as you don’t have access to the code file! As an alternative, you can include the new keyword to the offending Draw() member of the derived type (ThreeDCircle, in this example). Doing so explicitly states that the derived type’s implementation is intentionally designed to effectively ignore the parent’s version (again, in the real world, this can be helpful if external .NET software somehow conflicts with your current software). // This class extends Circle and hides the inherited Draw() method. class ThreeDCircle : Circle { // Hide any Draw() implementation above me. public new void Draw() CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 240 { Console.WriteLine(“Drawing a 3D Circle“); } } You can also apply the new keyword to any member type inherited from a base class (field, constant, static member, or property). As a further example, assume that ThreeDCircle wants to hide the inherited PetName property: class ThreeDCircle : Circle { // Hide the PetName property above me. public new string PetName { get; set; } // Hide any Draw() implementation above me. public new void Draw() { Console.WriteLine(“Drawing a 3D Circle“); } } Finally, be aware that it is still possible to trigger the base class implementation of a shadowed member using an explicit cast, as described in the next section. For example, the following code shows: static void Main(string[] args) { ... // This calls the Draw() method of the ThreeDCircle. ThreeDCircle o = new ThreeDCircle(); o.Draw(); // This calls the Draw() method of the parent! ((Circle)o).Draw(); Console.ReadLine(); }  Source Code The Shapes project can be found under the Chapter 6 subdirectory. Understanding Base Class/Derived Class Casting Rules Now that you can build a family of related class types, you need to learn the rules of class casting operations. To do so, let’s return to the Employees hierarchy created earlier in this chapter. Under the .NET platform, the ultimate base class in the system is System.Object. Therefore, everything “is-an” Object and can be treated as such. Given this fact, it is legal to store an instance of any type within an object variable: static void CastingExamples() { // A Manager “is-a“ System.Object, so we can CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 241 // store a Manager reference in an object variable just fine. object frank = new Manager(“Frank Zappa“, 9, 3000, 40000, “111-11-1111“, 5); } In the Employees example, Managers, SalesPerson, and PTSalesPerson types all extend Employee, so you can store any of these objects in a valid base class reference. Therefore, the following statements are also legal: static void CastingExamples() { // A Manager “is-a“ System.Object, so we can // store a Manager reference in an object variable just fine. object frank = new Manager(“Frank Zappa“, 9, 3000, 40000, “111-11-1111“, 5); // A Manager “is-an“ Employee too. Employee moonUnit = new Manager(“MoonUnit Zappa“, 2, 3001, 20000, “101-11-1321“, 1); // A PTSalesPerson “is-a“ SalesPerson. SalesPerson jill = new PTSalesPerson(“Jill“, 834, 3002, 100000, “111-12-1119“, 90); } The first law of casting between class types is that when two classes are related by an “is-a” relationship, it is always safe to store a derived object within a base class reference. Formally, this is called an implicit cast, as “it just works” given the laws of inheritance. This leads to some powerful programming constructs. For example, assume you have defined a new method within your current Program class: static void GivePromotion(Employee emp) { // Increase pay... // Give new parking space in company garage... Console.WriteLine(“{0} was promoted!“, emp.Name); } Because this method takes a single parameter of type Employee, you can effectively pass any descendent from the Employee class into this method directly, given the “is-a” relationship: static void CastingExamples() { // A Manager “is-a“ System.Object, so we can // store a Manager reference in an object variable just fine. object frank = new Manager(“Frank Zappa“, 9, 3000, 40000, “111-11-1111“, 5); // A Manager “is-an“ Employee too. Employee moonUnit = new Manager(“MoonUnit Zappa“, 2, 3001, 20000, “101-11-1321“, 1); GivePromotion(moonUnit); // A PTSalesPerson “is-a“ SalesPerson. SalesPerson jill = new PTSalesPerson(“Jill“, 834, 3002, 100000, “111-12-1119“, 90); GivePromotion(jill); } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 242 The previous code compiles given the implicit cast from the base class type (Employee) to the derived type. However, what if you also wanted to fire Frank Zappa (currently stored in a general System.Object reference)? If you pass the frank object directly into this method, you will find a compiler error as follows: // Error! object frank = new Manager(“Frank Zappa“, 9, 3000, 40000, “111-11-1111“, 5); GivePromotion(frank); The problem is that you are attempting to pass in a variable which IS-NOT-A Employee, but a more general System.Object. Given the fact that object is higher up the inheritance chain than Employee, the compiler will not allow for an implicit cast, in an effort to keep your code as type-safe as possible. Even though you can figure out that the object reference is pointing to an Employee-compatible class in memory, the compiler cannot, as that will not be known until runtime. You can satisfy the compiler by performing an explicit cast. This is the second law of casting: you can, in such cases, explicitly downcast using the C# casting operator. The basic template to follow when performing an explicit cast looks something like the following: (ClassIWantToCastTo)referenceIHave Thus, to pass the object variable into the GivePromotion() method, you must author the following code: // OK! GivePromotion((Manager)frank); The C# as Keyword Be very aware that explicit casting is evaluated at runtime, not compile time. Therefore, if you were to author the following C# code: // Ack! You can't cast frank to a Hexagon, but this compiles fine! Hexagon hex = (Hexagon)frank; you would compile without error, but would receive a runtime error, or more formally a runtime exception. Chapter 7 will examine the full details of structured exception handling; however, it is worth pointing out, for the time being, that when you are performing an explicit cast, you can trap the possibility of an invalid cast using the try and catch keywords (again, see Chapter 7 for full details): // Catch a possible invalid cast. try { Hexagon hex = (Hexagon)frank; } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } While this is a fine example of defensive programming, C# provides the as keyword to quickly determine at runtime whether a given type is compatible with another. When you use the as keyword, you are able to determine compatibility by checking against a null return value. Consider the following: CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 243 // Use “as“ to test compatability. Hexagon hex2 = frank as Hexagon; if (hex2 == null) Console.WriteLine(“Sorry, frank is not a Hexagon...“); The C# is Keyword Given that the GivePromotion() method has been designed to take any possible type derived from Employee, one question on your mind may be how this method can determine which derived type was sent into the method. On a related note, given that the incoming parameter is of type Employee, how can you gain access to the specialized members of the SalesPerson and Manager types? In addition to the as keyword, the C# language provides the is keyword to determine whether two items are compatible. Unlike the as keyword, however, the is keyword returns false, rather than a null reference, if the types are incompatible. Consider the following implementation of the GivePromotion() method: static void GivePromotion(Employee emp) { Console.WriteLine(“{0} was promoted!“, emp.Name); if (emp is SalesPerson) { Console.WriteLine(“{0} made {1} sale(s)!“, emp.Name, ((SalesPerson)emp).SalesNumber); Console.WriteLine(); } if (emp is Manager) { Console.WriteLine(“{0} had {1} stock options...“, emp.Name, ((Manager)emp).StockOptions); Console.WriteLine(); } } Here, you are performing a runtime check to determine what the incoming base class reference is actually pointing to in memory. After you determine whether you received a SalesPerson or Manager type, you are able to perform an explicit cast to gain access to the specialized members of the class. Also notice that you are not required to wrap your casting operations within a try/catch construct, as you know that the cast is safe if you enter either if scope, given your conditional check. The Master Parent Class: System.Object To wrap up this chapter, I’d like to examine the details of the master parent class in the .NET platform: Object. As you were reading the previous section, you might have noticed that the base classes in your hierarchies (Car, Shape, Employee) never explicitly specify their parent classes: // Who is the parent of Car? class Car {...} CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 244 In the .NET universe, every type ultimately derives from a base class named System.Object (which can be represented by the C# object keyword [lowercase “o”]). The Object class defines a set of common members for every type in the framework. In fact, when you do build a class that does not explicitly define its parent, the compiler automatically derives your type from Object. If you want to be very clear in your intentions, you are free to define classes that derive from Object as follows: // Here we are explicitly deriving from System.Object. class Car : object {...} Like any class, System.Object defines a set of members. In the following formal C# definition, note that some of these items are declared virtual, which specifies that a given member may be overridden by a subclass, while others are marked with static (and are therefore called at the class level): public class Object { // Virtual members. public virtual bool Equals(object obj); protected virtual void Finalize(); public virtual int GetHashCode(); public virtual string ToString(); // Instance-level, nonvirtual members. public Type GetType(); protected object MemberwiseClone(); // Static members. public static bool Equals(object objA, object objB); public static bool ReferenceEquals(object objA, object objB); } Table 6-1 offers a rundown of the functionality provided by some of the methods you’re most likely to make use of. Table 6-1. Core Members of System.Object Instance Method of Object Class Meaning in Life Equals() By default, this method returns true only if the items being compared refer to the exact same item in memory. Thus, Equals() is used to compare object references, not the state of the object. Typically, this method is overridden to return true only if the objects being compared have the same internal state values (that is, value-based semantics). Be aware that if you override Equals(), you should also override GetHashCode(), as these methods are used internally by Hashtable types to retrieve subobjects from the container. Also recall from Chapter 4, that the ValueType class overrides this method for all structures, so they work with value-based comparisons. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 245 Finalize() For the time being, you can understand this method (when overridden) is called to free any allocated resources before the object is destroyed. I talk more about the CLR garbage collection services in Chapter 9. GetHashCode() This method returns an int that identifies a specific object instance. ToString() This method returns a string representation of this object, using the . format (termed the fully qualified name). This method will often be overridden by a subclass to return a tokenized string of name/value pairs that represent the object’s internal state, rather than its fully qualified name. GetType() This method returns a Type object that fully describes the object you are currently referencing. In short, this is a Runtime Type Identification (RTTI) method available to all objects (discussed in greater detail in Chapter 15). MemberwiseClone() This method exists to return a member-by-member copy of the current object, which is often used when cloning an object (see Chapter 8). To illustrate some of the default behavior provided by the Object base class, create a final C# Console Application named ObjectOverrides. Insert a new C# class type that contains the following empty class definition for a type named Person: // Remember! Person extends Object. class Person {} Now, update your Main() method to interact with the inherited members of System.Object as follows: class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with System.Object *****\n“); Person p1 = new Person(); // Use inherited members of System.Object. Console.WriteLine(“ToString: {0}“, p1.ToString()); Console.WriteLine(“Hash code: {0}“, p1.GetHashCode()); Console.WriteLine(“Type: {0}“, p1.GetType()); // Make some other references to p1. Person p2 = p1; object o = p2; CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 246 // Are the references pointing to the same object in memory? if (o.Equals(p1) && p2.Equals(o)) { Console.WriteLine(“Same instance!“); } Console.ReadLine(); } } Here is the output of the current Main() method: ***** Fun with System.Object ***** ToString: ObjectOverrides.Person Hash code: 46104728 Type: ObjectOverrides.Person Same instance! First, notice how the default implementation of ToString() returns the fully qualified name of the current type (ObjectOverrides.Person). As you will see later during the examination of building custom namespaces in Chapter 14, every C# project defines a “root namespace,” which has the same name of the project itself. Here, you created a project named ObjectOverrides; thus, the Person type (as well as the Program class) have both been placed within the ObjectOverrides namespace. The default behavior of Equals() is to test whether two variables are pointing to the same object in memory. Here, you create a new Person variable named p1. At this point, a new Person object is placed on the managed heap. p2 is also of type Person. However, you are not creating a new instance, but rather assigning this variable to reference p1. Therefore, p1 and p2 are both pointing to the same object in memory, as is the variable o (of type object, which was thrown in for good measure). Given that p1, p2, and o all point to the same memory location, the equality test succeeds. Although the canned behavior of System.Object can fit the bill in a number of cases, it is quite common for your custom types to override some of these inherited methods. To illustrate, update the Person class to support some properties representing an individual’s first name, last name, and age, each of which can be set by a custom constructor: // Remember! Person extends Object. class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public Person(string fName, string lName, int personAge) { FirstName = fName; LastName = lName; Age = personAge; } public Person(){} } CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 247 Overriding System.Object.ToString() Many classes (and structures) that you create can benefit from overriding ToString() in order to return a string textual representation of the type’s current state. This can be quite helpful for purposes of debugging (among other reasons). How you choose to construct this string is a matter of personal choice; however, a recommended approach is to separate each name/value pair with semicolons and wrap the entire string within square brackets (many types in the .NET base class libraries follow this approach). Consider the following overridden ToString() for your Person class: public override string ToString() { string myState; myState = string.Format(“[First Name: {0}; Last Name: {1}; Age: {2}]“, FirstName, LastName, Age); return myState; } This implementation of ToString() is quite straightforward, given that the Person class only has three pieces of state data. However, always remember that a proper ToString() override should also account for any data defined up the chain of inheritance. When you override ToString() for a class extending a custom base class, the first order of business is to obtain the ToString() value from your parent using the base keyword. After you have obtained your parent’s string data, you can append the derived class’s custom information. Overriding System.Object.Equals() Let’s also override the behavior of Object.Equals() to work with value-based semantics. Recall that by default, Equals() returns true only if the two objects being compared reference the same object instance in memory. For the Person class, it may be helpful to implement Equals() to return true if the two variables being compared contain the same state values (e.g., first name, last name, and age). First of all, notice that the incoming argument of the Equals() method is a general System. Object. Given this, your first order of business is to ensure the caller has indeed passed in a Person object, and as an extra safeguard, to make sure the incoming parameter is not a null reference. After you have established the caller has passed you an allocated Person, one approach to implement Equals() is to perform a field-by-field comparison against the data of the incoming object to the data of the current object: public override bool Equals(object obj) { if (obj is Person && obj != null) { Person temp; temp = (Person)obj; if (temp.FirstName == this.FirstName && temp.LastName == this.LastName && temp.Age == this.Age) { return true; } else { return false; CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 248 } } return false; } Here, you are examining the values of the incoming object against the values of your internal values (note the use of the this keyword). If the name and age of each are identical, you have two objects with the exact same state data and, therefore, return true. Any other possibility results in returning false. While this approach does indeed work, you can certainly imagine how labor intensive it would be to implement a custom Equals() method for nontrivial types that may contain dozens of data fields. One common shortcut is to leverage your own implementation of ToString(). If a class has a prim-and- proper implementation of ToString() that accounts for all field data up the chain of inheritance, you can simply perform a comparison of the object’s string data: public override bool Equals(object obj) { // No need to cast “obj“ to a Person anymore, // as everything has a ToString() method. return obj.ToString() == this.ToString(); } Notice in this case that we no longer need to check whether the incoming argument is of the correct type (a Person, in our example), as everything in .NET supports a ToString() method. Even better, we no longer need to perform a property-by-property equality check, as we are not simply testing the value returned from ToString(). Overriding System.Object.GetHashCode() When a class overrides the Equals() method, you should also override the default implementation of GetHashCode(). Simply put, a hash code is a numerical value that represents an object as a particular state. For example, if you create two string variables that hold the value Hello, you would obtain the same hash code. However, if one of the string objects were in all lowercase (hello), you would obtain different hash codes. By default, System.Object.GetHashCode() uses your object’s current location in memory to yield the hash value. However, if you are building a custom type that you intend to store in a Hashtable type (within the System.Collections namespace), you should always override this member, as the Hashtable will be internally invoking Equals() and GetHashCode() to retrieve the correct object.  Note To be more specific, the System.Collections.Hashtable class calls GetHashCode() internally to gain a general idea where the object is located, but a subsequent (internal) call to Equals() determines the exact match. Although you are not going to place your Person into a System.Collections.Hashtable, for completion, let’s override GetHashCode(). There are many algorithms that can be used to create a hash code, some fancy, others not so fancy. Most of the time, you are able to generate a hash code value by leveraging the System.String’s GetHashCode() implementation. Given that the String class already has a solid hash code algorithm that is using the character data of the String to compute a hash value, if you can identify a piece of field data on your class that should CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 249 be unique for all instances (such as a Social Security number), simply call GetHashCode() on that point of field data. Thus, if the Person class defined a SSN property, we could author the following code: // Assume we have an SSN property as so. class Person { public string SSN {get; set;} // Return a hash code based on a point of unique string data. public override int GetHashCode() { return SSN.GetHashCode(); } } If you cannot find a single point of unique string data, but you have overridden ToString(), call GetHashCode() on your own string representation: // Return a hash code based on the person's ToString() value. public override int GetHashCode() { return this.ToString().GetHashCode(); } Testing Your Modified Person Class Now that you have overridden the virtual members of Object, update Main() to test your updates. static void Main(string[] args) { Console.WriteLine(“***** Fun with System.Object *****\n“); // NOTE: We want these to be identical to test // the Equals() and GetHashCode() methods. Person p1 = new Person(“Homer“, “Simpson“, 50); Person p2 = new Person(“Homer“, “Simpson“, 50); // Get stringified version of objects. Console.WriteLine(“p1.ToString() = {0}“, p1.ToString()); Console.WriteLine(“p2.ToString() = {0}“, p2.ToString()); // Test overridden Equals(). Console.WriteLine(“p1 = p2?: {0}“, p1.Equals(p2)); // Test hash codes. Console.WriteLine(“Same hash codes?: {0}“, p1.GetHashCode() == p2.GetHashCode()); Console.WriteLine(); // Change age of p2 and test again. p2.Age = 45; Console.WriteLine(“p1.ToString() = {0}“, p1.ToString()); Console.WriteLine(“p2.ToString() = {0}“, p2.ToString()); Console.WriteLine(“p1 = p2?: {0}“, p1.Equals(p2)); CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 250 Console.WriteLine(“Same hash codes?: {0}“, p1.GetHashCode() == p2.GetHashCode()); Console.ReadLine(); } The output can be seen here: ***** Fun with System.Object ***** p1.ToString() = [First Name: Homer; Last Name: Simpson; Age: 50] p2.ToString() = [First Name: Homer; Last Name: Simpson; Age: 50] p1 = p2?: True Same hash codes?: True p1.ToString() = [First Name: Homer; Last Name: Simpson; Age: 50] p2.ToString() = [First Name: Homer; Last Name: Simpson; Age: 45] p1 = p2?: False Same hash codes?: False The Static Members of System.Object In addition to the instance-level members you have just examined, System.Object does define two (very helpful) static members that also test for value-based or reference-based equality. Consider the following code: static void StaticMembersOfObject() { // Static members of System.Object. Person p3 = new Person(“Sally“, “Jones“, 4); Person p4 = new Person(“Sally“, “Jones“, 4); Console.WriteLine(“P3 and P4 have same state: {0}“, object.Equals(p3, p4)); Console.WriteLine(“P3 and P4 are pointing to same object: {0}“, object.ReferenceEquals(p3, p4)); } Here, you are able to simply send in two objects (of any type) and allow the System.Object class to determine the details automatically.  Source Code The ObjectOverrides project is located under the Chapter 6 subdirectory. Summary This chapter explored the role and details of inheritance and polymorphism. Over these pages you were introduced to numerous new keywords and tokens to support each of these techniques. For example, recall that the colon token is used to establish the parent class of a given type. Parent types are able to define any number of virtual and/or abstract members to establish a polymorphic interface. Derived types override such members using the override keyword. CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM 251 In addition to building numerous class hierarchies, this chapter also examined how to explicitly cast between base and derived types, and wrapped up by diving into the details of the cosmic parent class in the .NET base class libraries: System.Object. C H A P T E R 7 253 Understanding Structured Exception Handling In this chapter, you will learn how to handle runtime anomalies in your C# code through the use of structured exception handling (often cryptically abbreviated as SEH). Not only will you examine the C# keywords that allow you to handle such matters (try, catch, throw, finally), you will also come to understand the distinction between application-level and system-level exceptions, as well as the role of the System.Exception base class. This discussion will lead into the topic of building custom exceptions and, finally, to a quick look at the exception-centric debugging tools of Visual Studio. Ode to Errors, Bugs, and Exceptions Despite what our (sometimes inflated) egos may tell us, no programmer is perfect. Writing software is a complex undertaking, and given this complexity, it is quite common for even the best software to ship with various “problems.” Sometimes the problem is caused by “bad code” (such as overflowing the bounds of an array). Other times, a problem is caused by bogus user input that has not been accounted for in the application’s code base (e.g., a phone number input field assigned to the value “Chucky”). Now, regardless of the cause of the problem, the end result is that the application does not work as expected. To help frame the upcoming discussion of structured exception handling, allow me to provide definitions for three commonly used anomaly-centric terms: • Bugs: These are, simply put, errors made by the programmer. For example, suppose you are programming with unmanaged C++. If you fail to delete dynamically allocated memory, resulting in a memory leak, you have a bug. • User errors: User errors, on the other hand, are typically caused by the individual running your application, rather than by those who created it. For example, an end user who enters a malformed string into a text box could very well generate an error if you fail to handle this faulty input in your code base. • Exceptions: Exceptions are typically regarded as runtime anomalies that are difficult, if not impossible, to account for while programming your application. Possible exceptions include attempting to connect to a database that no longer exists, opening a corrupted XML file, or trying to contact a machine that is currently offline. In each of these cases, the programmer (or end user) has little control over these “exceptional” circumstances. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 254 Given these definitions, it should be clear that .NET structured exception handling is a technique for dealing with runtime exceptions. However, even for the bugs and user errors that have escaped your view, the CLR will often generate a corresponding exception that identifies the problem at hand. The .NET base class libraries define numerous exceptions, such as FormatException, IndexOutOfRangeException, FileNotFoundException, ArgumentOutOfRangeException, and so forth. Within the .NET nomenclature, an “exception” accounts for bugs, bogus user input, and runtime errors, even though we programmers may view each of these as a distinct issue. However, before we get too far ahead of ourselves, let’s formalize the role of structured exception handling and check out how it differs from traditional error-handling techniques.  Note To make the code examples used in this book as clean as possible, I will not catch every possible exception that may be thrown by a given method in the base class libraries. In your production-level projects, you should, of course, make liberal use of the techniques presented in this chapter. The Role of .NET Exception Handling Prior to .NET, error handling under the Windows operating system was a confused mishmash of techniques. Many programmers rolled their own error-handling logic within the context of a given application. For example, a development team could define a set of numerical constants that represented known error conditions, and make use of them as method return values. By way of an example, consider the following partial C code: /* A very C-style error trapping mechanism. */ #define E_FILENOTFOUND 1000 int UseFileSystem() { // Assume something happens in this function // that causes the following return value. return E_FILENOTFOUND; } void main() { int retVal = UseFileSystem(); if(retVal == E_FILENOTFOUND) printf(“Cannot find file...“); } This approach is less than ideal, given the fact that the constant E_FILENOTFOUND is little more than a numerical value, and is far from being a helpful agent regarding how to deal with the problem. Ideally, you would like to wrap the error’s name, a descriptive message, and other helpful information about this error condition into a single, well-defined package (which is exactly what happens under structured exception handling). In addition to a developer’s ad hoc techniques, the Windows API defines hundreds of error codes that come by way of #defines, HRESULTs, and far too many variations on the simple Boolean (bool, BOOL, VARIANT_BOOL, and so on). Furthermore, many C++ COM developers made use of a small set of standard CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 255 COM interfaces (e.g., ISupportErrorInfo, IErrorInfo, ICreateErrorInfo) to return meaningful error information to a COM client. The obvious problem with these older techniques is the tremendous lack of symmetry. Each approach is more or less tailored to a given technology, a given language, and perhaps even a given project. To put an end to this madness, the .NET platform provides a standard technique to send and trap runtime errors: structured exception handling (SEH). The beauty of this approach is that developers now have a unified approach to error handling, which is common to all languages targeting the .NET platform. Therefore, the way in which a C# programmer handles errors is syntactically similar to that of a VB programmer, or a C++ programmer using C++/CLI. As an added bonus, the syntax used to throw and catch exceptions across assemblies and machine boundaries is identical. For example, if you use C# to build a Windows Communication Foundation (WCF) service, you can throw a SOAP fault to a remote caller, using the same keywords that allow you to throw an exception between methods in the same application. Another bonus of .NET exceptions is that rather than receiving a cryptic numerical value that simply identifies the problem at hand, exceptions are objects that contain a human-readable description of the problem, as well as a detailed snapshot of the call stack that triggered the exception in the first place. Furthermore, you are able to give the end user help-link information that points the user to a URL that provides details about the error, as well as custom programmer-defined data. The Building Blocks of .NET Exception Handling Programming with structured exception handling involves the use of four interrelated entities: • A class type that represents the details of the exception • A member that throws an instance of the exception class to the caller under the correct circumstances • A block of code on the caller’s side that invokes the exception-prone member • A block of code on the caller’s side that will process (or catch) the exception, should it occur The C# programming language offers four keywords (try, catch, throw, and finally) that allow you to throw and handle exceptions. The object that represents the problem at hand is a class extending System.Exception (or a descendent thereof). Given this fact, let’s check out the role of this exception- centric base class. The System.Exception Base Class All exceptions ultimately derive from the System.Exception base class, which in turn derives from System.Object. Here is the crux of this class (note that some of these members are virtual and may thus be overridden by derived classes): public class Exception : ISerializable, _Exception { // Public constructors public Exception(string message, Exception innerException); public Exception(string message); public Exception(); ... CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 256 // Methods public virtual Exception GetBaseException(); public virtual void GetObjectData(SerializationInfo info, StreamingContext context); // Properties public virtual IDictionary Data { get; } public virtual string HelpLink { get; set; } public Exception InnerException { get; } public virtual string Message { get; } public virtual string Source { get; set; } public virtual string StackTrace { get; } public MethodBase TargetSite { get; } ... } As you can see, many of the properties defined by System.Exception are read-only in nature. This is due to the fact that derived types will typically supply default values for each property. For example, the default message of the IndexOutOfRangeException type is “Index was outside the bounds of the array.”  Note The Exception class implements two .NET interfaces. Although we have yet to examine interfaces (see Chapter 8), just understand that the _Exception interface allows a .NET exception to be processed by an unmanaged code base (such as a COM application), while the ISerializable interface allows an exception object to be persisted across boundaries (such as a machine boundary). Table 7-1 describes the most important members of System.Exception. Table 7-1. Core Members of the System.Exception Type System.Exception Property Meaning in Life Data This read-only property retrieves a collection of key/value pairs (represented by an object implementing IDictionary) that provide additional, programmer-defined information about the exception. By default, this collection is empty. HelpLink This property gets or sets a URL to a help file or web site describing the error in full detail. InnerException This read-only property can be used to obtain information about the previous exception(s) that caused the current exception to occur. The previous exception(s) are recorded by passing them into the constructor of the most current exception. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 257 Message This read-only property returns the textual description of a given error. The error message itself is set as a constructor parameter. Source This property gets or sets the name of the assembly, or the object, that threw the current exception. StackTrace This read-only property contains a string that identifies the sequence of calls that triggered the exception. As you might guess, this property is very useful during debugging or if you wish to dump the error to an external error log. TargetSite This read-only property returns a MethodBase object, which describes numerous details about the method that threw the exception (invoking ToString() will identify the method by name). The Simplest Possible Example To illustrate the usefulness of structured exception handling, we need to create a class that will throw an exception under the correct (or one might say exceptional) circumstances. Assume we have created a new C# Console Application project (named SimpleException) that defines two class types (Car and Radio) associated by the “has-a” relationship. The Radio type defines a single method that turns the radio’s power on or off: class Radio { public void TurnOn(bool on) { if(on) Console.WriteLine(“Jamming...“); else Console.WriteLine(“Quiet time...“); } } In addition to leveraging the Radio class via containment/delegation, the Car class (shown next) is defined in such a way that if the user accelerates a Car object beyond a predefined maximum speed (specified using a constant member variable named MaxSpeed), its engine explodes, rendering the Car unusable (captured by a private bool member variable named carIsDead). Beyond these points, the Car type has a few properties to represent the current speed and a user supplied “pet name,” as well as various constructors to set the state of a new Car object. Here is the complete definition (with code annotations): class Car { // Constant for maximum speed. public const int MaxSpeed = 100; // Car properties. public int CurrentSpeed {get; set;} public string PetName {get; set;} CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 258 // Is the car still operational? private bool carIsDead; // A car has-a radio. private Radio theMusicBox = new Radio(); // Constructors. public Car() {} public Car(string name, int speed) { CurrentSpeed = speed; PetName = name; } public void CrankTunes(bool state) { // Delegate request to inner object. theMusicBox.TurnOn(state); } // See if Car has overheated. public void Accelerate(int delta) { if (carIsDead) Console.WriteLine(“{0} is out of order...“, PetName); else { CurrentSpeed += delta; if (CurrentSpeed > MaxSpeed) { Console.WriteLine(“{0} has overheated!“, PetName); CurrentSpeed = 0; carIsDead = true; } else Console.WriteLine(“=> CurrentSpeed = {0}“, CurrentSpeed); } } } Now, if we implement a Main() method that forces a Car object to exceed the predefined maximum speed (set to 100, in the Car class) as shown here: static void Main(string[] args) { Console.WriteLine(“***** Simple Exception Example *****“); Console.WriteLine(“=> Creating a car and stepping on it!“); Car myCar = new Car(“Zippy“, 20); myCar.CrankTunes(true); for (int i = 0; i < 10; i++) myCar.Accelerate(10); CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 259 Console.ReadLine(); } we would see the following output: ***** Simple Exception Example ***** => Creating a car and stepping on it! Jamming... => CurrentSpeed = 30 => CurrentSpeed = 40 => CurrentSpeed = 50 => CurrentSpeed = 60 => CurrentSpeed = 70 => CurrentSpeed = 80 => CurrentSpeed = 90 => CurrentSpeed = 100 Zippy has overheated! Zippy is out of order... Throwing a General Exception Now that we have a functional Car class, I’ll demonstrate the simplest way to throw an exception. The current implementation of Accelerate() simply displays an error message if the caller attempts to speed up the Car beyond its upper limit. To retrofit this method to throw an exception if the user attempts to speed up the automobile after it has met its maker, you want to create and configure a new instance of the System.Exception class, setting the value of the read-only Message property via the class constructor. When you want to send the exception object back to the caller, use the C# throw keyword. Here is the relevant code update to the Accelerate() method: // This time, throw an exception if the user speeds up beyond MaxSpeed. public void Accelerate(int delta) { if (carIsDead) Console.WriteLine(“{0} is out of order...“, PetName); else { CurrentSpeed += delta; if (CurrentSpeed >= MaxSpeed) { carIsDead = true; CurrentSpeed = 0; // Use the “throw“ keyword to raise an exception. throw new Exception(string.Format(“{0} has overheated!“, PetName)); } else Console.WriteLine(“=> CurrentSpeed = {0}“, CurrentSpeed); } } CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 260 Before examining how a caller would catch this exception, let’s look at a few points of interest. First of all, when you are throwing an exception, it is always up to you to decide exactly what constitutes the error in question, and when an exception should be thrown. Here, you are making the assumption that if the program attempts to increase the speed of a Car object that has expired, a System.Exception object should be thrown to indicate the Accelerate() method cannot continue (which may or may not be a valid assumption; this will be a judgment call on your part based on the application you are creating). Alternatively, you could implement Accelerate() to recover automatically without needing to throw an exception in the first place. By and large, exceptions should be thrown only when a more terminal condition has been met (for example, not finding a necessary file, failing to connect to a database, and the like). Deciding exactly what justifies throwing an exception is a design issue you must always contend with. For our current purposes, assume that asking a doomed automobile to increase its speed is cause to throw an exception. Catching Exceptions Because the Accelerate() method now throws an exception, the caller needs to be ready to handle the exception, should it occur. When you are invoking a method that may throw an exception, you make use of a try/catch block. After you have caught the exception object, you are able to invoke the members of the exception object to extract the details of the problem. What you do with this data is largely up to you. You might wish to log this information to a report file, write the data to the Windows event log, e-mail a system administrator, or display the problem to the end user. Here, you will simply dump the contents to the console window: // Handle the thrown exception. static void Main(string[] args) { Console.WriteLine(“***** Simple Exception Example *****“); Console.WriteLine(“=> Creating a car and stepping on it!“); Car myCar = new Car(“Zippy“, 20); myCar.CrankTunes(true); // Speed up past the car's max speed to // trigger the exception. try { for(int i = 0; i < 10; i++) myCar. Accelerate(10); } catch(Exception e) { Console.WriteLine(“\n*** Error! ***“); Console.WriteLine(“Method: {0}“, e.TargetSite); Console.WriteLine(“Message: {0}“, e.Message); Console.WriteLine(“Source: {0}“, e.Source); } // The error has been handled, processing continues with the next statement. Console.WriteLine(“\n***** Out of exception logic *****“); Console.ReadLine(); } CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 261 In essence, a try block is a section of statements that may throw an exception during execution. If an exception is detected, the flow of program execution is sent to the appropriate catch block. On the other hand, if the code within a try block does not trigger an exception, the catch block is skipped entirely, and all is right with the world. The following output shows a test run of this program. ***** Simple Exception Example ***** => Creating a car and stepping on it! Jamming... => CurrentSpeed = 30 => CurrentSpeed = 40 => CurrentSpeed = 50 => CurrentSpeed = 60 => CurrentSpeed = 70 => CurrentSpeed = 80 => CurrentSpeed = 90 *** Error! *** Method: Void Accelerate(Int32) Message: Zippy has overheated! Source: SimpleException ***** Out of exception logic ***** As you can see, after an exception has been handled, the application is free to continue on from the point after the catch block. In some circumstances, a given exception could be critical enough to warrant the termination of the application. However, in a good number of cases, the logic within the exception handler will ensure the application can continue on its merry way (although it could be slightly less functional, such as not being able to connect to a remote data source). Configuring the State of an Exception Currently, the System.Exception object configured within the Accelerate() method simply establishes a value exposed to the Message property (via a constructor parameter). As shown previously in Table 7-1, however, the Exception class also supplies a number of additional members (TargetSite, StackTrace, HelpLink, and Data) that can be useful in further qualifying the nature of the problem. To spruce up our current example, let’s examine further details of these members on a case-by-case basis. The TargetSite Property The System.Exception.TargetSite property allows you to determine various details about the method that threw a given exception. As shown in the previous Main() method, printing the value of TargetSite will display the return type, name, and parameter types of the method that threw the exception. However, TargetSite does not return just a vanilla-flavored string, but rather a strongly typed System.Reflection.MethodBase object. This type can be used to gather numerous details regarding the offending method, as well as the class that defines the offending method. To illustrate, assume the previous catch logic has been updated as follows: CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 262 static void Main(string[] args) { ... // TargetSite actually returns a MethodBase object. catch(Exception e) { Console.WriteLine(“\n*** Error! ***“); Console.WriteLine(“Member name: {0}“, e.TargetSite); Console.WriteLine(“Class defining member: {0}“, e.TargetSite.DeclaringType); Console.WriteLine(“Member type: {0}“, e.TargetSite.MemberType); Console.WriteLine(“Message: {0}“, e.Message); Console.WriteLine(“Source: {0}“, e.Source); } Console.WriteLine(“\n***** Out of exception logic *****“); Console.ReadLine(); } This time, you make use of the MethodBase.DeclaringType property to determine the fully qualified name of the class that threw the error (SimpleException.Car, in this case) as well as the MemberType property of the MethodBase object to identify the type of member (such as a property vs. a method) where this exception originated. In this case, the catch logic would display the following: *** Error! *** Member name: Void Accelerate(Int32) Class defining member: SimpleException.Car Member type: Method Message: Zippy has overheated! Source: SimpleException The StackTrace Property The System.Exception.StackTrace property allows you to identify the series of calls that resulted in the exception. Be aware that you never set the value of StackTrace, as it is established automatically at the time the exception is created. To illustrate, assume you have once again updated your catch logic: catch(Exception e) { ... Console.WriteLine(“Stack: {0}“, e.StackTrace); } If you were to run the program, you would find the following stack trace is printed to the console (your line numbers and file paths may differ, of course): Stack: at SimpleException.Car.Accelerate(Int32 delta) in c:\MyApps\SimpleException\car.cs:line 65 at SimpleException.Program.Main() in c:\MyApps\SimpleException\Program.cs:line 21 CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 263 The string returned from StackTrace documents the sequence of calls that resulted in the throwing of this exception. Notice how the bottommost line number of this string identifies the first call in the sequence, while the topmost line number identifies the exact location of the offending member. Clearly, this information can be quite helpful during the debugging or logging of a given application, as you are able to “follow the flow” of the error’s origin. The HelpLink Property While the TargetSite and StackTrace properties allow programmers to gain an understanding of a given exception, this information is of little use to the end user. As you have already seen, the System.Exception.Message property can be used to obtain human-readable information that can be displayed to the current user. In addition, the HelpLink property can be set to point the user to a specific URL or standard Windows help file that contains more detailed information. By default, the value managed by the HelpLink property is an empty string. If you want to fill this property with a more interesting value, you need to do so before throwing the System.Exception object. Here are the relevant updates to the Car.Accelerate() method: public void Accelerate(int delta) { if (carIsDead) Console.WriteLine(“{0} is out of order...“, PetName); else { CurrentSpeed += delta; if (CurrentSpeed >= MaxSpeed) { carIsDead = true; CurrentSpeed = 0; // We need to call the HelpLink property, thus we need to // create a local variable before throwing the Exception object. Exception ex = new Exception(string.Format(“{0} has overheated!“, PetName)); ex.HelpLink = “http://www.CarsRUs.com“; throw ex; } else Console.WriteLine(“=> CurrentSpeed = {0}“, CurrentSpeed); } } The catch logic could now be updated to print out this help link information as follows: catch(Exception e) { ... Console.WriteLine(“Help Link: {0}“, e.HelpLink); } CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 264 The Data Property The Data property of System.Exception allows you to fill an exception object with relevant auxiliary information (such as a time stamp). The Data property returns an object implementing an interface named IDictionary, defined in the System.Collections namespace. Chapter 8 examines the role of interface-based programming, as well as the System.Collections namespace. For the time being, just understand that dictionary collections allow you to create a set of values that are retrieved using a specific key. Observe the next update to the Car.Accelerate() method: public void Accelerate(int delta) { if (carIsDead) Console.WriteLine(“{0} is out of order...“, PetName); else { CurrentSpeed += delta; if (CurrentSpeed >= MaxSpeed) { carIsDead = true; CurrentSpeed = 0; // We need to call the HelpLink property, thus we need // to create a local variable before throwing the Exception object. Exception ex = new Exception(string.Format(“{0} has overheated!“, PetName)); ex.HelpLink = “http://www.CarsRUs.com“; // Stuff in custom data regarding the error. ex.Data.Add(“TimeStamp“, string.Format(“The car exploded at {0}“, DateTime.Now)); ex.Data.Add(“Cause“, “You have a lead foot.“); throw ex; } else Console.WriteLine(“=> CurrentSpeed = {0}“, CurrentSpeed); } } To successfully enumerate over the key/value pairs, you must first make sure to specify a using directive for the System.Collections namespace, since you will use a DictionaryEntry type in the file containing the class implementing your Main() method: using System.Collections; Next, you need to update the catch logic to test that the value returned from the Data property is not null (the default value). After that, you make use of the Key and Value properties of the DictionaryEntry type to print the custom data to the console: catch (Exception e) { ... // By default, the data field is empty, so check for null. Console.WriteLine(“\n-> Custom Data:“); CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 265 if (e.Data != null) { foreach (DictionaryEntry de in e.Data) Console.WriteLine(“-> {0}: {1}“, de.Key, de.Value); } } With this, here’s the final output you’d see: ***** Simple Exception Example ***** => Creating a car and stepping on it! Jamming... => CurrentSpeed = 30 => CurrentSpeed = 40 => CurrentSpeed = 50 => CurrentSpeed = 60 => CurrentSpeed = 70 => CurrentSpeed = 80 => CurrentSpeed = 90 *** Error! *** Member name: Void Accelerate(Int32) Class defining member: SimpleException.Car Member type: Method Message: Zippy has overheated! Source: SimpleException Stack: at SimpleException.Car.Accelerate(Int32 delta) at SimpleException.Program.Main(String[] args) Help Link: http://www.CarsRUs.com -> Custom Data: -> TimeStamp: The car exploded at 5/12/2012 9:02:12 PM -> Cause: You have a lead foot. ***** Out of exception logic ***** The Data property is very useful in that it allows us to pack in custom information regarding the error at hand, without requiring the building of a brand-new class type to extend the Exception base class. As helpful as the Data property may be, however, it is still common for .NET developers to build strongly typed exception classes, which handle custom data using strongly typed properties. This approach allows the caller to catch a specific exception-derived type, rather than having to dig into a data collection to obtain additional details. To understand how to do this, we need to examine the distinction between system-level and application-level exceptions.  Source Code The SimpleException project is included under the Chapter 7 subdirectory. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 266 System-Level Exceptions (System.SystemException) The .NET base class libraries define many classes that ultimately derive from System.Exception. For example, the System namespace defines core exception objects such as ArgumentOutOfRangeException, IndexOutOfRangeException, StackOverflowException, and so forth. Other namespaces define exceptions that reflect the behavior of that namespace. For example, System.Drawing.Printing defines printing exceptions, System.IO defines input/output-based exceptions, System.Data defines database-centric exceptions, and so forth. Exceptions that are thrown by the .NET platform are (appropriately) called system exceptions. These exceptions are generally regarded as nonrecoverable, fatal errors. System exceptions derive directly from a base class named System.SystemException, which in turn derives from System.Exception (which derives from System.Object): public class SystemException : Exception { // Various constructors. } Given that the System.SystemException type does not add any additional functionality beyond a set of custom constructors, you might wonder why SystemException exists in the first place. Simply put, when an exception type derives from System.SystemException, you are able to determine that the .NET runtime is the entity that has thrown the exception, rather than the code base of the executing application. You can verify this quite simply using the is keyword: // True! NullReferenceException is-a SystemException. NullReferenceException nullRefEx = new NullReferenceException(); Console.WriteLine(“NullReferenceException is-a SystemException? : {0}“, nullRefEx is SystemException); Application-Level Exceptions (System.ApplicationException) Given that all .NET exceptions are class types, you are free to create your own application-specific exceptions. However, due to the fact that the System.SystemException base class represents exceptions thrown from the CLR, you might naturally assume that you should derive your custom exceptions from the System.Exception type. You could do this, but you could instead derive from the System.ApplicationException class: public class ApplicationException : Exception { // Various constructors. } Like SystemException, ApplicationException does not define any additional members beyond a set of constructors. Functionally, the only purpose of System.ApplicationException is to identify the source of the error. When you handle an exception deriving from System.ApplicationException, you can assume the exception was raised by the code base of the executing application, rather than by the .NET base class libraries or .NET runtime engine. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 267  Note In practice, few .NET developers build custom exceptions that extend ApplicationException. Rather, it is more common to simply subclass System.Exception; however, either approach is technically valid. Building Custom Exceptions, Take One While you can always throw instances of System.Exception to signal a runtime error (as shown in the first example), it is sometimes advantageous to build a strongly typed exception that represents the unique details of your current problem. For example, assume you want to build a custom exception (named CarIsDeadException) to represent the error of speeding up a doomed automobile. The first step is to derive a new class from System.Exception/System.ApplicationException (by convention, all exception classes end with the “Exception” suffix; in fact, this is a .NET best practice).  Note As a rule, all custom exception classes should be defined as public classes (recall, the default access modifier of a non-nested type is internal). The reason is that exceptions are often passed outside of assembly boundaries, and should therefore be accessible to the calling code base. Create a new Console Application project named CustomException, and copy the previous Car.cs and Radio.cs files into your new project using the Project Add Existing Item menu option (for clarity, be sure to change the namespace that defines the Car and Radio types from SimpleException to CustomException). Next, add the following class definition: // This custom exception describes the details of the car-is-dead condition. // (Remember, you can also simply extend Exception.) public class CarIsDeadException : ApplicationException {} As with any class, you are free to include any number of custom members that can be called within the catch block of the calling logic. You are also free to override any virtual members defined by your parent classes. For example, we could implement the CarIsDeadException by overriding the virtual Message property. As well, rather than populating a data dictionary (via the Data property) when throwing our exception, our constructor allows the sender to pass in a time stamp and reason for the error. Finally, the time stamp data and cause of the error can be obtained using strongly typed properties: public class CarIsDeadException : ApplicationException { private string messageDetails = String.Empty; public DateTime ErrorTimeStamp {get; set;} public string CauseOfError {get; set;} public CarIsDeadException(){} public CarIsDeadException(string message, string cause, DateTime time) { CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 268 messageDetails = message; CauseOfError = cause; ErrorTimeStamp = time; } // Override the Exception.Message property. public override string Message { get { return string.Format(“Car Error Message: {0}“, messageDetails); } } } Here, the CarIsDeadException class maintains a private field (messageDetails) that represents data regarding the current exception, which can be set using a custom constructor. Throwing this exception from the Accelerate() method is straightforward. Simply allocate, configure, and throw a CarIsDeadException type rather than a System.Exception (notice that in this case, we no longer need to fill the data collection manually): // Throw the custom CarIsDeadException. public void Accelerate(int delta) { ... CarIsDeadException ex = new CarIsDeadException (string.Format(“{0} has overheated!“, PetName), “You have a lead foot“, DateTime.Now); ex.HelpLink = “http://www.CarsRUs.com“; throw ex; ... } To catch this incoming exception, your catch scope can now be updated to catch a specific CarIsDeadException type (however, given that CarIsDeadException “is-a” System.Exception, it is still permissible to catch a System.Exception as well): static void Main(string[] args) { Console.WriteLine(“***** Fun with Custom Exceptions *****\n“); Car myCar = new Car(“Rusty“, 90); try { // Trip exception. myCar.Accelerate(50); } catch (CarIsDeadException e) { Console.WriteLine(e.Message); Console.WriteLine(e.ErrorTimeStamp); Console.WriteLine(e.CauseOfError); } CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 269 Console.ReadLine(); } So, now that you understand the basic process of building a custom exception, you might wonder when you are required to do so. Typically, you only need to create custom exceptions when the error is tightly bound to the class issuing the error (for example, a custom file-centric class that throws a number of file-related errors, a Car class that throws a number of car-related errors, a data access object that throws errors regarding a particular database table, and so forth). In doing so, you provide the caller with the ability to handle numerous exceptions on a descriptive error-by-error basis. Building Custom Exceptions, Take Two The current CarIsDeadException type has overridden the virtual System.Exception.Message property in order to configure a custom error message, and has supplied two custom properties to account for additional bits of data. In reality, however, you are not required to override the virtual Message property, as you could simply pass the incoming message to the parent’s constructor as follows: public class CarIsDeadException : ApplicationException { public DateTime ErrorTimeStamp { get; set; } public string CauseOfError { get; set; } public CarIsDeadException() { } // Feed message to parent constructor. public CarIsDeadException(string message, string cause, DateTime time) :base(message) { CauseOfError = cause; ErrorTimeStamp = time; } } Notice that this time you have not defined a string variable to represent the message, and have not overridden the Message property. Rather, you are simply passing the parameter to your base class constructor. With this design, a custom exception class is little more than a uniquely named class deriving from System.ApplicationException (with additional properties if appropriate), devoid of any base class overrides. Don’t be surprised if most (if not all) of your custom exception classes follow this simple pattern. Many times, the role of a custom exception is not necessarily to provide additional functionality beyond what is inherited from the base classes, but to supply a strongly named type that clearly identifies the nature of the error, so the client can provide different handler-logic for different types of exceptions. Building Custom Exceptions, Take Three If you want to build a truly prim-and-proper custom exception class, you would want to make sure your type adheres to .NET best practices. Specifically, this requires that your custom exception does the following: • Derives from Exception/ApplicationException • Is marked with the [System.Serializable] attribute CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 270 • Defines a default constructor • Defines a constructor that sets the inherited Message property • Defines a constructor to handle “inner exceptions” • Defines a constructor to handle the serialization of your type Now, based on your current background with .NET, you might have no experience regarding the role of attributes or object serialization, which is just fine. I’ll address these topics later in the text (see Chapter 15 for information on attributes and Chapter 20 for details on serialization services). However, to complete our examination of building custom exceptions, here is the final iteration of CarIsDeadException, which accounts for each of these special constructors (the other custom properties and constructors would be as seen in the example found in “Building Custom Exceptions, Take Two”): [Serializable] public class CarIsDeadException : ApplicationException { public CarIsDeadException() { } public CarIsDeadException(string message) : base( message ) { } public CarIsDeadException(string message, System.Exception inner) : base( message, inner ) { } protected CarIsDeadException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base( info, context ) { } // Any additional custom properties, constructors and data members... } Given that building custom exceptions that adhere to .NET best practices really differ by only their name, you will be happy to know that Visual Studio provides a code snippet template named “Exception” (see Figure 7-1) that will autogenerate a new exception class that adheres to .NET best practices. (Recall from Chapter 2, a code snippet can be activated by typing its name [exception, in this case] and pressing the Tab key twice.) CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 271 Figure 7-1. The Exception code snippet template  Source Code The CustomException project is included under the Chapter 7 subdirectory. Processing Multiple Exceptions In its simplest form, a try block has a single catch block. In reality, though, you often run into situations where the statements within a try block could trigger numerous possible exceptions. Create a new C# Console Application project named ProcessMultipleExceptions, add the Car.cs, Radio.cs, and CarIsDeadException.cs files from the previous CustomException example into the new project (via Project  Add Existing Item), and update your namespace names accordingly. Now, update the Car’s Accelerate() method to also throw a predefined base class library ArgumentOutOfRangeException if you pass an invalid parameter (which we will assume is any value less than zero). Note the constructor of this exception class takes the name of the offending argument as the first string, followed by a message describing the error. // Test for invalid argument before proceeding. public void Accelerate(int delta) { if(delta < 0) throw new ArgumentOutOfRangeException(“delta“, “Speed must be greater than zero!“); ... } The catch logic could now specifically respond to each type of exception: CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 272 static void Main(string[] args) { Console.WriteLine(“***** Handling Multiple Exceptions *****\n“); Car myCar = new Car(“Rusty“, 90); try { // Trip Arg out of range exception. myCar.Accelerate(-10); } catch (CarIsDeadException e) { Console.WriteLine(e.Message); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); } Console.ReadLine(); } When you are authoring multiple catch blocks, you must be aware that when an exception is thrown, it will be processed by the “first available” catch. To illustrate exactly what the “first available” catch means, assume you retrofitted the previous logic with an additional catch scope that attempts to handle all exceptions beyond CarIsDeadException and ArgumentOutOfRangeException by catching a general System.Exception as follows: // This code will not compile! static void Main(string[] args) { Console.WriteLine(“***** Handling Multiple Exceptions *****\n“); Car myCar = new Car(“Rusty“, 90); try { // Trigger an argument out of range exception. myCar.Accelerate(-10); } catch(Exception e) { // Process all other exceptions? Console.WriteLine(e.Message); } catch (CarIsDeadException e) { Console.WriteLine(e.Message); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); } Console.ReadLine(); } CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 273 This exception-handling logic generates compile-time errors. The problem is due to the fact that the first catch block can handle anything derived from System.Exception (given the “is-a” relationship), including the CarIsDeadException and ArgumentOutOfRangeException types. Therefore, the final two catch blocks are unreachable! The rule of thumb to keep in mind is to make sure your catch blocks are structured such that the very first catch is the most specific exception (i.e., the most derived type in an exception-type inheritance chain), leaving the final catch for the most general (i.e., the base class of a given exception inheritance chain, in this case System.Exception). Thus, if you want to define a catch block that will handle any errors beyond CarIsDeadException and ArgumentOutOfRangeException, you could write the following: // This code compiles just fine. static void Main(string[] args) { Console.WriteLine(“***** Handling Multiple Exceptions *****\n“); Car myCar = new Car(“Rusty“, 90); try { // Trigger an argument out of range exception. myCar.Accelerate(-10); } catch (CarIsDeadException e) { Console.WriteLine(e.Message); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); } // This will catch any other exception // beyond CarIsDeadException or // ArgumentOutOfRangeException. catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); }  Note Where at all possible, always favor catching specific exception classes, rather than a general System.Exception. Though it might appear to make life simple in the short term (you may think, “Ah! This catches all the other things I don’t care about.”), in the long term you could end up with strange runtime crashes, as a more serious error was not directly dealt with in your code. Remember, a final catch block that deals with System.Exception tends to be very general indeed. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 274 General catch Statements C# also supports a “general” catch scope that does not explicitly receive the exception object thrown by a given member: // A generic catch. static void Main(string[] args) { Console.WriteLine(“***** Handling Multiple Exceptions *****\n“); Car myCar = new Car(“Rusty“, 90); try { myCar.Accelerate(90); } catch { Console.WriteLine(“Something bad happened...“); } Console.ReadLine(); } Obviously, this is not the most informative way to handle exceptions, since you have no way to obtain meaningful data about the error that occurred (such as the method name, call stack, or custom message). Nevertheless, C# does allow for such a construct, which can be helpful when you want to handle all errors in a very, very general fashion. Rethrowing Exceptions When you catch an exception, it is permissible for the logic in a try block to rethrow the exception up the call stack to the previous caller. To do so, simply use the throw keyword within a catch block. This passes the exception up the chain of calling logic, which can be helpful if your catch block is only able to partially handle the error at hand: // Passing the buck. static void Main(string[] args) { ... try { // Speed up car logic... } catch(CarIsDeadException e) { // Do any partial processing of this error and pass the buck. throw; } ... } Be aware that in this example code, the ultimate receiver of CarIsDeadException is the CLR, because it is the Main() method rethrowing the exception. Because of this, your end user is presented with a CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 275 system-supplied error dialog box. Typically, you would only rethrow a partial handled exception to a caller that has the ability to handle the incoming exception more gracefully. Notice as well that we are not explicitly rethrowing the CarIsDeadException object, but rather making use of the throw keyword with no argument. We’re not creating a new exception object; we’re just rethrowing the original exception object (with all its original information). Doing so preserves the context of the original target. Inner Exceptions As you might suspect, it is entirely possible to trigger an exception at the time you are handling another exception. For example, assume you are handling a CarIsDeadException within a particular catch scope, and during the process you attempt to record the stack trace to a file on your C: drive named carErrors.txt (you must specify you are using the System.IO namespace to gain access to these I/O- centric types): catch(CarIsDeadException e) { // Attempt to open a file named carErrors.txt on the C drive. FileStream fs = File.Open(@“C:\carErrors.txt“, FileMode.Open); ... } Now, if the specified file is not located on your C: drive, the call to File.Open() results in a FileNotFoundException! Later in this text, you will learn all about the System.IO namespace where you’ll discover how to programmatically determine whether a file exists on the hard drive before attempting to open the file in the first place (thereby avoiding the exception altogether). However, to stay focused on the topic of exceptions, assume the exception has been raised. When you encounter an exception while processing another exception, best practice states that you should record the new exception object as an “inner exception” within a new object of the same type as the initial exception. (That was a mouthful!) The reason you need to allocate a new object of the exception being handled is that the only way to document an inner exception is via a constructor parameter. Consider the following code: catch (CarIsDeadException e) { try { FileStream fs = File.Open(@“C:\carErrors.txt“, FileMode.Open); ... } catch (Exception e2) { // Throw an exception that records the new exception, // as well as the message of the first exception. throw new CarIsDeadException(e.Message, e2); } } Notice in this case, we have passed in the FileNotFoundException object as the second parameter to the CarIsDeadException constructor. After we have configured this new object, we throw it up the call stack to the next caller, which in this case would be the Main() method. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 276 Given that there is no “next caller” after Main() to catch the exception, we would be again presented with an error dialog box. Much like the act of rethrowing an exception, recording inner exceptions is usually only useful when the caller has the ability to gracefully catch the exception in the first place. If this is the case, the caller’s catch logic can make use of the InnerException property to extract the details of the inner exception object. The finally Block A try/catch scope may also define an optional finally block. The purpose of a finally block is to ensure that a set of code statements will always execute, exception (of any type) or not. To illustrate, assume you want to always power down the car’s radio before exiting Main(), regardless of any handled exception: static void Main(string[] args) { Console.WriteLine(“***** Handling Multiple Exceptions *****\n“); Car myCar = new Car(“Rusty“, 90); myCar.CrankTunes(true); try { // Speed up car logic. } catch(CarIsDeadException e) { // Process CarIsDeadException. } catch(ArgumentOutOfRangeException e) { // Process ArgumentOutOfRangeException. } catch(Exception e) { // Process any other Exception. } finally { // This will always occur. Exception or not. myCar.CrankTunes(false); } Console.ReadLine(); } If you did not include a finally block, the radio would not be turned off if an exception is encountered (which might or might not be problematic). In a more real-world scenario, when you need to dispose of objects, close a file, or detach from a database (or whatever), a finally block ensures a location for proper cleanup. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 277 Who Is Throwing What? Given that a method in the .NET Framework could throw any number of exceptions under various circumstances, a logical question is, “How do I know which exceptions may be thrown by a given base class library method?” The ultimate answer is simple: consult the .NET Framework 4.5 SDK documentation. Each method in the help system documents the exceptions a given member may throw. As a quick alternative, Visual Studio allows you to view the list of all exceptions thrown by a base class library member (if any) simply by hovering your mouse cursor over the member name in the code window (see Figure 7-2). Figure 7-2. Identifying the exceptions thrown from a given method  Note For those coming to .NET from a Java background, understand that type members are not prototyped with the set of exceptions they may throw (in other words, .NET does not support checked exceptions). For better or for worse, you are not required to handle each and every exception thrown from a given member. The Result of Unhandled Exceptions At this point, you might be wondering what would happen if you do not handle an exception thrown in your direction. Assume that the logic in Main() increases the speed of the Car object beyond the maximum speed, without the benefit of try/catch logic: CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 278 static void Main(string[] args) { Console.WriteLine(“***** Handling Multiple Exceptions *****\n“); Car myCar = new Car(“Rusty“, 90); myCar.Accelerate(500); Console.ReadLine(); } The result of ignoring an exception would be rather obstructive to the end user of your application, as an “unhandled exception” dialog box is displayed (see Figure 7-3). Figure 7-3. The result of not dealing with exceptions Debugging Unhandled Exceptions Using Visual Studio Do be aware that Visual Studio supplies a number of tools that help you debug unhandled custom exceptions. Again, assume you have increased the speed of a Car object beyond the maximum. If you start a debugging session within Visual Studio (using the Debug  Start Debugging menu selection), Visual Studio automatically breaks at the time the uncaught exception is thrown. Better yet, you are presented with a window (see Figure 7-4) displaying the value of the Message property. CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 279 Figure 7-4. Debugging unhandled custom exceptions with Visual Studio  Note If you fail to handle an exception thrown by a method in the .NET base class libraries, the Visual Studio debugger breaks at the statement that called the offending method. If you click the View Detail link, you will find the details regarding the state of the object (see Figure 7-5). CHAPTER 7  UNDERSTANDING STRUCTURED EXCEPTION HANDLING 280 Figure 7-5. Viewing exception details  Source Code The ProcessMultipleExceptions project is included under the Chapter 7 subdirectory. Summary In this chapter, you examined the role of structured exception handling. When a method needs to send an error object to the caller, it will allocate, configure, and throw a specific System.Exception derived type via the C# throw keyword. The caller is able to handle any possible incoming exceptions using the C# catch keyword and an optional finally scope. When you are creating your own custom exceptions, you ultimately create a class type deriving from System.ApplicationException, which denotes an exception thrown from the currently executing application. In contrast, error objects deriving from System.SystemException represent critical (and fatal) errors thrown by the CLR. Last but not least, this chapter illustrated various tools within Visual Studio that can be used to create custom exceptions (according to .NET best practices) as well as debug exceptions. C H A P T E R 8 281 Working with Interfaces This chapter builds upon your current understanding of object-oriented development by examining the topic of interface-based programming. Here you’ll learn how to define and implement interfaces, and come to understand the benefits of building types that support multiple behaviors. Along the way, I’ll also discuss a number of related topics, such as obtaining interface references, explicit interface implementation, and the construction of interface hierarchies. We’ll also examine a number of standard interfaces defined within the .NET base class libraries. As you will see, your custom classes and structures are free to implement these predefined interfaces to support a number of useful behaviors, such as object cloning, object enumeration, and object sorting. Understanding Interface Types To begin this chapter, allow me to provide a formal definition of the interface type. An interface is nothing more than a named set of abstract members. Recall from Chapter 6 that abstract methods are pure protocol in that they do not provide a default implementation. The specific members defined by an interface depend on the exact behavior it is modeling. Said another way, an interface expresses a behavior that a given class or structure may choose to support. Furthermore, as you will see in this chapter, a class or structure can support as many interfaces as necessary, thereby supporting (in essence) multiple behaviors. As you might guess, the .NET base class libraries ship with hundreds of predefined interface types that are implemented by various classes and structures. For example, as you will see in Chapter 21, ADO.NET ships with multiple data providers that allow you to communicate with a particular database management system. Thus, under ADO.NET we have numerous connection objects to choose from (SqlConnection, OleDbConnection, OdbcConnection, etc.). Regardless of the fact that each connection object has a unique name, is defined within a different namespace, and (in some cases) is bundled within a different assembly, all connection objects implement a common interface named IDbConnection: // The IDbConnection interface defines a common // set of members supported by all connection objects. public interface IDbConnection : IDisposable { // Methods IDbTransaction BeginTransaction(); IDbTransaction BeginTransaction(IsolationLevel il); void ChangeDatabase(string databaseName); void Close(); IDbCommand CreateCommand(); void Open(); CHAPTER 8  WORKING WITH INTERFACES 282 // Properties string ConnectionString { get; set;} int ConnectionTimeout { get; } string Database { get; } ConnectionState State { get; } }  Note By convention, .NET interfaces are prefixed with a capital letter “I.” When you are creating your own custom interfaces, it is considered a best practice to do the same. Don’t concern yourself with the details of what these members actually do at this point. Simply understand that the IDbConnection interface defines a set of members that are common to all ADO.NET connection objects. Given this, you are guaranteed that every connection object supports members such as Open(), Close(), CreateCommand(), and so forth. Furthermore, given that interface members are always abstract, each connection object is free to implement these methods in its own unique manner. As you work through the remainder of this book, you’ll be exposed to dozens of interfaces that ship with the .NET base class libraries. As you will see, these interfaces can be implemented on your own custom classes and structures to define types that integrate tightly within the framework. Interface Types vs. Abstract Base Classes Given your work in Chapter 6, the interface type might seem very similar to an abstract base class. Recall that when a class is marked as abstract, it may define any number of abstract members to provide a polymorphic interface to all derived types. However, even when a class does define a set of abstract members, it is also free to define any number of constructors, field data, nonabstract members (with implementation), and so on. Interfaces, on the other hand, contain only abstract member definitions. The polymorphic interface established by an abstract parent class suffers from one major limitation in that only derived types support the members defined by the abstract parent. However, in larger software systems, it is very common to develop multiple class hierarchies that have no common parent beyond System.Object. Given that abstract members in an abstract base class apply only to derived types, we have no way to configure types in different hierarchies to support the same polymorphic interface. By way of example, assume you have defined the following abstract class: public abstract class CloneableType { // Only derived types can support this // “polymorphic interface.“ Classes in other // hierarchies have no access to this abstract // member. public abstract object Clone(); } Given this definition, only members that extend CloneableType are able to support the Clone() method. If you create a new set of classes that do not extend this base class, you can’t gain this polymorphic interface. Also, you might recall that C# does not support multiple inheritance for classes. Therefore, if you wanted to create a MiniVan that is-a Car and is-a CloneableType, you are unable to do so: CHAPTER 8  WORKING WITH INTERFACES 283 // Nope! Multiple inheritance is not possible in C# // for classes. public class MiniVan : Car, CloneableType { } As you would guess, interface types come to the rescue. After an interface has been defined, it can be implemented by any class or structure, in any hierarchy, within any namespace or any assembly (written in any .NET programming language). As you can see, interfaces are highly polymorphic. Consider the standard .NET interface named ICloneable, defined in the System namespace. This interface defines a single method named Clone(): public interface ICloneable { object Clone(); } If you examine the .NET Framework 4.5 SDK documentation, you’ll find that a large number of seemingly unrelated types (System.Array, System.Data.SqlClient.SqlConnection, System.OperatingSystem, System.String, etc.) all implement this interface. Although these types have no common parent (other than System.Object), we can treat them polymorphically via the ICloneable interface type. For example, if you had a method named CloneMe() that took an ICloneable interface parameter, you could pass this method any object that implements said interface. Consider the following simple Program class defined within a Console Application named ICloneableExample: class Program { static void Main(string[] args) { Console.WriteLine(“***** A First Look at Interfaces *****\n“); // All of these classes support the ICloneable interface. string myStr = “Hello“; OperatingSystem unixOS = new OperatingSystem(PlatformID.Unix, new Version()); System.Data.SqlClient.SqlConnection sqlCnn = new System.Data.SqlClient.SqlConnection(); // Therefore, they can all be passed into a method taking ICloneable. CloneMe(myStr); CloneMe(unixOS); CloneMe(sqlCnn); Console.ReadLine(); } private static void CloneMe(ICloneable c) { // Clone whatever we get and print out the name. object theClone = c.Clone(); Console.WriteLine(“Your clone is a: {0}“, theClone.GetType().Name); } } CHAPTER 8  WORKING WITH INTERFACES 284 When you run this application, the class name of each class prints out to the console, via the GetType() method you inherit from System.Object. As explained in Chapter 15, this method (and .NET reflection services) allow you to understand the composition of any type at runtime. In any case, the output of the previous program is shown next: ***** A First Look at Interfaces ***** Your clone is a: String Your clone is a: OperatingSystem Your clone is a: SqlConnection Another limitation of abstract base classes is that each derived type must contend with the set of abstract members and provide an implementation. To see this problem, recall the shapes hierarchy we defined in Chapter 6. Assume we defined a new abstract method in the Shape base class named GetNumberOfPoints(), which allows derived types to return the number of points required to render the shape: abstract class Shape { ... // Every derived class must now support this method! public abstract byte GetNumberOfPoints(); } Clearly, the only class that has any points in the first place is Hexagon. However, with this update, every derived class (Circle, Hexagon, and ThreeDCircle) must now provide a concrete implementation of this function, even if it makes no sense to do so. Again, the interface type provides a solution. If you define an interface that represents the behavior of “having points,” you can simply plug it into the Hexagon type, leaving Circle and ThreeDCircle untouched.  Source Code The ICloneableExample project is located under the Chapter 8 subdirectory. Defining Custom Interfaces Now that you better understand the overall role of interface types, let’s see an example of defining and implementing custom interfaces. To begin, create a brand-new Console Application named CustomInterface. Using the Project  Add Existing Item menu option, insert the file(s) containing your shape type definitions (Shape.cs and DerivedShapes.cs in the book’s solution code) created back in Chapter 6 during the Shapes example. After you have done so, rename the namespace that defines your shape-centric types to CustomInterface (simply to avoid having to import namespace definitions in your new project): namespace CustomInterface { // Your shape types defined here... } CHAPTER 8  WORKING WITH INTERFACES 285 Now, insert a new interface into your project named IPointy using the Project  Add New Item menu option, as shown in Figure 8-1. Figure 8-1. Interfaces, like classes, can be defined in any *.cs file At a syntactic level, an interface is defined using the C# interface keyword. Unlike a class, interfaces never specify a base class (not even System.Object; however, as you will see later in this chapter, an interface can specify base interfaces). Moreover, the members of an interface never specify an access modifier (as all interface members are implicitly public and abstract). To get the ball rolling, here is a custom interface defined in C#: // This interface defines the behavior of “having points.“ public interface IPointy { // Implicitly public and abstract. byte GetNumberOfPoints(); } Remember that when you define interface members, you do not define an implementation scope for the members in question. Interfaces are pure protocol and, therefore, never define an implementation (that is up to the supporting class or structure). Hence, the following version of IPointy would result in various compiler errors: // Ack! Errors abound! public interface IPointy { // Error! Interfaces cannot have data fields! public int numbOfPoints; CHAPTER 8  WORKING WITH INTERFACES 286 // Error! Interfaces do not have constructors! public IPointy() { numbOfPoints = 0;}; // Error! Interfaces don't provide an implementation of members! byte GetNumberOfPoints() { return numbOfPoints; } } In any case, this initial IPointy interface defines a single method. However, .NET interface types are also able to define any number of property prototypes. For example, let’s update the IPointy interface to use a read-only property rather than a traditional accessor method: // The pointy behavior as a read-only property. public interface IPointy { // A read-write property in an interface would look like: // retType PropName { get; set; } // // while a write-only property in an interface would be: // retType PropName { set; } byte Points { get; } }  Note Interface types can also contain event (see Chapter 10) and indexer (see Chapter 11) definitions. Interface types are quite useless on their own, as they are nothing more than a named collection of abstract members. For example, you can’t allocate interface types as you would a class or structure: // Ack! Illegal to allocate interface types. static void Main(string[] args) { IPointy p = new IPointy(); // Compiler error! } Interfaces do not bring much to the table until they are implemented by a class or structure. Here, IPointy is an interface that expresses the behavior of “having points.” The idea is simple: some classes in the shapes hierarchy have points (such as the Hexagon), while others (such as the Circle) do not. Implementing an Interface When a class (or structure) chooses to extend its functionality by supporting interfaces, it does so using a comma-delimited list in the type definition. Be aware that the direct base class must be the first item listed after the colon operator. When your class type derives directly from System.Object, you are free to simply list the interface(s) supported by the class, as the C# compiler will extend your types from System.Object if you do not say otherwise. On a related note, given that structures always derive from System.ValueType (see Chapter 4), simply list each interface directly after the structure definition. Ponder the following examples: CHAPTER 8  WORKING WITH INTERFACES 287 // This class derives from System.Object and // implements a single interface. public class Pencil : IPointy {...} // This class also derives from System.Object // and implements a single interface. public class SwitchBlade : object, IPointy {...} // This class derives from a custom base class // and implements a single interface. public class Fork : Utensil, IPointy {...} // This struct implicitly derives from System.ValueType and // implements two interfaces. public struct PitchFork : ICloneable, IPointy {...} Understand that implementing an interface is an all-or-nothing proposition. The supporting type is not able to selectively choose which members it will implement. Given that the IPointy interface defines a single read-only property, this is not too much of a burden. However, if you are implementing an interface that defines ten members (such as the IDbConnection interface shown earlier), the type is now responsible for fleshing out the details of all ten abstract members. For this example, insert a new class type named Triangle that is-a Shape and supports IPointy. Note that the implementation of the read-only Points property simply returns the correct number of points (3). // New Shape derived class named Triangle. class Triangle : Shape, IPointy { public Triangle() { } public Triangle(string name) : base(name) { } public override void Draw() { Console.WriteLine(“Drawing {0} the Triangle“, PetName); } // IPointy implementation. public byte Points { get { return 3; } } } Now, update your existing Hexagon type to also support the IPointy interface type: // Hexagon now implements IPointy. class Hexagon : Shape, IPointy { public Hexagon(){ } public Hexagon(string name) : base(name){ } public override void Draw() { Console.WriteLine(“Drawing {0} the Hexagon“, PetName); } CHAPTER 8  WORKING WITH INTERFACES 288 // IPointy implementation. public byte Points { get { return 6; } } } To sum up the story so far, the Visual Studio class diagram shown in Figure 8-2 illustrates IPointy- compatible classes using the popular “lollipop” notation. Notice again that Circle and ThreeDCircle do not implement IPointy, as this behavior makes no sense for these particular classes. Figure 8-2. The shapes hierarchy, now with interfaces  Note To display or hide interface names on the class designer, right-click on the interface icon and select the Collapse or Expand option. Invoking Interface Members at the Object Level Now that you have some classes that support the IPointy interface, the next question is how you interact with the new functionality. The most straightforward way to interact with functionality supplied by a given interface is to invoke the members directly from the object level (provided the interface members are not implemented explicitly; more details later in the section “Explicit Interface Implementation”). For example, consider the following Main() method: static void Main(string[] args) { Console.WriteLine(“***** Fun with Interfaces *****\n“); CHAPTER 8  WORKING WITH INTERFACES 289 // Call Points property defined by IPointy. Hexagon hex = new Hexagon(); Console.WriteLine(“Points: {0}“, hex.Points); Console.ReadLine(); } This approach works fine in this particular case, given that you are well aware that the Hexagon type has implemented the interface in question and, therefore, has a Points property. Other times, however, you might not be able to determine which interfaces are supported by a given type. For example, suppose you have an array containing 50 Shape-compatible types, only some of which support IPointy. Obviously, if you attempt to invoke the Points property on a type that has not implemented IPointy, you would receive an error. So how can you dynamically determine if a class or structure supports the correct interface? One way to determine at runtime whether a type supports a specific interface is to make use of an explicit cast. If the type does not support the requested interface, you receive an InvalidCastException. To handle this possibility gracefully, use structured exception handling as in the following example: static void Main(string[] args) { ... // Catch a possible InvalidCastException. Circle c = new Circle(“Lisa“); IPointy itfPt = null; try { itfPt = (IPointy)c; Console.WriteLine(itfPt.Points); } catch (InvalidCastException e) { Console.WriteLine(e.Message); } Console.ReadLine(); } While you could use try/catch logic and hope for the best, it would be ideal to determine which interfaces are supported before invoking the interface members in the first place. Let’s see two ways of doing so. Obtaining Interface References: The as Keyword You can determine whether a given type supports an interface by using the as keyword, introduced in Chapter 6. If the object can be treated as the specified interface, you are returned a reference to the interface in question. If not, you receive a null reference. Therefore, be sure to check against a null value before proceeding: static void Main(string[] args) { ... // Can we treat hex2 as IPointy? Hexagon hex2 = new Hexagon(“Peter“); CHAPTER 8  WORKING WITH INTERFACES 290 IPointy itfPt2 = hex2 as IPointy; if(itfPt2 != null) Console.WriteLine(“Points: {0}“, itfPt2.Points); else Console.WriteLine(“OOPS! Not pointy...“); Console.ReadLine(); } Notice that when you use the as keyword, you have no need to use try/catch logic, given that if the reference is not null, you know you are calling on a valid interface reference. Obtaining Interface References: The is Keyword You may also check for an implemented interface using the is keyword (also first discussed in Chapter 6). If the object in question is not compatible with the specified interface, you are returned the value false. On the other hand, if the type is compatible with the interface in question, you can safely call the members without needing to use try/catch logic. To illustrate, assume we have an array of Shape types containing some members that implement IPointy. Notice how we are able to determine which items in the array support this interface using the is keyword, as shown in this retrofitted Main() method: static void Main(string[] args) { Console.WriteLine(“***** Fun with Interfaces *****\n“); // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle(“Joe“), new Circle(“JoJo“)} ; for(int i = 0; i < myShapes.Length; i++) { // Recall the Shape base class defines an abstract Draw() // member, so all shapes know how to draw themselves. myShapes[i].Draw(); // Who's pointy? if(myShapes[i] is IPointy) Console.WriteLine(“-> Points: {0}“, ((IPointy) myShapes[i]).Points); else Console.WriteLine(“-> {0}\'s not pointy!“, myShapes[i].PetName); Console.WriteLine(); } Console.ReadLine(); } CHAPTER 8  WORKING WITH INTERFACES 291 The output is as follows: ***** Fun with Interfaces ***** Drawing NoName the Hexagon -> Points: 6 Drawing NoName the Circle -> NoName's not pointy! Drawing Joe the Triangle -> Points: 3 Drawing JoJo the Circle -> JoJo's not pointy! Interfaces As Parameters Given that interfaces are valid .NET types, you may construct methods that take interfaces as parameters, as illustrated by the CloneMe() method earlier in this chapter. For the current example, assume you have defined another interface named IDraw3D: // Models the ability to render a type in stunning 3D. public interface IDraw3D { void Draw3D(); } Next, assume that two of your three shapes (ThreeDCircle and Hexagon) have been configured to support this new behavior: // Circle supports IDraw3D. class ThreeDCircle : Circle, IDraw3D { ... public void Draw3D() { Console.WriteLine(“Drawing Circle in 3D!“); } } // Hexagon supports IPointy and IDraw3D. class Hexagon : Shape, IPointy, IDraw3D { ... public void Draw3D() { Console.WriteLine(“Drawing Hexagon in 3D!“); } } Figure 8-3 presents the updated Visual Studio class diagram. CHAPTER 8  WORKING WITH INTERFACES 292 Figure 8-3. The updated shapes hierarchy If you now define a method taking an IDraw3D interface as a parameter, you can effectively send in any object implementing IDraw3D. (If you attempt to pass in a type not supporting the necessary interface, you receive a compile-time error.) Consider the following method defined within your Program class: // I'll draw anyone supporting IDraw3D. static void DrawIn3D(IDraw3D itf3d) { Console.WriteLine(“-> Drawing IDraw3D compatible type“); itf3d.Draw3D(); } We could now test whether an item in the Shape array supports this new interface, and if so, pass it into the DrawIn3D() method for processing: static void Main(string[] args) { Console.WriteLine(“***** Fun with Interfaces *****\n“); Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle(), new Circle(“JoJo“) } ; for(int i = 0; i < myShapes.Length; i++) { ... // Can I draw you in 3D? if(myShapes[i] is IDraw3D) DrawIn3D((IDraw3D)myShapes[i]); } } Here is the output of the updated application. Notice that only the Hexagon object prints out in 3D, as the other members of the Shape array do not implement the IDraw3D interface. CHAPTER 8  WORKING WITH INTERFACES 293 ***** Fun with Interfaces ***** Drawing NoName the Hexagon -> Points: 6 -> Drawing IDraw3D compatible type Drawing Hexagon in 3D! Drawing NoName the Circle -> NoName's not pointy! Drawing Joe the Triangle -> Points: 3 Drawing JoJo the Circle -> JoJo's not pointy! Interfaces As Return Values Interfaces can also be used as method return values. For example, you could write a method that takes an array of Shape objects and returns a reference to the first item that supports IPointy: // This method returns the first object in the // array that implements IPointy. static IPointy FindFirstPointyShape(Shape[] shapes) { foreach (Shape s in shapes) { if (s is IPointy) return s as IPointy; } return null; } You could interact with this method as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Interfaces *****\n“); // Make an array of Shapes. Shape[] myShapes = { new Hexagon(), new Circle(), new Triangle(“Joe“), new Circle(“JoJo“)}; // Get first pointy item. // To be safe, you'd want to check firstPointyItem for null before proceeding. IPointy firstPointyItem = FindFirstPointyShape(myShapes); Console.WriteLine(“The item has {0} points“, firstPointyItem.Points); ... } CHAPTER 8  WORKING WITH INTERFACES 294 Arrays of Interface Types Recall that the same interface can be implemented by numerous types, even if they are not within the same class hierarchy and do not have a common parent class beyond System.Object. This can yield some very powerful programming constructs. For example, assume you have developed three new class types within your current project that model kitchen utensils (via Knife and Fork classes) and another modeling gardening equipment (à la PitchFork). Consider Figure 8-4. Figure 8-4. Recall that interfaces can be “plugged into” any type in any part of a class hierarchy If you defined the PitchFork, Fork, and Knife types, you could now define an array of IPointy- compatible objects. Given that these members all support the same interface; you can iterate through the array and treat each item as an IPointy-compatible object, regardless of the overall diversity of the class hierarchies: static void Main(string[] args) { ... // This array can only contain types that // implement the IPointy interface. IPointy[] myPointyObjects = {new Hexagon(), new Knife(), new Triangle(), new Fork(), new PitchFork()}; foreach(IPointy i in myPointyObjects) Console.WriteLine(“Object has {0} points.“, i.Points); Console.ReadLine(); } Just to highlight the importance of this example, remember this: when you have an array of a given interface, the array can contain any class or structure which implements that interface. CHAPTER 8  WORKING WITH INTERFACES 295  Source Code The CustomInterface project is located under the Chapter 8 subdirectory. Implementing Interfaces Using Visual Studio Although interface-based programming is a very powerful technique, implementing interfaces may entail a healthy amount of typing. Given that interfaces are a named set of abstract members, you are required to type in the definition and implementation for each interface method on each type that supports the behavior. Therefore, if you want to support an interface that defines a total of five methods and three properties, you need to account for all eight members (or else you will receive compiler errors). As you would hope, Visual Studio supports various tools that make the task of implementing interfaces less burdensome. By way of a simple test, insert a final class into your current project named PointyTestClass. When you implement IPointy (or any interface for that matter), you might have noticed that when you complete typing the interface’s name (or when you position the mouse cursor on the interface name in the code window), the first letter is underlined (formally termed a smart tag). When you click the smart tag, you will be presented with a drop-down list that allows you to implement the interface (see Figure 8-5). Figure 8-5. Implementing interfaces using Visual Studio Notice you are presented with two options, the second of which (explicit interface implementation) will be examined in the next section. For the time being, select the first option and you’ll see that Visual Studio has generated stub code for you to update (note that the default implementation throws a System.NotImplementedException, which can obviously be deleted). CHAPTER 8  WORKING WITH INTERFACES 296 namespace CustomInterface { class PointyTestClass : IPointy { public byte Points { get { throw new NotImplementedException(); } } } }  Note Visual Studio also supports extract interface refactoring, available from the Refactor menu. This allows you to pull out a new interface definition from an existing class definition. For example, you might be halfway through writing a class when it dawns on you that you can generalize the behavior into an interface (and thereby open up the possibility of alternative implementations). Explicit Interface Implementation As shown earlier in this chapter, a single class or structure can implement any number of interfaces. Given this, there is always the possibility you might implement interfaces that contain identical members and, therefore, have a name clash to contend with. To illustrate various manners in which you can resolve this issue, create a new Console Application named InterfaceNameClash. Now design three interfaces that represent various locations to which an implementing type could render its output: // Draw image to a form. public interface IDrawToForm { void Draw(); } // Draw to buffer in memory. public interface IDrawToMemory { void Draw(); } // Render to the printer. public interface IDrawToPrinter { void Draw(); } Notice that each interface defines a method named Draw(), with the identical signature (which happen to be no arguments). If you now want to support each of these interfaces on a single class type named Octagon, the compiler would allow the following definition: CHAPTER 8  WORKING WITH INTERFACES 297 class Octagon : IDrawToForm, IDrawToMemory, IDrawToPrinter { public void Draw() { // Shared drawing logic. Console.WriteLine(“Drawing the Octagon...“); } } Although the code compiles cleanly, we do have a possible problem. Simply put, providing a single implementation of the Draw() method does not allow us to take unique courses of action based on which interface is obtained from an Octagon object. For example, the following code will invoke the same Draw() method, regardless of which interface you obtain: static void Main(string[] args) { Console.WriteLine(“***** Fun with Interface Name Clashes *****\n“); // All of these invocations call the // same Draw() method! Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToPrinter itfPriner = (IDrawToPrinter)oct; itfPriner.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); Console.ReadLine(); } Clearly, the sort of code required to render the image to a window is quite different from the code needed to render the image to a networked printer or a region of memory. When you implement several interfaces that have identical members, you can resolve this sort of name clash using explicit interface implementation syntax. Consider the following update to the Octagon type: class Octagon : IDrawToForm, IDrawToMemory, IDrawToPrinter { // Explicitly bind Draw() implementations // to a given interface. void IDrawToForm.Draw() { Console.WriteLine(“Drawing to form...“); } void IDrawToMemory.Draw() { Console.WriteLine(“Drawing to memory...“); } void IDrawToPrinter.Draw() { Console.WriteLine(“Drawing to a printer...“); CHAPTER 8  WORKING WITH INTERFACES 298 } } As you can see, when explicitly implementing an interface member, the general pattern breaks down to: returnType InterfaceName.MethodName(params){} Note that when using this syntax, you do not supply an access modifier; explicitly implemented members are automatically private. For example, the following is illegal syntax: // Error! No access modifer! public void IDrawToForm.Draw() { Console.WriteLine(“Drawing to form...“); } Because explicitly implemented members are always implicitly private, these members are no longer available from the object level. In fact, if you were to apply the dot operator to an Octagon type, you would find that IntelliSense does not show you any of the Draw() members. As expected, you must make use of explicit casting to access the required functionality. For example: static void Main(string[] args) { Console.WriteLine(“***** Fun with Interface Name Clashes *****\n“); Octagon oct = new Octagon(); // We now must use casting to access the Draw() // members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToPrinter)oct).Draw(); // Could also use the “as“ keyword. if(oct is IDrawToMemory) ((IDrawToMemory)oct).Draw(); Console.ReadLine(); } While this syntax is quite helpful when you need to resolve name clashes, you can use explicit interface implementation simply to hide more “advanced” members from the object level. In this way, when the object user applies the dot operator, he or she will see only a subset of the type’s overall functionality. However, those who require the more advanced behaviors can extract out the desired interface via an explicit cast. CHAPTER 8  WORKING WITH INTERFACES 299  Source Code The InterfaceNameClash project is located under the Chapter 8 subdirectory. Designing Interface Hierarchies Interfaces can be arranged in an interface hierarchy. Like a class hierarchy, when an interface extends an existing interface, it inherits the abstract members defined by the parent(s). Of course, unlike class- based inheritance, derived interfaces never inherit true implementation. Rather, a derived interface simply extends its own definition with additional abstract members. Interface hierarchies can be useful when you want to extend the functionality of an existing interface without breaking existing code bases. To illustrate, create a new Console Application named InterfaceHierarchy. Now, let’s design a new set of rendering-centric interfaces such that IDrawable is the root of the family tree: public interface IDrawable { void Draw(); } Given that IDrawable defines a basic drawing behavior, we could now create a derived interface that extends this interface with the ability to render in modified formats. For example: public interface IAdvancedDraw : IDrawable { void DrawInBoundingBox(int top, int left, int bottom, int right); void DrawUpsideDown(); } Given this design, if a class were to implement IAdvancedDraw, it would now be required to implement each and every member defined up the chain of inheritance (specifically, the Draw(), DrawInBoundingBox(), and DrawUpsideDown() methods): public class BitmapImage : IAdvancedDraw { public void Draw() { Console.WriteLine(“Drawing...“); } public void DrawInBoundingBox(int top, int left, int bottom, int right) { Console.WriteLine(“Drawing in a box...“); } public void DrawUpsideDown() { Console.WriteLine(“Drawing upside down!“); } } CHAPTER 8  WORKING WITH INTERFACES 300 Now, when we make use of the BitmapImage, we are able to invoke each method at the object level (as they are all public), as well as extract out a reference to each supported interface explicitly via casting: static void Main(string[] args) { Console.WriteLine(“***** Simple Interface Hierarchy *****“); // Call from object level. BitmapImage myBitmap = new BitmapImage(); myBitmap.Draw(); myBitmap.DrawInBoundingBox(10, 10, 100, 150); myBitmap.DrawUpsideDown(); // Get IAdvancedDraw explicitly. IAdvancedDraw iAdvDraw = myBitmap as IAdvancedDraw; if(iAdvDraw != null) iAdvDraw.DrawUpsideDown(); Console.ReadLine(); }  Source Code The InterfaceHierarchy project is located under the Chapter 8 subdirectory. Multiple Inheritance with Interface Types Unlike class types, a single interface can extend multiple base interfaces, allowing us to design some very powerful and flexible abstractions. Create a new Console Application project named MIInterfaceHierarchy. Here is another collection of interfaces that model various rendering and shape abstractions. Notice that the IShape interface is extending both IDrawable and IPrintable: // Multiple inheritance for interface types is a-okay. interface IDrawable { void Draw(); } interface IPrintable { void Print(); void Draw(); // <-- Note possible name clash here! } // Multiple interface inheritance. OK! interface IShape : IDrawable, IPrintable { int GetNumberOfSides(); } CHAPTER 8  WORKING WITH INTERFACES 301 Figure 8-6 illustrates the current interface hierarchy. Figure 8-6. Unlike classes, interfaces can extend multiple interface types Now, the million dollar question is, if you have a class supporting IShape, how many methods will it be required to implement? The answer: it depends. If you want to provide a simple implementation of the Draw() method, you need only provide three members, as shown in the following Rectangle type: class Rectangle : IShape { public int GetNumberOfSides() { return 4; } public void Draw() { Console.WriteLine(“Drawing...“); } public void Print() { Console.WriteLine(“Prining...“); } } If you’d rather have specific implementations for each Draw() method (which in this case would make the most sense), you can resolve the name clash using explicit interface implementation, as shown in the following Square type: class Square : IShape { // Using explicit implementation to handle member name clash. void IPrintable.Draw() { // Draw to printer ... } void IDrawable.Draw() { // Draw to screen ... } public void Print() { // Print ... } CHAPTER 8  WORKING WITH INTERFACES 302 public int GetNumberOfSides() { return 4; } } Hopefully, at this point you feel more comfortable with the process of defining and implementing custom interfaces using the C# syntax. To be honest, interface-based programming can take a while to get comfortable with, so if you are in fact still scratching your head just a bit, this is a perfectly normal reaction. Do be aware, however, that interfaces are a fundamental aspect of the .NET Framework. Regardless of the type of application you are developing (web-based, desktop GUIs, data-access libraries, etc.), working with interfaces will be part of the process. To summarize the story thus far, remember that interfaces can be extremely useful when • You have a single hierarchy where only a subset of the derived types supports a common behavior. • You need to model a common behavior that is found across multiple hierarchies with no common parent class beyond System.Object. Now that you have drilled into the specifics of building and implementing custom interfaces, the remainder of this chapter examines a number of predefined interfaces contained within the .NET base class libraries.  Source Code The MIInterfaceHierarchy project is located under the Chapter 8 subdirectory. The IEnumerable and IEnumerator Interfaces To begin examining the process of implementing existing .NET interfaces, let’s first look at the role of IEnumerable and IEnumerator. Recall that C# supports a keyword named foreach that allows you to iterate over the contents of any array type: // Iterate over an array of items. int[] myArrayOfInts = {10, 20, 30, 40}; foreach(int i in myArrayOfInts) { Console.WriteLine(i); } While it might seem that only array types can make use of this construct, the truth of the matter is any type supporting a method named GetEnumerator() can be evaluated by the foreach construct. To illustrate, begin by creating a new Console Application project named CustomEnumerator. Next, add the Car.cs and Radio.cs files defined in the SimpleException example of Chapter 7 (via the Project  Add Existing Item menu option). CHAPTER 8  WORKING WITH INTERFACES 303  Note You might want to rename the namespace containing the Car and Radio types to CustomEnumerator, to avoid having to import the CustomException namespace within this new project. Now, insert a new class named Garage that stores a set of Car objects within a System.Array: // Garage contains a set of Car objects. public class Garage { private Car[] carArray = new Car[4]; // Fill with some Car objects upon startup. public Garage() { carArray[0] = new Car(“Rusty“, 30); carArray[1] = new Car(“Clunker“, 55); carArray[2] = new Car(“Zippy“, 30); carArray[3] = new Car(“Fred“, 30); } } Ideally, it would be convenient to iterate over the Garage object’s subitems using the foreach construct, just like an array of data values: // This seems reasonable ... public class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with IEnumerable / IEnumerator *****\n“); Garage carLot = new Garage(); // Hand over each car in the collection? foreach (Car c in carLot) { Console.WriteLine(“{0} is going {1} MPH“, c.PetName, c.CurrentSpeed); } Console.ReadLine(); } } Sadly, the compiler informs you that the Garage class does not implement a method named GetEnumerator(). This method is formalized by the IEnumerable interface, which is found lurking within the System.Collections namespace. CHAPTER 8  WORKING WITH INTERFACES 304  Note In Chapter 9, you will learn about the role of generics and the System.Collections.Generic namespace. As you will see, this namespace contains generic versions of IEnumerable/IEnumerator that provide a more type-safe way to iterate over subobjects. Classes or structures that support this behavior advertise that they are able to expose contained subitems to the caller (in this example, the foreach keyword itself). Here is the definition of this standard .NET interface: // This interface informs the caller // that the object's subitems can be enumerated. public interface IEnumerable { IEnumerator GetEnumerator(); } As you can see, the GetEnumerator() method returns a reference to yet another interface named System.Collections.IEnumerator. This interface provides the infrastructure to allow the caller to traverse the internal objects contained by the IEnumerable-compatible container: // This interface allows the caller to // obtain a container's subitems. public interface IEnumerator { bool MoveNext (); // Advance the internal position of the cursor. object Current { get;} // Get the current item (read-only property). void Reset (); // Reset the cursor before the first member. } If you want to update the Garage type to support these interfaces, you could take the long road and implement each method manually. While you are certainly free to provide customized versions of GetEnumerator(), MoveNext(), Current, and Reset(), there is a simpler way. As the System.Array type (as well as many other collection classes) already implements IEnumerable and IEnumerator, you can simply delegate the request to the System.Array as follows: using System.Collections; ... public class Garage : IEnumerable { // System.Array already implements IEnumerator! private Car[] carArray = new Car[4]; public Garage() { carArray[0] = new Car(“FeeFee“, 200); carArray[1] = new Car(“Clunker“, 90); carArray[2] = new Car(“Zippy“, 30); carArray[3] = new Car(“Fred“, 30); } CHAPTER 8  WORKING WITH INTERFACES 305 public IEnumerator GetEnumerator() { // Return the array object's IEnumerator. return carArray.GetEnumerator(); } } After you have updated your Garage type, you can safely use the type within the C# foreach construct. Furthermore, given that the GetEnumerator() method has been defined publicly, the object user could also interact with the IEnumerator type: // Manually work with IEnumerator. IEnumerator i = carLot.GetEnumerator(); i.MoveNext(); Car myCar = (Car)i.Current; Console.WriteLine(“{0} is going {1} MPH“, myCar.PetName, myCar.CurrentSpeed); However, if you prefer to hide the functionality of IEnumerable from the object level, simply make use of explicit interface implementation: IEnumerator IEnumerable.GetEnumerator() { // Return the array object's IEnumerator. return carArray.GetEnumerator(); } By doing so, the casual object user will not find the Garage’s GetEnumerator() method, while the foreach construct will obtain the interface in the background when necessary.  Source Code The CustomEnumerator project is located under the Chapter 8 subdirectory. Building Iterator Methods with the yield Keyword In earlier versions of the .NET platform, when you wanted to build a custom collection (such as Garage) that supported foreach enumeration, implementing the IEnumerable interface (and possibly the IEnumerator interface) was your only option. However, there’s an alternative way to build types that work with the foreach loop via iterators. Simply put, an iterator is a member that specifies how a container’s internal items should be returned when processed by foreach. While the iterator method must still be named GetEnumerator(), and the return value must still be of type IEnumerator, your custom class does not need to implement any of the expected interfaces. To illustrate, create a new Console Application project named CustomEnumeratorWithYield and insert the Car, Radio, and Garage types from the previous example (again, renaming your namespace definitions to the current project if you like). Now, retrofit the current Garage type as follows: public class Garage : IEnumerator { private Car[] carArray = new Car[4]; ... // Iterator method. CHAPTER 8  WORKING WITH INTERFACES 306 public IEnumerator GetEnumerator() { foreach (Car c in carArray) { yield return c; } } } Notice that this implementation of GetEnumerator() iterates over the subitems using internal foreach logic and returns each Car to the caller using the yield return syntax. The yield keyword is used to specify the value (or values) to be returned to the caller’s foreach construct. When the yield return statement is reached, the current location in the container is stored, and execution is restarted from this location the next time the iterator is called. Iterator methods are not required to make use of the foreach keyword to return its contents. It is also permissible to define this iterator method as follows: public IEnumerator GetEnumerator() { yield return carArray[0]; yield return carArray[1]; yield return carArray[2]; yield return carArray[3]; } In this implementation, notice that the GetEnumerator() method is explicitly returning a new value to the caller with each pass through. Doing so for this example makes little sense, given that if we were to add more objects to the carArray member variable, our GetEnumerator() method would now be out of sync. Nevertheless, this syntax can be useful when you want to return local data from a method for processing by the foreach syntax. Building a Named Iterator It is also interesting to note that the yield keyword can technically be used within any method, regardless of its name. These methods (which are technically called named iterators) are also unique in that they can take any number of arguments. When building a named iterator, be very aware that the method will return the IEnumerable interface, rather than the expected IEnumerator-compatible type. To illustrate, you could add the following method to the Garage type: public IEnumerable GetTheCars(bool ReturnRevesed) { // Return the items in reverse. if (ReturnRevesed) { for (int i = carArray.Length; i != 0; i--) { yield return carArray[i-1]; } } else { // Return the items as placed in the array. CHAPTER 8  WORKING WITH INTERFACES 307 foreach (Car c in carArray) { yield return c; } } } Notice that the new method allows the caller to obtain the subitems in sequential order, as well as in reverse order, if the incoming parameter has the value true. You could now interact with our new method as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with the Yield Keyword *****\n“); Garage carLot = new Garage(); // Get items using GetEnumerator(). foreach (Car c in carLot) { Console.WriteLine(“{0} is going {1} MPH“, c.PetName, c.CurrentSpeed); } Console.WriteLine(); // Get items (in reverse!) using named iterator. foreach (Car c in carLot.GetTheCars(true)) { Console.WriteLine(“{0} is going {1} MPH“, c.PetName, c.CurrentSpeed); } Console.ReadLine(); } As you might agree, named iterators are helpful constructs, in that a single custom container can define multiple ways to request the returned set. So, to wrap up our look at building enumerable objects, remember that for your custom types to work with the C# foreach keyword, the container must define a method named GetEnumerator(), which has been formalized by the IEnumerable interface type. The implementation of this method is typically achieved by simply delegating it to the internal member that is holding onto the subobjects; however, it is also possible to make use of the yield return syntax to provide multiple “named iterator” methods.  Source Code The CustomEnumeratorWithYield project is located under the Chapter 8 subdirectory. CHAPTER 8  WORKING WITH INTERFACES 308 The ICloneable Interface As you might recall from Chapter 6, System.Object defines a method named MemberwiseClone(). This method is used to obtain a shallow copy of the current object. Object users do not call this method directly, as it is protected. However, a given object may call this method itself during the cloning process. To illustrate, create a new Console Application named CloneablePoint that defines a class named Point: // A class named Point. public class Point { public int X {get; set;} public int Y {get; set;} public Point(int xPos, int yPos) { X = xPos; Y = yPos;} public Point(){} // Override Object.ToString(). public override string ToString() { return string.Format(“X = {0}; Y = {1}“, X, Y ); } } Given what you already know about reference types and value types (see Chapter 4), you are aware that if you assign one reference variable to another, you have two references pointing to the same object in memory. Thus, the following assignment operation results in two references to the same Point object on the heap; modifications using either reference affect the same object on the heap: static void Main(string[] args) { Console.WriteLine(“***** Fun with Object Cloning *****\n“); // Two references to same object! Point p1 = new Point(50, 50); Point p2 = p1; p2.X = 0; Console.WriteLine(p1); Console.WriteLine(p2); Console.ReadLine(); } When you want to give your custom type the ability to return an identical copy of itself to the caller, you may implement the standard ICloneable interface. As shown at the start of this chapter, this type defines a single method named Clone(): public interface ICloneable { object Clone(); } Obviously, the implementation of the Clone() method varies among your classes. However, the basic functionality tends to be the same: copy the values of your member variables into a new object instance of the same type, and return it to the user. To illustrate, ponder the following update to the Point class: CHAPTER 8  WORKING WITH INTERFACES 309 // The Point now supports “clone-ability.“ public class Point : ICloneable { public int X { get; set; } public int Y { get; set; } public Point(int xPos, int yPos) { X = xPos; Y = yPos; } public Point() { } // Override Object.ToString(). public override string ToString() { return string.Format(“X = {0}; Y = {1}“, X, Y); } // Return a copy of the current object. public object Clone() { return new Point(this.X, this.Y); } } In this way, you can create exact stand-alone copies of the Point type, as illustrated by the following code: static void Main(string[] args) { Console.WriteLine(“***** Fun with Object Cloning *****\n“); // Notice Clone() returns a plain object type. // You must perform an explicit cast to obtain the derived type. Point p3 = new Point(100, 100); Point p4 = (Point)p3.Clone(); // Change p4.X (which will not change p3.X). p4.X = 0; // Print each object. Console.WriteLine(p3); Console.WriteLine(p4); Console.ReadLine(); } While the current implementation of Point fits the bill, you can streamline things just a bit. Because the Point type does not contain any internal reference type variables, you could simplify the implementation of the Clone() method as follows: public object Clone() { // Copy each field of the Point member by member. return this.MemberwiseClone(); } Be aware, however, that if the Point did contain any reference type member variables, MemberwiseClone() will copy the references to those objects (i.e., a shallow copy). If you want to support a true deep copy, you will need to create a new instance of any reference type variables during the cloning process. Let’s see an example next. CHAPTER 8  WORKING WITH INTERFACES 310 A More Elaborate Cloning Example Now assume the Point class contains a reference type member variable of type PointDescription. This class maintains a point’s friendly name as well as an identification number expressed as a System.Guid (a globally unique identifier [GUID] is a statistically unique 128-bit number). Here is the implementation: // This class describes a point. public class PointDescription { public string PetName {get; set;} public Guid PointID {get; set;} public PointDescription() { PetName = “No-name“; PointID = Guid.NewGuid(); } } The initial updates to the Point class itself included modifying ToString() to account for these new bits of state data, as well as defining and creating the PointDescription reference type. To allow the outside world to establish a pet name for the Point, you also update the arguments passed into the overloaded constructor: public class Point : ICloneable { public int X { get; set; } public int Y { get; set; } public PointDescription desc = new PointDescription(); public Point(int xPos, int yPos, string petName) { X = xPos; Y = yPos; desc.PetName = petName; } public Point(int xPos, int yPos) { X = xPos; Y = yPos; } public Point() { } // Override Object.ToString(). public override string ToString() { return string.Format(“X = {0}; Y = {1}; Name = {2};\nID = {3}\n“, X, Y, desc.PetName, desc.PointID); } // Return a copy of the current object. public object Clone() { return this.MemberwiseClone(); } } CHAPTER 8  WORKING WITH INTERFACES 311 Notice that you did not yet update your Clone() method. Therefore, when the object user asks for a clone using the current implementation, a shallow (member-by-member) copy is achieved. To illustrate, assume you have updated Main() as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Object Cloning *****\n“); Console.WriteLine(“Cloned p3 and stored new Point in p4“); Point p3 = new Point(100, 100, “Jane“); Point p4 = (Point)p3.Clone(); Console.WriteLine(“Before modification:“); Console.WriteLine(“p3: {0}“, p3); Console.WriteLine(“p4: {0}“, p4); p4.desc.PetName = “My new Point“; p4.X = 9; Console.WriteLine(“\nChanged p4.desc.petName and p4.X“); Console.WriteLine(“After modification:“); Console.WriteLine(“p3: {0}“, p3); Console.WriteLine(“p4: {0}“, p4); Console.ReadLine(); } Notice in the following output that while the value types have indeed been changed, the internal reference types maintain the same values, as they are “pointing” to the same objects in memory (specifically, note that the pet name for both objects is now “My new Point”). ***** Fun with Object Cloning ***** Cloned p3 and stored new Point in p4 Before modification: p3: X = 100; Y = 100; Name = Jane; ID = 133d66a7-0837-4bd7-95c6-b22ab0434509 p4: X = 100; Y = 100; Name = Jane; ID = 133d66a7-0837-4bd7-95c6-b22ab0434509 Changed p4.desc.petName and p4.X After modification: p3: X = 100; Y = 100; Name = My new Point; ID = 133d66a7-0837-4bd7-95c6-b22ab0434509 p4: X = 9; Y = 100; Name = My new Point; ID = 133d66a7-0837-4bd7-95c6-b22ab0434509 To have your Clone() method make a complete deep copy of the internal reference types, you need to configure the object returned by MemberwiseClone() to account for the current point’s name (the CHAPTER 8  WORKING WITH INTERFACES 312 System.Guid type is in fact a structure, so the numerical data is indeed copied). Here is one possible implementation: // Now we need to adjust for the PointDescription member. public object Clone() { // First get a shallow copy. Point newPoint = (Point)this.MemberwiseClone(); // Then fill in the gaps. PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return newPoint; } If you rerun the application once again and view the output (see below), you see that the Point returned from Clone() does copy its internal reference type member variables (note the pet name is now unique for both p3 and p4). ***** Fun with Object Cloning ***** Cloned p3 and stored new Point in p4 Before modification: p3: X = 100; Y = 100; Name = Jane; ID = 51f64f25-4b0e-47ac-ba35-37d263496406 p4: X = 100; Y = 100; Name = Jane; ID = 0d3776b3-b159-490d-b022-7f3f60788e8a Changed p4.desc.petName and p4.X After modification: p3: X = 100; Y = 100; Name = Jane; ID = 51f64f25-4b0e-47ac-ba35-37d263496406 p4: X = 9; Y = 100; Name = My new Point; ID = 0d3776b3-b159-490d-b022-7f3f60788e8a To summarize the cloning process, if you have a class or structure that contains nothing but value types, implement your Clone() method using MemberwiseClone(). However, if you have a custom type that maintains other reference types, you might want to create a new object that takes into account each reference type member variable, in order to get a “deep copy.”  Source Code The CloneablePoint project is located under the Chapter 8 subdirectory. CHAPTER 8  WORKING WITH INTERFACES 313 The IComparable Interface The System.IComparable interface specifies a behavior that allows an object to be sorted based on some specified key. Here is the formal definition: // This interface allows an object to specify its // relationship between other like objects. public interface IComparable { int CompareTo(object o); }  Note The generic version of this interface (IComparable) provides a more type-safe manner to handle comparisons between objects. You’ll examine generics in Chapter 9. Let’s assume you have a new Console Application named ComparableCar that defines the following updated Car class (notice that we have basically just added a new property to represent a unique ID for each car and a modified constructor): public class Car { ... public int CarID {get; set;} public Car(string name, int currSp, int id) { CurrentSpeed = currSp; PetName = name; CarID = id; } ... } Now assume you have an array of Car objects as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Object Sorting *****\n“); // Make an array of Car objects. Car[] myAutos = new Car[5]; myAutos[0] = new Car(“Rusty“, 80, 1); myAutos[1] = new Car(“Mary“, 40, 234); myAutos[2] = new Car(“Viper“, 40, 34); myAutos[3] = new Car(“Mel“, 40, 4); myAutos[4] = new Car(“Chucky“, 40, 5); Console.ReadLine(); } CHAPTER 8  WORKING WITH INTERFACES 314 The System.Array class defines a static method named Sort(). When you invoke this method on an array of intrinsic types (int, short, string, etc.), you are able to sort the items in the array in numeric/alphabetic order, as these intrinsic data types implement IComparable. However, what if you were to send an array of Car types into the Sort() method as follows? // Sort my cars? Not yet! Array.Sort(myAutos); If you run this test, you would get a runtime exception, as the Car class does not support the necessary interface. When you build custom types, you can implement IComparable to allow arrays of your types to be sorted. When you flesh out the details of CompareTo(), it will be up to you to decide what the baseline of the ordering operation will be. For the Car type, the internal CarID seems to be the logical candidate: // The iteration of the Car can be ordered // based on the CarID. public class Car : IComparable { ... // IComparable implementation. int IComparable.CompareTo(object obj) { Car temp = obj as Car; if (temp != null) { if (this.CarID > temp.CarID) return 1; if (this.CarID < temp.CarID) return -1; else return 0; } else throw new ArgumentException(“Parameter is not a Car!“); } } As you can see, the logic behind CompareTo() is to test the incoming object against the current instance based on a specific point of data. The return value of CompareTo() is used to discover whether this type is less than, greater than, or equal to the object it is being compared with (see Table 8-1). Table 8-1. CompareTo() Return Values CompareTo() Return Value Description Any number less than zero This instance comes before the specified object in the sort order. Zero This instance is equal to the specified object. Any number greater than zero This instance comes after the specified object in the sort order. CHAPTER 8  WORKING WITH INTERFACES 315 We can streamline the previous implementation of CompareTo() given the fact that the C# int data type (which is just a shorthand notation for the CLR System.Int32) implements IComparable. You could implement the Car’s CompareTo() as follows: int IComparable.CompareTo(object obj) { Car temp = obj as Car; if (temp != null) return this.CarID.CompareTo(temp.CarID); else throw new ArgumentException(“Parameter is not a Car!“); } In either case, so that your Car type understands how to compare itself to like objects, you can write the following user code: // Exercise the IComparable interface. static void Main(string[] args) { // Make an array of Car objects. ... // Display current array. Console.WriteLine(“Here is the unordered set of cars:“); foreach(Car c in myAutos) Console.WriteLine(“{0} {1}“, c.CarID, c.PetName); // Now, sort them using IComparable! Array.Sort(myAutos); Console.WriteLine(); // Display sorted array. Console.WriteLine(“Here is the ordered set of cars:“); foreach(Car c in myAutos) Console.WriteLine(“{0} {1}“, c.CarID, c.PetName); Console.ReadLine(); } Here is the output from the previous Main() method: ***** Fun with Object Sorting ***** Here is the unordered set of cars: 1 Rusty 234 Mary 34 Viper 4 Mel 5 Chucky Here is the ordered set of cars: 1 Rusty 4 Mel 5 Chucky CHAPTER 8  WORKING WITH INTERFACES 316 34 Viper 234 Mary Specifying Multiple Sort Orders with IComparer In this version of the Car type, you used the car’s ID as the base for the sort order. Another design might have used the pet name of the car as the basis for the sorting algorithm (to list cars alphabetically). Now, what if you wanted to build a Car that could be sorted by ID as well as by pet name? If this is the type of behavior you are interested in, you need to make friends with another standard interface named IComparer, defined within the System.Collections namespace as follows: // A general way to compare two objects. interface IComparer { int Compare(object o1, object o2); }  Note The generic version of this interface (IComparer) provides a more type-safe manner to handle comparisons between objects. You’ll examine generics in Chapter 9. Unlike the IComparable interface, IComparer is typically not implemented on the type you are trying to sort (i.e., the Car). Rather, you implement this interface on any number of helper classes, one for each sort order (pet name, car ID, etc.). Currently, the Car type already knows how to compare itself against other cars based on the internal car ID. Therefore, allowing the object user to sort an array of Car objects by pet name will require an additional helper class that implements IComparer. Here’s the code (be sure to import the System.Collections namespace in the code file): // This helper class is used to sort an array of Cars by pet name. public class PetNameComparer : IComparer { // Test the pet name of each object. int IComparer.Compare(object o1, object o2) { Car t1 = o1 as Car; Car t2 = o2 as Car; if(t1 != null && t2 != null) return String.Compare(t1.PetName, t2.PetName); else throw new ArgumentException(“Parameter is not a Car!“); } } The object user code is able to make use of this helper class. System.Array has a number of overloaded Sort() methods, one that just happens to take an object implementing IComparer. CHAPTER 8  WORKING WITH INTERFACES 317 static void Main(string[] args) { ... // Now sort by pet name. Array.Sort(myAutos, new PetNameComparer()); // Dump sorted array. Console.WriteLine(“Ordering by pet name:“); foreach(Car c in myAutos) Console.WriteLine(“{0} {1}“, c.CarID, c.PetName); ... } Custom Properties and Custom Sort Types It is worth pointing out that you can make use of a custom static property in order to help the object user along when sorting your Car types by a specific data point. Assume the Car class has added a static read- only property named SortByPetName that returns an instance of an object implementing the IComparer interface (PetNameComparer, in this case): // We now support a custom property to return // the correct IComparer interface. public class Car : IComparable { ... // Property to return the PetNameComparer. public static IComparer SortByPetName { get { return (IComparer)new PetNameComparer(); } } } The object user code can now sort by pet name using a strongly associated property, rather than just “having to know” to use the stand-alone PetNameComparer class type: // Sorting by pet name made a bit cleaner. Array.Sort(myAutos, Car.SortByPetName);  Source Code The ComparableCar project is located under the Chapter 8 subdirectory. Hopefully, at this point you not only understand how to define and implement your own interfaces, but also understand their usefulness. To be sure, interfaces are found within every major .NET namespace, and you will continue working with various standard interfaces over the remainder of this text. CHAPTER 8  WORKING WITH INTERFACES 318 Summary An interface can be defined as a named collection of abstract members. Because an interface does not provide any implementation details, it is common to regard an interface as a behavior that may be supported by a given type. When two or more classes implement the same interface, you can treat each type the same way (interface-based polymorphism) even if the types are defined within unique class hierarchies. C# provides the interface keyword to allow you to define a new interface. As you have seen, a type can support as many interfaces as necessary using a comma-delimited list. Furthermore, it is permissible to build interfaces that derive from multiple base interfaces. In addition to building your custom interfaces, the .NET libraries define a number of standard (i.e., framework-supplied) interfaces. As you have seen, you are free to build custom types that implement these predefined interfaces to gain a number of desirable traits such as cloning, sorting, and enumerating.P A R T I V 1 Advanced C# Programming C H A P T E R 9 321 Collections and Generics Any application you create with the .NET platform will need to contend with the issue of maintaining and manipulating a set of data points in memory. These data points can come from any variety of locations including a relational database, a local text file, an XML document, a web service call, or perhaps via user provided input. When the .NET platform was first released, programmers frequently used the classes of the System.Collections namespace to store and interact with bits of data used within an application. In .NET 2.0, the C# programming language was enhanced to support a feature termed generics; and with this change, a brand new namespace was introduced in the base class libraries: System.Collections.Generic. This chapter will provide you will an overview of the various collection (generic and nongeneric) namespaces and types found within the .NET base class libraries. As you will see, generic containers are often favored over their nongeneric counterparts because they typically provide greater type safety and performance benefits. After you’ve seen learned how to create and manipulate the generic items found in the framework, the remainder of this chapter will examine how to build your own generic methods and generic types. As you do this, you will learn about the role of constraints (and the corresponding C# where keyword), which allow you to build extremely type-safe classes. The Motivation for Collection Classes The most primitive container one could use to hold application data is undoubtedly the array. As you saw in Chapter 4, C# arrays allow you to define a set of identically typed items (including an array of System.Objects, which essentially represents an array of any type of data) of a fixed upper limit. Also recall from Chapter 4 that all C# array variables gather a good deal of functionality from the System.Array class. By way of a quick review, consider the following Main() method, which creates an array of textual data and manipulates its contents in various ways: static void Main(string[] args) { // Make an array of string data. string[] strArray = {“First“, “Second“, “Third“ }; // Show number of items in array using Length property. Console.WriteLine(“This array has {0} items.“, strArray.Length); Console.WriteLine(); // Display contents using enumerator. foreach (string s in strArray) { CHAPTER 9  COLLECTIONS AND GENERICS 322 Console.WriteLine(“Array Entry: {0}“, s); } Console.WriteLine(); // Reverse the array and print again. Array.Reverse(strArray); foreach (string s in strArray) { Console.WriteLine(“Array Entry: {0}“, s); } Console.ReadLine(); } While basic arrays can be useful to manage small amounts of fixed-size data, there are many other times where you require a more flexible data structure, such as a dynamically growing and shrinking container, or a container that can hold objects that meet only a specific criteria (e.g., only objects deriving from a specific base class or only objects implementing a particular interface). When you make use of a simple array, always remember they are “fixed size.” If you make an array of three items, you only get three items; therefore, the following code would result in a runtime exception (an IndexOutOfRangeException, to be exact): static void Main(string[] args) { // Make an array of string data. string[] strArray = { “First“, “Second“, “Third“ }; // Try to add a new item at the end?? Runtime error! strArray[3] = “new item?“; ... } To help overcome the limitations of a simple array, the .NET base class libraries ship with a number of namespaces containing collection classes. Unlike a simple C# array, collection classes are built to dynamically resize themselves on the fly as you insert or remove items. Moreover, many of the collection classes offer increased type safety and are highly optimized to process the contained data in a memory- efficient manner. As you read over this chapter, you will quickly notice that a collection class can belong to one of two broad categories: • Nongeneric collections (primarily found in the System.Collections namespace) • Generic collections (primarily found in the System.Collections.Generic namespace) Nongeneric collections are typically designed to operate on System.Object types and are, therefore, very loosely typed containers (however, some nongeneric collections do operate only on a specific type of data, such as string objects). In contrast, generic collections are much more type safe, given that you must specify the “type of type” they contain upon creation. As you will see, the telltail sign of any generic item is the “type parameter” marked with angled brackets (for example, List). We will examine the details of generics (including the many benefits they provide) a bit later in this chapter. For now, let’s examine some of the key nongeneric collection types in the System.Collections and System.Collections.Specialized namespaces. CHAPTER 9  COLLECTIONS AND GENERICS 323 The System.Collections Namespace When the .NET platform was first released, programmers frequently used the nongeneric collection classes found within the System.Collections namespace, which contains a set of classes used to manage and organize large amounts of in-memory data. Table 9-1 documents some of the more commonly used collection classes of this namespace, and the core interfaces they implement.  Note Any .NET application built with .NET 2.0 or higher should ignore the classes in System.Collections in favor of the corresponding classes in System.Collections.Generic. However, it is important to know the basics of the nongeneric collection classes, as you might have some legacy software to maintain. Table 9-1. Useful Types of System.Collections System.Collections Class Meaning in Life Key Implemented Interfaces ArrayList Represents a dynamically sized collection of objects listed in sequential order. IList, ICollection, IEnumerable, and ICloneable BitArray Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). ICollection, IEnumerable, and ICloneable Hashtable Represents a collection of key/value pairs that are organized based on the hash code of the key. IDictionary, ICollection, IEnumerable, and ICloneable Queue Represents a standard first-in, first- out (FIFO) collection of objects. ICollection, IEnumerable, and ICloneable SortedList Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. IDictionary, ICollection, IEnumerable, and ICloneable Stack A last-in, first-out (LIFO) stack providing push and pop (and peek) functionality. ICollection, IEnumerable, and ICloneable The interfaces implemented by these collection classes provide huge insights into their overall functionality. Table 9-2 documents the overall nature of these key interfaces, some of which you worked with firsthand in Chapter 8. CHAPTER 9  COLLECTIONS AND GENERICS 324 Table 9-2. Key Interfaces Supported by Classes of System.Collections System.Collections Interface Meaning in Life ICollection Defines general characteristics (e.g., size, enumeration, and thread safety) for all nongeneric collection types. ICloneable Allows the implementing object to return a copy of itself to the caller. IDictionary Allows a nongeneric collection object to represent its contents using key/value pairs. IEnumerable Returns an object implementing the IEnumerator interface (see next table entry). IEnumerator Enables foreach style iteration of collection items. IList Provides behavior to add, remove, and index items in a sequential list of objects. An Illustrative Example: Working with the ArrayList Based on your experience, you might have some firsthand experience using (or implementing) some of these classic data structures such as stacks, queues, and lists. If this is not the case, I’ll provide some further details on their differences when we examine their generic counterparts a bit later in this chapter. Until then, here is a Main() method making use of an ArrayList object. Notice that we can add (or remove) items on the fly and the container automatically resizes itself accordingly: // You must import System.Collections to access the ArrayList. static void Main(string[] args) { ArrayList strArray = new ArrayList(); strArray.AddRange(new string[] { “First“, “Second“, “Third“ }); // Show number of items in ArrayList. Console.WriteLine(“This collection has {0} items.“, strArray.Count); Console.WriteLine(); // Add a new item and display current count. strArray.Add(“Fourth!“); Console.WriteLine(“This collection has {0} items.“, strArray.Count); // Display contents. foreach (string s in strArray) { Console.WriteLine(“Entry: {0}“, s); } Console.WriteLine(); } CHAPTER 9  COLLECTIONS AND GENERICS 325 As you would guess, the ArrayList class has many useful members beyond the Count property and AddRange() and Add() methods, so be sure you consult the .NET Framework documentation for full details. On a related note, the other classes of System.Collections (Stack, Queue, and so on) are also fully documented in the .NET help system. However, it is very important to point out that a majority of your .NET projects will most likely not make use of the collection classes in the System.Collections namespace! To be sure, these days it is far more common to make use of the generic counterpart classes found in the System.Collections.Generic namespace. Given this point, I won’t comment on (or provide code examples for) the remaining nongeneric classes found in System.Collections. A Survey of System.Collections.Specialized Namespace System.Collections is not the only .NET namespace that contains nongeneric collection classes. For example, the System.Collections.Specialized namespace defines a number of (pardon the redundancy) specialized collection types. Table 9-3 documents some of the more useful types in this particular collection-centric namespace, all of which are nongeneric. Table 9-3. Useful Classes of System.Collections.Specialized System.Collections.Specialized Type Meaning in Life HybridDictionary This class implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large. ListDictionary This class is useful when you need to manage a small number of items (10 or so) that can change over time. This class makes use of a singly linked list to maintain its data. StringCollection This class provides an optimal way to manage large collections of string data. BitVector32 This class provides a simple structure that stores Boolean values and small integers in 32 bits of memory. Beyond these concrete types, this namespace also contains many additional interfaces and abstract base classes that you can use as a starting point for creating custom collection classes. While these “specialized” types might be just what your projects require in some situations, I won’t comment on their usage here. Again, in many cases, you will likely find that the System.Collections.Generic namespace provides classes with similar functionality and additional benefits. CHAPTER 9  COLLECTIONS AND GENERICS 326  Note There are two additional collection-centric namespaces (System.Collections.ObjectModel and System.Collections.Concurrent) in the .NET base class libraries. You will examine the former namespace later in this chapter, after you are comfortable with the topic of generics. System.Collections.Concurrent provides thread-safe collection classes (see Chapter 19 for information on multithreading). The Problems of Nongeneric Collections While it is true that many successful .NET applications have been built over the years using these nongeneric collection classes (and interfaces), history has shown that use of these types can result in a number of issues. The first issue is that using the System.Collections and System.Collections.Specialized classes can result in some poorly performing code, especially when you are manipulating numerical data (e.g., value types). As you’ll see momentarily, the CLR must perform a number of memory transfer operations when you store structures in any nongeneric collection class prototyped to operate on System.Objects, which can hurt runtime execution speed. The second issue is that most of the nongeneric collection classes are not type safe because (again) they were developed to operate on System.Objects, and they could therefore contain anything at all. If a .NET developer needed to create a highly type-safe collection (e.g., a container that can hold objects implementing only a certain interface), the only real choice was to create a brand new collection class by hand. Doing so was not too labor intensive, but it was a tad bit on the tedious side. Before you look at how to use generics in your programs, you’ll find it helpful to examine the issues of nongeneric collection classes a bit closer; this will help you better understand the problems generics intend to solve in the first place. If you want to follow along, create a new Console Application named IssuesWithNonGenericCollections. Next, make sure you import the System.Collections namespace to the top of your C# code file: using System.Collections; The Issue of Performance As you might recall from Chapter 4, the .NET platform supports two broad categories of data: value types and reference types. Given that .NET defines two major categories of types, you might occasionally need to represent a variable of one category as a variable of the other category. To do so, C# provides a simple mechanism, termed boxing, to store the data in a value type within a reference variable. Assume that you have created a local variable of type int in a method called SimpleBoxUnboxOperation(). If, during the course of your application, you were to represent this value type as a reference type, you would box the value, as follows: static void SimpleBoxUnboxOperation() { // Make a ValueType (int) variable. int myInt = 25; // Box the int into an object reference. object boxedInt = myInt; CHAPTER 9  COLLECTIONS AND GENERICS 327 } Boxing can be formally defined as the process of explicitly assigning a value type to a System.Object variable. When you box a value, the CLR allocates a new object on the heap and copies the value type’s value (25, in this case) into that instance. What is returned to you is a reference to the newly allocated heap-based object. The opposite operation is also permitted through unboxing. Unboxing is the process of converting the value held in the object reference back into a corresponding value type on the stack. Syntactically speaking, an unboxing operation looks like a normal casting operation. However, the semantics are quite different. The CLR begins by verifying that the receiving data type is equivalent to the boxed type; and if so, it copies the value back into a local stack-based variable. For example, the following unboxing operations work successfully, given that the underlying type of the boxedInt is indeed an int: static void SimpleBoxUnboxOperation() { // Make a ValueType (int) variable. int myInt = 25; // Box the int into an object reference. object boxedInt = myInt; // Unbox the reference back into a corresponding int. int unboxedInt = (int)boxedInt; } When the C# compiler encounters boxing/unboxing syntax, it emits CIL code that contains the box/unbox op codes. If you were to examine your compiled assembly using ildasm.exe, you would find the following: .method private hidebysig static void SimpleBoxUnboxOperation() cil managed { // Code size 19 (0x13) .maxstack 1 .locals init ([0] int32 myInt, [1] object boxedInt, [2] int32 unboxedInt) IL_0000: nop IL_0001: ldc.i4.s 25 IL_0003: stloc.0 IL_0004: ldloc.0 IL_0005: box [mscorlib]System.Int32 IL_000a: stloc.1 IL_000b: ldloc.1 IL_000c: unbox.any [mscorlib]System.Int32 IL_0011: stloc.2 IL_0012: ret } // end of method Program::SimpleBoxUnboxOperation Remember that unlike when performing a typical cast, you must unbox into an appropriate data type. If you attempt to unbox a piece of data into the incorrect data type, an InvalidCastException exception will be thrown. To be perfectly safe, you should wrap each unboxing operation in try/catch logic; however, this would be quite labor intensive to do for every unboxing operation. Consider the following code update, which will throw an error because you’re attempting to unbox the boxed int into a long: bCHAPTER 9  COLLECTIONS AND GENERICS 328 static void SimpleBoxUnboxOperation() { // Make a ValueType (int) variable. int myInt = 25; // Box the int into an object reference. object boxedInt = myInt; // Unbox in the wrong data type to trigger // runtime exception. try { long unboxedInt = (long)boxedInt; } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } } At first glance, boxing/unboxing might seem like a rather uneventful language feature that is more academic than practical. After all, you will seldom need to store a local value type in a local object variable, as seen here. However, it turns out that the boxing/unboxing process is quite helpful because it allows you to assume everything can be treated as a System.Object, while the CLR takes care of the memory-related details on your behalf. Let’s look at a practical use of these techniques. Assume you have created a nongeneric System.Collections.ArrayList to hold onto a batch of numeric (stack-allocated) data. If you were to examine the members of ArrayList, you would find they are prototyped to operate on System.Object data. Now consider the Add(), Insert(), and Remove() methods, as well as the class indexer: public class ArrayList : object, IList, ICollection, IEnumerable, ICloneable { ... public virtual int Add(object value); public virtual void Insert(int index, object value); public virtual void Remove(object obj); public virtual object this[int index] {get; set; } } ArrayList has been built to operate on objects, which represent data allocated on the heap, so it might seem strange that the following code compiles and executes without throwing an error: static void WorkWithArrayList() { // Value types are automatically boxed when // passed to a method requesting an object. ArrayList myInts = new ArrayList(); myInts.Add(10); myInts.Add(20); myInts.Add(35); } CHAPTER 9  COLLECTIONS AND GENERICS 329 Although you pass in numerical data directly into methods requiring an object, the runtime automatically boxes the stack-based data on your behalf. Later, if you want to retrieve an item from the ArrayList using the type indexer, you must unbox the heap-allocated object into a stack-allocated integer using a casting operation. Remember that the indexer of the ArrayList is returning System.Objects, not System.Int32s: static void WorkWithArrayList() { // Value types are automatically boxed when // passed to a member requesting an object. ArrayList myInts = new ArrayList(); myInts.Add(10); myInts.Add(20); myInts.Add(35); // Unboxing occurs when a object is converted back to // stack-based data. int i = (int)myInts[0]; // Now it is reboxed, as WriteLine() requires object types! Console.WriteLine(“Value of your int: {0}“, i); } Again, note that the stack-allocated System.Int32 is boxed prior to the call to ArrayList.Add(), so it can be passed in the required System.Object. Also note that the System.Object is unboxed back into a System.Int32 once it is retrieved from the ArrayList via the casting operation, only to be boxed again when it is passed to the Console.WriteLine() method, as this method is operating on System.Object variables. Boxing and unboxing are convenient from a programmer’s viewpoint, but this simplified approach to stack/heap memory transfer comes with the baggage of performance issues (in both speed of execution and code size) and a lack of type safety. To understand the performance issues, ponder the steps that must occur to box and unbox a simple integer. 1. A new object must be allocated on the managed heap. 2. The value of the stack-based data must be transferred into that memory location. 3. When unboxed, the value stored on the heap-based object must be transferred back to the stack. 4. The now unused object on the heap will (eventually) be garbage collected. Although this particular WorkWithArrayList() method won’t cause a major bottleneck in terms of performance, you could certainly feel the impact if an ArrayList contained thousands of integers that your program manipulates on a somewhat regular basis. In an ideal world, you could manipulate stack- based data in a container without any performance issues. Ideally, it would be nice if you did not have to have to bother plucking data from this container using try/catch scopes (this is exactly what generics let you achieve). CHAPTER 9  COLLECTIONS AND GENERICS 330 The Issue of Type Safety We touched on the issue of type safety when we looked at unboxing operations. Recall that you must unbox your data into the same data type it was declared as before boxing. However, there is another aspect of type safety you must keep in mind in a generic-free world: the fact that a majority of the classes of System.Collections can typically hold anything whatsoever because their members are prototyped to operate on System.Objects. For example, this method builds an ArrayList of random bits of unrelated data: static void ArrayListOfRandomObjects() { // The ArrayList can hold anything at all. ArrayList allMyObjects = new ArrayList(); allMyObjects.Add(true); allMyObjects.Add(new OperatingSystem(PlatformID.MacOSX, new Version(10, 0))); allMyObjects.Add(66); allMyObjects.Add(3.14); } In some cases, you will require an extremely flexible container that can hold literally anything (as seen here). However, most of the time you desire a type-safe container that can operate only on a particular type of data point. For example, you might need a container that can hold only database connections, bitmaps, or IPointy-compatible objects. Prior to generics, the only way you could address this issue of type safety was to create a custom (strongly typed) collection class manually. Assume you wish to create a custom collection that can contain only objects of type Person: public class Person { public int Age {get; set;} public string FirstName {get; set;} public string LastName {get; set;} public Person(){} public Person(string firstName, string lastName, int age) { Age = age; FirstName = firstName; LastName = lastName; } public override string ToString() { return string.Format(“Name: {0} {1}, Age: {2}“, FirstName, LastName, Age); } } To build a collection which can hold only Person objects, you could define a System.Collections.ArrayList member variable within a class named PersonCollection and configure all members to operate on strongly typed Person objects, rather than on System.Object types. Here is a simple example (a production-level custom collection could support many additional members and CHAPTER 9  COLLECTIONS AND GENERICS 331 might extend an abstract base class from the System.Collections or System.Collections.Specialized namespace): public class PersonCollection : IEnumerable { private ArrayList arPeople = new ArrayList(); // Cast for caller. public Person GetPerson(int pos) { return (Person)arPeople[pos]; } // Insert only Person objects. public void AddPerson(Person p) { arPeople.Add(p); } public void ClearPeople() { arPeople.Clear(); } public int Count { get { return arPeople.Count; } } // Foreach enumeration support. IEnumerator IEnumerable.GetEnumerator() { return arPeople.GetEnumerator(); } } Notice that the PersonCollection class implements the IEnumerable interface, which allows a foreach-like iteration over each contained item. Also notice that your GetPerson() and AddPerson() methods have been prototyped to operate only on Person objects, not bitmaps, strings, database connections, or other items. With these types defined, you are now assured of type safety, given that the C# compiler will be able to determine any attempt to insert an incompatible data type: static void UsePersonCollection() { Console.WriteLine(“***** Custom Person Collection *****\n“); PersonCollection myPeople = new PersonCollection(); myPeople.AddPerson(new Person(“Homer“, “Simpson“, 40)); myPeople.AddPerson(new Person(“Marge“, “Simpson“, 38)); myPeople.AddPerson(new Person(“Lisa“, “Simpson“, 9)); myPeople.AddPerson(new Person(“Bart“, “Simpson“, 7)); myPeople.AddPerson(new Person(“Maggie“, “Simpson“, 2)); // This would be a compile-time error! // myPeople.AddPerson(new Car()); foreach (Person p in myPeople) Console.WriteLine(p); } While custom collections do ensure type safety, this approach leaves you in a position where you must create an (almost identical) custom collection for each unique data type you want to contain. Thus, if you need a custom collection that can operate only on classes deriving from the Car base class, you need to build a highly similar collection class: CHAPTER 9  COLLECTIONS AND GENERICS 332 public class CarCollection : IEnumerable { private ArrayList arCars = new ArrayList(); // Cast for caller. public Car GetCar(int pos) { return (Car) arCars[pos]; } // Insert only Car objects. public void AddCar(Car c) { arCars.Add(c); } public void ClearCars() { arCars.Clear(); } public int Count { get { return arCars.Count; } } // Foreach enumeration support. IEnumerator IEnumerable.GetEnumerator() { return arCars.GetEnumerator(); } } However, a custom collection class does nothing to solve the issue of boxing/unboxing penalties. Even if you were to create a custom collection named IntCollection that you designed to operate only on System.Int32 items, you would have to allocate some type of object to hold the data (e.g., System.Array and ArrayList): public class IntCollection : IEnumerable { private ArrayList arInts = new ArrayList(); // Get an int (performs unboxing!). public int GetInt(int pos) { return (int)arInts[pos]; } // Insert an int (performs boxing)! public void AddInt(int i) { arInts.Add(i); } public void ClearInts() { arInts.Clear(); } public int Count { get { return arInts.Count; } } IEnumerator IEnumerable.GetEnumerator() { return arInts.GetEnumerator(); } } Regardless of which type you might choose to hold the integers, you cannot escape the boxing dilemma using nongeneric containers. CHAPTER 9  COLLECTIONS AND GENERICS 333 A First Look at Generic Collections When you use generic collection classes, you rectify all of the previous issues, including boxing/unboxing penalties and a lack of type safety. Also, the need to build a custom (generic) collection class becomes quite rare. Rather than having to build unique classes that can contain people, cars, and integers, you can use a generic collection class and specify the type of type. Consider the following method, which uses the generic List class (in the System.Collections.Generic namespace) to contain various types of data in a strongly typed manner (don’t fret the details of generic syntax at this time): static void UseGenericList() { Console.WriteLine(“***** Fun with Generics *****\n“); // This List<> can hold only Person objects. List morePeople = new List(); morePeople.Add(new Person (“Frank“, “Black“, 50)); Console.WriteLine(morePeople[0]); // This List<> can hold only integers. List moreInts = new List(); moreInts.Add(10); moreInts.Add(2); int sum = moreInts[0] + moreInts[1]; // Compile-time error! Can't add Person object // to a list of ints! // moreInts.Add(new Person()); } The first List object can contain only Person objects. Therefore, you do not need to perform a cast when plucking the items from the container, which makes this approach more type safe. The second List can contain only integers, all of which are allocated on the stack; in other words, there is no hidden boxing or unboxing as you found with the nongeneric ArrayList. Here is a short list of the benefits generic containers provide over their nongeneric counterparts. • Generics provide better performance because they do not result in boxing or unboxing penalties when storing value types. • Generics are type safe because they can contain only the type of type you specify. • Generics greatly reduce the need to build custom collection types because you specify the “type of type” when creating the generic container.  Source Code You can find the IssuesWithNonGenericCollections project under the Chapter 9 subdirectory. CHAPTER 9  COLLECTIONS AND GENERICS 334 The Role of Generic Type Parameters You can find generic classes, interfaces, structures, and delegates throughout the .NET base class libraries, and these might be part of any .NET namespace. Also be very aware that generics have far more uses then simply defining a collection class. To be sure, you will see many different generics used in the remainder of this book for various reasons.  Note Only classes, structures, interfaces, and delegates can be written generically; enum types cannot. When you see a generic item listed in the .NET Framework documentation or the Visual Studio object browser, you will notice a pair of angled brackets with a letter or other token sandwiched within. Figure 9-1 shows the Visual Studio object browser displaying a number of generic items located within the System.Collections.Generic namespace, including the highlighted List class. Figure 9-1. Generic items supporting type parameters CHAPTER 9  COLLECTIONS AND GENERICS 335 Formally speaking, you call these tokens type parameters; however, in more user-friendly terms, you can simply call them placeholders. You can read the symbol as “of T.” Thus, you can read IEnumerable as IEnumerable of T; or, to say it another way, IEnumerable of type T.  Note The name of a type parameter (placeholder) is irrelevant, and it is up to the developer who created the generic item. However, typically T is used to represent types, TKey or K is used for keys, and TValue or V is used for values. When you create a generic object, implement a generic interface, or invoke a generic member, it is up to you to supply a value to the type parameter. You’ll see many examples in this chapter, and throughout the remainder of the text. However, to set the stage, let’s see the basics of interacting with generic types and members. Specifying Type Parameters for Generic Classes/Structures When you create an instance of a generic class or structure, you specify the type parameter when you declare the variable and when you invoke the constructor. As you saw in the preceding code example, UseGenericList() defined two List objects: // This List<> can hold only Person objects. List morePeople = new List(); You can read the preceding snippet as a List<> of T, where T is of type Person. Or, more simply, you can read it as a list of person objects. After you specify the type parameter of a generic item, it cannot be changed (remember: generics are all about type safety). When you specify a type parameter for a generic class or structure, all occurrences of the placeholder(s) are now replaced with your supplied value. If you were to view the full declaration of the generic List class using the Visual Studio object browser, you would see that the placeholder T is used throughout the definition of the List type. Here is a partial listing (note the items in bold): // A partial listing of the List class. namespace System.Collections.Generic { public class List : IList, ICollection, IEnumerable, IReadOnlyList IList, ICollection, IEnumerable { ... public void Add(T item); public ReadOnlyCollection AsReadOnly(); public int BinarySearch(T item); public bool Contains(T item); public void CopyTo(T[] array); public int FindIndex(System.Predicate match); public T FindLast(System.Predicate match); public bool Remove(T item); public int RemoveAll(System.Predicate match); public T[] ToArray(); public bool TrueForAll(System.Predicate match); CHAPTER 9  COLLECTIONS AND GENERICS 336 public T this[int index] { get; set; } } } When you create a List specifying Person objects, it is as if the List type were defined as follows: namespace System.Collections.Generic { public class List : IList, ICollection, IEnumerable, IReadOnlyList IList, ICollection, IEnumerable { ... public void Add(Person item); public ReadOnlyCollection AsReadOnly(); public int BinarySearch(Person item); public bool Contains(Person item); public void CopyTo(Person[] array); public int FindIndex(System.Predicate match); public Person FindLast(System.Predicate match); public bool Remove(Person item); public int RemoveAll(System.Predicate match); public Person[] ToArray(); public bool TrueForAll(System.Predicate match); public Person this[int index] { get; set; } } } Of course, when you create a generic List variable, the compiler does not literally create a brand new implementation of the List class. Rather, it will address only the members of the generic type you actually invoke. Specifying Type Parameters for Generic Members It is fine for a nongeneric class or structure to support a handful of generic members (e.g., methods and properties). In these cases, you would also need to specify the placeholder value at the time you invoke the method. For example, System.Array supports a several generic methods. Specifically, the nongeneric static Sort() method now has a generic counterpart named Sort(). Consider the following code snippet, where T is of type int: int[] myInts = { 10, 4, 2, 33, 93 }; // Specify the placeholder to the generic // Sort<>() method. Array.Sort(myInts); foreach (int i in myInts) { Console.WriteLine(i); } CHAPTER 9  COLLECTIONS AND GENERICS 337 Specifying Type Parameters for Generic Interfaces It is common to implement generic interfaces when you build classes or structures that need to support various framework behaviors (e.g., cloning, sorting, and enumeration). In Chapter 8, you learned about a number of nongeneric interfaces, such as IComparable, IEnumerable, IEnumerator, and IComparer. Recall that the nongeneric IComparable interface was defined like this: public interface IComparable { int CompareTo(object obj); } In Chapter 8, you also implemented this interface on your Car class to enable sorting in a standard array. However, the code required several runtime checks and casting operations because the parameter was a general System.Object: public class Car : IComparable { ... // IComparable implementation. int IComparable.CompareTo(object obj) { Car temp = obj as Car; if (temp != null) { if (this.CarID > temp.CarID) return 1; if (this.CarID < temp.CarID) return -1; else return 0; } else throw new ArgumentException(“Parameter is not a Car!“); } } Now assume you use the generic counterpart of this interface: public interface IComparable { int CompareTo(T obj); } In this case, your implementation code will be cleaned up considerably: public class Car : IComparable { ... // IComparable implementation. int IComparable.CompareTo(Car obj) { if (this.CarID > obj.CarID) return 1; CHAPTER 9  COLLECTIONS AND GENERICS 338 if (this.CarID < obj.CarID) return -1; else return 0; } } Here, you do not need to check whether the incoming parameter is a Car because it can only be a Car! If someone were to pass in an incompatible data type, you would get a compile-time error. Now that you have a better handle on how to interact with generic items, as well as the role of type parameters (a.k.a. placeholders), you’re ready to examine the classes and interfaces of the System.Collections.Generic namespace. The System.Collections.Generic Namespace When you are building a .NET application and need a way to manage in-memory data, the classes of System.Collections.Generic will most likely fit the bill. At the opening of this chapter, I briefly mentioned some of the core nongeneric interfaces implemented by the nongeneric collection classes. Not too surprisingly, the System.Collections.Generic namespace defines generic replacements for many of them. In fact, you can find a number of the generic interfaces that extend their nongeneric counterparts. This might seem odd; however, by doing so, implementing classes will also support the legacy functionally found in their nongeneric siblings. For example, IEnumerable extends IEnumerable. Table 9-4 documents the core generic interfaces you’ll encounter when working with the generic collection classes. Table 9-4. Key Interfaces Supported by Classes of System.Collections.Generic System.Collections.Generic Interface Meaning in Life ICollection Defines general characteristics (e.g., size, enumeration, and thread safety) for all generic collection types. IComparer Defines a way to compare to objects. IDictionary Allows a generic collection object to represent its contents using key/value pairs. IEnumerable Returns the IEnumerator interface for a given object. IEnumerator Enables foreach-style iteration over a generic collection. IList Provides behavior to add, remove, and index items in a sequential list of objects. ISet Provides the base interface for the abstraction of sets. CHAPTER 9  COLLECTIONS AND GENERICS 339 The System.Collections.Generic namespace also defines several classes that implement many of these key interfaces. Table 9-5 describes some commonly used classes of this namespace, the interfaces they implement, and their basic functionality. Table 9-5. Classes of System.Collections.Generic Generic Class Supported Key Interfaces Meaning in Life Dictionary ICollection, IDictionary, IEnumerable This represents a generic collection of keys and values. LinkedList ICollection, IEnumerable This represents a doubly linked list. List ICollection, IEnumerable, IList This is a dynamically resizable sequential list of items. Queue ICollection (Not a typo! This is the nongeneric collection interface) , IEnumerable This is a generic implementation of a first-in, first-out (FIFO) list. SortedDictionary ICollection, IDictionary, IEnumerable This is a generic implementation of a sorted set of key/value pairs. SortedSet ICollection, IEnumerable, ISet This represents a collection of objects that is maintained in sorted order with no duplication. Stack ICollection (Not a typo! This is the nongeneric collection interface) , IEnumerable This is a generic implementation of a last-in, first-out (LIFO) list. The System.Collections.Generic namespace also defines many auxiliary classes and structures that work in conjunction with a specific container. For example, the LinkedListNode type represents a node within a generic LinkedList, the KeyNotFoundException exception is raised when attempting to grab an item from a container using a nonexistent key, and so forth. It is also worth pointing out that mscorlib.dll and System.dll are not the only assemblies that add new types to the System.Collections.Generic namespace. For example, System.Core.dll adds the HashSet class to the mix. Be sure to consult the .NET Framework documentation for full details of the System.Collections.Generic namespace. In any case, your next task is to learn how to use some of these generic collection classes. Before you do however, allow me to illustrate a C# language feature (first introduced in .NET 3.5) that simplifies the way you populate generic (and nongeneric) collection containers with data. CHAPTER 9  COLLECTIONS AND GENERICS 340 Understanding Collection Initialization Syntax In Chapter 4, you learned about object initialization syntax, which allows you to set properties on a new variable at the time of construction. Closely related to this is collection initialization syntax. This C# language feature makes it possible to populate many containers (such as ArrayList or List) with items by using syntax similar to what you use to populate a basic array.  Note You can apply collection initialization syntax only to classes that support an Add() method, which is formalized by the ICollection/ICollection interfaces. Consider the following examples: // Init a standard array. int[] myArrayOfInts = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Init a generic List<> of ints. List myGenericList = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Init an ArrayList with numerical data. ArrayList myList = new ArrayList { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; If your container is managing a collection of classes or a structure, you can blend object initialization syntax with collection initialization syntax to yield some functional code. You might recall the Point class from Chapter 5, which defined two properties named X and Y. If you wanted to build a generic List of Point objects, you could write the following: List myListOfPoints = new List { new Point { X = 2, Y = 2 }, new Point { X = 3, Y = 3 }, new Point(PointColor.BloodRed){ X = 4, Y = 4 } }; foreach (var pt in myListOfPoints) { Console.WriteLine(pt); } Again, the benefit of this syntax is that you save yourself numerous keystrokes. While the nested curly brackets can become difficult to read if you don’t mind your formatting, imagine the amount of code that would be required to fill the following List of Rectangles if you did not have collection initialization syntax (you might recall from Chapter 4 that you created a Rectangle class that contained two properties encapsulating Point objects): List myListOfRects = new List { new Rectangle {TopLeft = new Point { X = 10, Y = 10 }, BottomRight = new Point { X = 200, Y = 200}}, new Rectangle {TopLeft = new Point { X = 2, Y = 2 }, CHAPTER 9  COLLECTIONS AND GENERICS 341 BottomRight = new Point { X = 100, Y = 100}}, new Rectangle {TopLeft = new Point { X = 5, Y = 5 }, BottomRight = new Point { X = 90, Y = 75}} }; foreach (var r in myListOfRects) { Console.WriteLine(r); } Working with the List Class Create a brand new Console Application project named FunWithGenericCollections. Note that your initial C# code file already imports the System.Collections.Generic namespace. The first generic class you will examine is List, which you’ve already seen once or twice in this chapter. The List class is bound to be your most frequently used type in the System.Collections.Generic namespace because it allows you to resize the contents of the container dynamically. To illustrate the basics of this type, ponder the following method in your Program class, which leverages List to manipulate the set of Person objects seen earlier in this chapter; you might recall that these Person objects defined three properties (Age, FirstName, and LastName) and a custom ToString() implementation: static void UseGenericList() { // Make a List of Person objects, filled with // collection/object init syntax. List people = new List() { new Person {FirstName= “Homer“, LastName=“Simpson“, Age=47}, new Person {FirstName= “Marge“, LastName=“Simpson“, Age=45}, new Person {FirstName= “Lisa“, LastName=“Simpson“, Age=9}, new Person {FirstName= “Bart“, LastName=“Simpson“, Age=8} }; // Print out # of items in List. Console.WriteLine(“Items in list: {0}“, people.Count); // Enumerate over list. foreach (Person p in people) Console.WriteLine(p); // Insert a new person. Console.WriteLine(“\n->Inserting new person.“); people.Insert(2, new Person { FirstName = “Maggie“, LastName = “Simpson“, Age = 2 }); Console.WriteLine(“Items in list: {0}“, people.Count); // Copy data into a new array. Person[] arrayOfPeople = people.ToArray(); for (int i = 0; i < arrayOfPeople.Length; i++) { Console.WriteLine(“First Names: {0}“, arrayOfPeople[i].FirstName); } CHAPTER 9  COLLECTIONS AND GENERICS 342 } Here, you use initialization syntax to populate your List with objects, as a shorthand notation for calling Add() multiple times. After you print out the number of items in the collection (as well as enumerate over each item), you invoke Insert(). As you can see, Insert() allows you to plug a new item into the List at a specified index. Finally, notice the call to the ToArray() method, which returns an array of Person objects based on the contents of the original List. From this array, you loop over the items again using the array’s indexer syntax. If you call this method from within Main(), you get the following output: ***** Fun with Generic Collections ***** Items in list: 4 Name: Homer Simpson, Age: 47 Name: Marge Simpson, Age: 45 Name: Lisa Simpson, Age: 9 Name: Bart Simpson, Age: 8 ->Inserting new person. Items in list: 5 First Names: Homer First Names: Marge First Names: Maggie First Names: Lisa First Names: Bart The List class defines many additional members of interest, so be sure to consult the .NET Framework documentation for more information. Next, let’s look at a few more generic collections, specifically Stack, Queue, and SortedSet. This should get you in a great position to understand your basic choices regarding how to hold your custom application data. Working with the Stack Class The Stack class represents a collection that maintains items using a last-in, first-out manner. As you might expect, Stack defines members named Push() and Pop() to place items onto or remove items from the stack. The following method creates a stack of Person objects: static void UseGenericStack() { Stack stackOfPeople = new Stack(); stackOfPeople.Push(new Person { FirstName = “Homer“, LastName = “Simpson“, Age = 47 }); stackOfPeople.Push(new Person { FirstName = “Marge“, LastName = “Simpson“, Age = 45 }); stackOfPeople.Push(new Person { FirstName = “Lisa“, LastName = “Simpson“, Age = 9 }); CHAPTER 9  COLLECTIONS AND GENERICS 343 // Now look at the top item, pop it, and look again. Console.WriteLine(“First person is: {0}“, stackOfPeople.Peek()); Console.WriteLine(“Popped off {0}“, stackOfPeople.Pop()); Console.WriteLine(“\nFirst person is: {0}“, stackOfPeople.Peek()); Console.WriteLine(“Popped off {0}“, stackOfPeople.Pop()); Console.WriteLine(“\nFirst person item is: {0}“, stackOfPeople.Peek()); Console.WriteLine(“Popped off {0}“, stackOfPeople.Pop()); try { Console.WriteLine(“\nnFirst person is: {0}“, stackOfPeople.Peek()); Console.WriteLine(“Popped off {0}“, stackOfPeople.Pop()); } catch (InvalidOperationException ex) { Console.WriteLine(“\nError! {0}“, ex.Message); } } Here, you build a stack that contains three people, added in the order of their first names: Homer, Marge, and Lisa. As you peek into the stack, you will always see the object at the top first; therefore, the first call to Peek() reveals the third Person object. After a series of Pop() and Peek() calls, the stack eventually empties, at which time additional Peek() and Pop() calls raise a system exception. You can see the output for this here: ***** Fun with Generic Collections ***** First person is: Name: Lisa Simpson, Age: 9 Popped off Name: Lisa Simpson, Age: 9 First person is: Name: Marge Simpson, Age: 45 Popped off Name: Marge Simpson, Age: 45 First person item is: Name: Homer Simpson, Age: 47 Popped off Name: Homer Simpson, Age: 47 Error! Stack empty. Working with the Queue Class Queues are containers that ensure items are accessed in a first-in, first-out manner. Sadly, we humans are subject to queues all day long: lines at the bank, lines at the movie theater, and lines at the morning coffeehouse. When you need to model a scenario in which items are handled on a first-come, first- served basis, you will find the Queue class fits the bill. In addition to the functionality provided by the supported interfaces, Queue defines the key members shown in Table 9-6. CHAPTER 9  COLLECTIONS AND GENERICS 344 Table 9-6. Members of the Queue Type Select Member of Queue Meaning in Life Dequeue() Removes and returns the object at the beginning of the Queue. Enqueue() Adds an object to the end of the Queue. Peek() Returns the object at the beginning of the Queue without removing it. Now let’s put these methods to work. You can begin by leveraging your Person class again and building a Queue object that simulates a line of people waiting to order coffee. First, assume you have the following static helper method: static void GetCoffee(Person p) { Console.WriteLine(“{0} got coffee!“, p.FirstName); } Now assume you have this additional helper method, which calls GetCoffee() internally: static void UseGenericQueue() { // Make a Q with three people. Queue peopleQ = new Queue(); peopleQ.Enqueue(new Person {FirstName= “Homer“, LastName=“Simpson“, Age=47}); peopleQ.Enqueue(new Person {FirstName= “Marge“, LastName=“Simpson“, Age=45}); peopleQ.Enqueue(new Person {FirstName= “Lisa“, LastName=“Simpson“, Age=9}); // Peek at first person in Q. Console.WriteLine(“{0} is first in line!“, peopleQ.Peek().FirstName); // Remove each person from Q. GetCoffee(peopleQ.Dequeue()); GetCoffee(peopleQ.Dequeue()); GetCoffee(peopleQ.Dequeue()); // Try to de-Q again? try { GetCoffee(peopleQ.Dequeue()); } catch(InvalidOperationException e) { Console.WriteLine(“Error! {0}“, e.Message); } } CHAPTER 9  COLLECTIONS AND GENERICS 345 Here, you insert three items into the Queue class using its Enqueue() method. The call to Peek() allows you to view (but not remove) the first item currently in the Queue. Finally, the call to Dequeue() removes the item from the line and sends it into the GetCoffee() helper function for processing. Note that if you attempt to remove items from an empty queue, a runtime exception is thrown. Here is the output you receive when calling this method: ***** Fun with Generic Collections ***** Homer is first in line! Homer got coffee! Marge got coffee! Lisa got coffee! Error! Queue empty. Working with the SortedSet Class The SortedSet class is useful because it automatically ensures that the items in the set are sorted when you insert or remove items. However, you do need to inform the SortedSet class exactly how you want it to sort the objects, by passing in as a constructor argument an object that implements the generic IComparer interface. Begin by creating a brand new class named SortPeopleByAge, which implements IComparer, where T is of type Person. Recall that this interface defines a single method named Compare(), where you can author whatever logic you require for the comparison. Here is a simple implementation of this class: class SortPeopleByAge : IComparer { public int Compare(Person firstPerson, Person secondPerson) { if (firstPerson.Age > secondPerson.Age) return 1; if (firstPerson.Age < secondPerson.Age) return -1; else return 0; } } Now update your Program class with the following new method, which I assume you will call from Main(): static void UseSortedSet() { // Make some people with different ages. SortedSet setOfPeople = new SortedSet(new SortPeopleByAge()) { new Person {FirstName= “Homer“, LastName=“Simpson“, Age=47}, new Person {FirstName= “Marge“, LastName=“Simpson“, Age=45}, new Person {FirstName= “Lisa“, LastName=“Simpson“, Age=9}, new Person {FirstName= “Bart“, LastName=“Simpson“, Age=8} }; CHAPTER 9  COLLECTIONS AND GENERICS 346 // Note the items are sorted by age! foreach (Person p in setOfPeople) { Console.WriteLine(p); } Console.WriteLine(); // Add a few new people, with various ages. setOfPeople.Add(new Person { FirstName = “Saku“, LastName = “Jones“, Age = 1 }); setOfPeople.Add(new Person { FirstName = “Mikko“, LastName = “Jones“, Age = 32 }); // Still sorted by age! foreach (Person p in setOfPeople) { Console.WriteLine(p); } } When you run your application, the listing of objects is now always ordered based on the value of the Age property, regardless of the order you inserted or removed objects: ***** Fun with Generic Collections ***** Name: Bart Simpson, Age: 8 Name: Lisa Simpson, Age: 9 Name: Marge Simpson, Age: 45 Name: Homer Simpson, Age: 47 Name: Saku Jones, Age: 1 Name: Bart Simpson, Age: 8 Name: Lisa Simpson, Age: 9 Name: Mikko Jones, Age: 32 Name: Marge Simpson, Age: 45 Name: Homer Simpson, Age: 47  Note You can find the FunWithGenericCollections project under the Chapter 9 subdirectory. The System.Collections.ObjectModel Namespace Now that you understand the basics of how to work with generic classes, we can briefly examine an additional collection-centric namespace, System.Collections.ObjectModel. This is a relatively small namespace, which contains a handful of classes. Table 9-7 documents the two classes that you should most certainly be aware of. CHAPTER 9  COLLECTIONS AND GENERICS 347 Table 9-7. Useful Members of System.Collections.ObjectModel System.Collections.ObjectModel Type Meaning in Life ObservableCollection Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. ReadOnlyObservableCollection Represents a read-only version of ObservableCollection. The ObservableCollection class is very useful in that it has the ability to inform external objects when its contents have changed in some way (as you might guess, working with ReadOnlyObservableCollection is very similar, but read-only in nature). Working with ObservableCollection Create a new Console Application named FunWithObservableCollection, and import the System.Collections.ObjectModel namespace into your initial C# code file. In many ways, working with the ObservableCollection is identical to working with List, given that both of these classes implement the same core interfaces. What makes the ObservableCollection class unique is that this class supports an event named CollectionChanged. This event will fire whenever a new item is inserted, a current item is removed (or relocated), or if the entire collection is modified. Like any event, CollectionChanged is defined in terms of a delegate, which in this case is NotifyCollectionChangedEventHandler. This delegate can call any method that takes an object as the first parameter, and a NotifyCollectionChangedEventArgs as the second. Consider the following Main() method, which populates an observable collection containing Person objects and wires up the CollectionChanged event: class Program { static void Main(string[] args) { // Make a collection to observe and add a few Person objects. ObservableCollection people = new ObservableCollection() { new Person{ FirstName = “Peter“, LastName = “Murphy“, Age = 52 }, new Person{ FirstName = “Kevin“, LastName = “Key“, Age = 48 }, }; // Wire up the CollectionChanged event. people.CollectionChanged += people_CollectionChanged; } static void people_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { throw new NotImplementedException(); } CHAPTER 9  COLLECTIONS AND GENERICS 348 } The incoming NotifyCollectionChangedEventArgs parameter defines two important properties, OldItems and NewItems, which will give you a list of items that were currently in the collection before the event fired, and the new items that were involved in the change. However, you will want to examine these lists only under the correct circumstances. Recall that the CollectionChanged event can fire when items are added, removed, relocated, or reset. To discover which of these actions triggered the event, you can use the Action property of NotifyCollectionChangedEventArgs. The Action property can be tested against any of the following members of the NotifyCollectionChangedAction enumeration: public enum NotifyCollectionChangedAction { Add = 0, Remove = 1, Replace = 2, Move = 3, Reset = 4, } Here is an implementation of the CollectionChanged event handler that will traverse the old and new set when an item has been inserted or removed to the collection at hand: static void people_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // What was the action that caused the event? Console.WriteLine(“Action for this event: {0}“, e.Action); // They removed something. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { Console.WriteLine(“Here are the OLD items:“); foreach (Person p in e.OldItems) { Console.WriteLine(p.ToString()); } Console.WriteLine(); } // They added something. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { // Now show the NEW items that were inserted. Console.WriteLine(“Here are the NEW items:“); foreach (Person p in e.NewItems) { Console.WriteLine(p.ToString()); } } } Now, assuming you have updated your Main() method to add and remove an item, you will see output similar to the following: CHAPTER 9  COLLECTIONS AND GENERICS 349 Action for this event: Add Here are the NEW items: Name: Fred Smith, Age: 32 Action for this event: Remove Here are the OLD items: Name: Peter Murphy, Age: 52 That wraps up our examination of the various collection-centric namespaces in the .NET base class libraries. To conclude our chapter, we will now examine how we can build our own custom generic methods and custom generic types.  Source Code You can find the FunWithObservableCollection project under the Chapter 9 subdirectory. Creating Custom Generic Methods While most developers typically use the existing generic types within the base class libraries, it is also possible to build your own generic members and custom generic types. Let’s look at how to incorporate custom generics into your own projects. The first step is to build a generic swap method. Begin by creating a new console application named CustomGenericMethods. When you build custom generic methods, you achieve a supercharged version of traditional method overloading. In Chapter 2, you learned that overloading is the act of defining multiple versions of a single method, which differ by the number of, or type of, parameters. While overloading is a useful feature in an object-oriented language, one problem is that you can easily end up with a ton of methods that essentially do the same thing. For example, assume you need to build some methods that can switch two pieces of data using a simple swap routine. You might begin by authoring a new method that can operate on integers, like this: // Swap two integers. static void Swap(ref int a, ref int b) { int temp; temp = a; a = b; b = temp; } So far, so good. But now assume you also need to swap two Person objects; this would require authoring a new version of Swap(): // Swap two Person objects. static void Swap(ref Person a, ref Person b) { Person temp; temp = a; a = b; CHAPTER 9  COLLECTIONS AND GENERICS 350 b = temp; } No doubt, you can see where this is going. If you also needed to swap floating-point numbers, bitmaps, cars, buttons, and whatnot, you would have to build even more methods, which would become a maintenance nightmare. You could build a single (nongeneric) method that operated on object parameters, but then you face all the issues you examined earlier in this chapter, including boxing, unboxing, a lack of type safety, explicit casting, and so on. Whenever you have a group of overloaded methods that differ only by incoming arguments, this is your clue that generics could make your life easier. Consider the following generic Swap method that can swap any two Ts: // This method will swap any two items. // as specified by the type parameter . static void Swap(ref T a, ref T b) { Console.WriteLine(“You sent the Swap() method a {0}“, typeof(T)); T temp; temp = a; a = b; b = temp; } Notice how a generic method is defined by specifying the type parameters after the method name, but before the parameter list. Here, you state that the Swap() method can operate on any two parameters of type . To spice things up a bit, you also print out the type name of the supplied placeholder to the console using C#’s typeof() operator. Now consider the following Main() method, which swaps integers and strings: static void Main(string[] args) { Console.WriteLine(“***** Fun with Custom Generic Methods *****\n“); // Swap 2 ints. int a = 10, b = 90; Console.WriteLine(“Before swap: {0}, {1}“, a, b); Swap(ref a, ref b); Console.WriteLine(“After swap: {0}, {1}“, a, b); Console.WriteLine(); // Swap 2 strings. string s1 = “Hello“, s2 = “There“; Console.WriteLine(“Before swap: {0} {1}!“, s1, s2); Swap(ref s1, ref s2); Console.WriteLine(“After swap: {0} {1}!“, s1, s2); Console.ReadLine(); } CHAPTER 9  COLLECTIONS AND GENERICS 351 The output looks like this: ***** Fun with Custom Generic Methods ***** Before swap: 10, 90 You sent the Swap() method a System.Int32 After swap: 90, 10 Before swap: Hello There! You sent the Swap() method a System.String After swap: There Hello! The major benefit of this approach is that you have only one version of Swap() to maintain, yet it can operate on any two items of a given type in a type-safe manner. Better yet, stack-based items stay on the stack, while heap-based items stay on the heap! Inference of Type Parameters When you invoke generic methods such as Swap, you can optionally omit the type parameter if (and only if) the generic method requires arguments because the compiler can infer the type parameter based on the member parameters. For example, you could swap two System.Boolean values by adding the following code to Main(): // Compiler will infer System.Boolean. bool b1 = true, b2 = false; Console.WriteLine(“Before swap: {0}, {1}“, b1, b2); Swap(ref b1, ref b2); Console.WriteLine(“After swap: {0}, {1}“, b1, b2); Even though the compiler is able to discover the correct type parameter based on the data type used to declare b1 and b2, you should get in the habit of always specifying the type parameter explicitly: Swap(ref b1, ref b2); This makes it clear to your fellow programmers that this method is indeed generic. Moreover, inference of type parameters works only if the generic method has at least one parameter. For example, assume you have the following generic method in your Program class: static void DisplayBaseClass() { // BaseType is a method used in reflection, // which will be examined in Chapter 15 Console.WriteLine(“Base class of {0} is: {1}.“, typeof(T), typeof(T).BaseType); } In this case, you must supply the type parameter upon invocation: static void Main(string[] args) { ... // Must supply type parameter if CHAPTER 9  COLLECTIONS AND GENERICS 352 // the method does not take params. DisplayBaseClass(); DisplayBaseClass(); // Compiler error! No params? Must supply placeholder! // DisplayBaseClass(); Console.ReadLine(); } Currently, the generic Swap and DisplayBaseClass methods are defined within the application’s Program class. Of course, as with any method, you are free to define these members in a separate class type (MyGenericMethods) if you would prefer to do it that way: public static class MyGenericMethods { public static void Swap(ref T a, ref T b) { Console.WriteLine(“You sent the Swap() method a {0}“, typeof(T)); T temp; temp = a; a = b; b = temp; } public static void DisplayBaseClass() { Console.WriteLine(“Base class of {0} is: {1}.“, typeof(T), typeof(T).BaseType); } } The static Swap and DisplayBaseClass methods have been scoped within a new static class type, so you need to specify the type’s name when invoking either member, as in this example: MyGenericMethods.Swap(ref a, ref b); Of course, generic methods do not need to be static. If Swap and DisplayBaseClass were instance level (and defined in a nonstatic class), you would simply make an instance of MyGenericMethods and invoke them using the object variable: MyGenericMethods c = new MyGenericMethods(); c.Swap(ref a, ref b);  Source Code You can find the CustomGenericMethods project under the Chapter 9 subdirectory. CHAPTER 9  COLLECTIONS AND GENERICS 353 Creating Custom Generic Structures and Classes Now that you understand how to define and invoke generic methods, it’s time to turn your attention to the construction of a generic structure (the process of building a generic class is identical) within a new Console Application project named GenericPoint. Assume you have built a generic Point structure that supports a single type parameter that represents the underlying storage for the (x, y) coordinates. The caller can then create Point types as follows: // Point using ints. Point p = new Point(10, 10); // Point using double. Point p2 = new Point(5.4, 3.3); Here is the complete definition of Point, with some analysis to follow: // A generic Point structure. public struct Point { // Generic state date. private T xPos; private T yPos; // Generic constructor. public Point(T xVal, T yVal) { xPos = xVal; yPos = yVal; } // Generic properties. public T X { get { return xPos; } set { xPos = value; } } public T Y { get { return yPos; } set { yPos = value; } } public override string ToString() { return string.Format(“[{0}, {1}]“, xPos, yPos); } // Reset fields to the default value of the // type parameter. public void ResetPoint() { CHAPTER 9  COLLECTIONS AND GENERICS 354 xPos = default(T); yPos = default(T); } } The default Keyword in Generic Code As you can see, Point leverages its type parameter in the definition of the field data, constructor arguments, and property definitions. Notice that, in addition to overriding ToString(), Point defines a method named ResetPoint() that uses some new syntax you have not yet seen: // The “default“ keyword is overloaded in C#. // When used with generics, it represents the default // value of a type parameter. public void ResetPoint() { X = default(T); Y = default(T); } With the introduction of generics, the C# default keyword has been given a dual identity. In addition to its use within a switch construct, it can also be used to set a type parameter to its default value. This is helpful because a generic type does not know the actual placeholders up front, which means it cannot safely assume what the default value will be. The defaults for a type parameter are as follows: • Numeric values have a default value of 0. • Reference types have a default value of null. • Fields of a structure are set to 0 (for value types) or null (for reference types). For Point, you can set the X and Y values to 0 directly because it is safe to assume the caller will supply only numerical data. However, you can also increase the overall flexibility of the generic type by using the default(T) syntax. In any case, you can now exercise the methods of Point: static void Main(string[] args) { Console.WriteLine(“***** Fun with Generic Structures *****\n“); // Point using ints. Point p = new Point(10, 10); Console.WriteLine(“p.ToString()={0}“, p.ToString()); p.ResetPoint(); Console.WriteLine(“p.ToString()={0}“, p.ToString()); Console.WriteLine(); // Point using double. Point p2 = new Point(5.4, 3.3); Console.WriteLine(“p2.ToString()={0}“, p2.ToString()); p2.ResetPoint(); Console.WriteLine(“p2.ToString()={0}“, p2.ToString()); CHAPTER 9  COLLECTIONS AND GENERICS 355 Console.ReadLine(); } Here is the output: ***** Fun with Generic Structures ***** p.ToString()=[10, 10] p.ToString()=[0, 0] p2.ToString()=[5.4, 3.3] p2.ToString()=[0, 0]  Source Code You can find the GenericPoint project under the Chapter 9 subdirectory. Constraining Type Parameters As this chapter illustrates, any generic item has at least one type parameter that you need to specify at the time you interact with the generic type or member. This alone allows you to build some type-safe code; however, the .NET platform allows you to use the where keyword to get extremely specific about what a given type parameter must look like. Using this keyword, you can add a set of constraints to a given type parameter, which the C# compiler will check at compile time. Specifically, you can constrain a type parameter as described in Table 9-8. Table 9-8. Possible Constraints for Generic Type Parameters Generic Constraint Meaning in Life where T : struct The type parameter must have System.ValueType in its chain of inheritance (i.e., must be a structure). where T : class The type parameter must not have System.ValueType in its chain of inheritance (i.e., must be a reference type). where T : new() The type parameter must have a default constructor. This is helpful if your generic type must create an instance of the type parameter because you cannot assume you know the format of custom constructors. Note that this constraint must be listed last on a multiconstrained type. where T : NameOfBaseClass The type parameter must be derived from the class specified by NameOfBaseClass. CHAPTER 9  COLLECTIONS AND GENERICS 356 Generic Constraint Meaning in Life where T : NameOfInterface The type parameter must implement the interface specified by NameOfInterface. You can separate multiple interfaces as a comma- delimited list. Unless you need to build some extremely type-safe custom collections, you might never need to use the where keyword in your C# projects. Regardless, the following handful of (partial) code examples illustrate how to work with the where keyword. Examples Using the where Keyword Begin by assuming that you have created a custom generic class, and you want to ensure that the type parameter has a default constructor. This could be useful when the custom generic class needs to create instances of the T because the default constructor is the only constructor that is potentially common to all types. Also, constraining T in this way lets you get compile-time checking; if T is a reference type, the programmer remembered to redefine the default in the class definition (you might recall that the default constructor is removed in classes when you define your own): // MyGenericClass derives from object, while // contained items must have a default ctor. public class MyGenericClass where T : new() { ... } Notice that the where clause specifies which type parameter is being constrained, followed by a colon operator. After the colon operator, you list each possible constraint (in this case, a default constructor). Here is another example: // MyGenericClass derives from object, while // contained items must be a class implementing IDrawable // and must support a default ctor. public class MyGenericClass where T : class, IDrawable, new() { ... } In this case, T has three requirements. It must be a reference type (not a structure), as marked with the class token. Second, T must implement the IDrawable interface. Third, it must also have a default constructor. Multiple constraints are listed in a comma-delimited list; however, you should be aware that the new() constraint must always be listed last! Thus, the following code will not compile: // Error! new() constraint must be listed last! public class MyGenericClass where T : new(), class, IDrawable { ... } If you ever create a custom generic collection class that specifies multiple type parameters, you can specify a unique set of constraints for each, using separate where clauses: CHAPTER 9  COLLECTIONS AND GENERICS 357 // must extend SomeBaseClass and have a default ctor, // while must be a structure and implement the // generic IComparable interface. public class MyGenericClass where K : SomeBaseClass, new() where T : struct, IComparable { ... } You will rarely encounter cases where you need to build a complete custom generic collection class; however, you can use the where keyword on generic methods, as well. For example, if you want to specify that your generic Swap() method can operate only on structures, you would update the method like this: // This method will swap any structure, but not classes. static void Swap(ref T a, ref T b) where T : struct { ... } Note that if you were to constrain the Swap() method in this manner, you would no longer be able to swap string objects (as is shown in the sample code) because string is a reference type. The Lack of Operator Constraints I want to make one more comment on generic methods and constraints as this chapter draws to a close. It might come as a surprise to you to find out that, when creating generic methods, you will get a compiler error if you apply any C# operators (+, -, *, ==, etc.) on the type parameters. For example, imagine the usefulness of a class that can Add(), Subtract(), Multiply(), and Divide() generic types: // Compiler error! Cannot apply // operators to type parameters! public class BasicMath { public T Add(T arg1, T arg2) { return arg1 + arg2; } public T Subtract(T arg1, T arg2) { return arg1 - arg2; } public T Multiply(T arg1, T arg2) { return arg1 * arg2; } public T Divide(T arg1, T arg2) { return arg1 / arg2; } } Unfortunately, the preceding BasicMath class will not compile. While this might seem like a major restriction, you need to remember that generics are generic. Of course, the numerical data can work just fine with the binary operators of C#. However, for the sake of argument, if were a custom class or structure type, the compiler could assume the class supports the +, -, *, and / operators. Ideally, C# would allow a generic type to be constrained by supported operators, as in this example: // Illustrative code only! public class BasicMath where T : operator +, operator -, operator *, operator / { CHAPTER 9  COLLECTIONS AND GENERICS 358 public T Add(T arg1, T arg2) { return arg1 + arg2; } public T Subtract(T arg1, T arg2) { return arg1 - arg2; } public T Multiply(T arg1, T arg2) { return arg1 * arg2; } public T Divide(T arg1, T arg2) { return arg1 / arg2; } } Alas, operator constraints are not supported under the current version of C#. However, it is possible (albeit it requires a bit more work) to achieve the desired effect by defining an interface that supports these operators (C# interfaces can define operators!) and then specifying an interface constraint of the generic class. In any case, this wraps up this book’s initial look at building custom generic types. In Chapter 10, you will pick up the topic of generics once again in the course of examining the .NET delegate type. Summary This chapter began by examining the nongeneric collection types of System.Collections and System.Collections.Specialized, including the various issues associated with many nongeneric containers, including a lack of type safety and the runtime overhead of boxing and unboxing operations. As mentioned, for these very reasons, modern-day .NET programs will typically make use of the generic collection classes found in System.Collections.Generic and System.Collections.ObjectModel. As you have seen, a generic item allows you to specify placeholders (type parameters) that you specify at the time of object creation (or invocation, in the case of generic methods). While you will most often simply use the generic types provided in the .NET base class libraries, you will also be able to create your own generic types (and generic methods). When you do so, you have the option of specifying any number of constraints (using the where keyword) to increase the level of type safety and ensure that you perform operations on types of a known quantity that are guaranteed to exhibit certain basic capabilities. As a final note, remember that generics are found in numerous locations within the .NET base class libraries. Here, we focused specifically on generic collections. However, as you work through the remainder of this book (and when you dive into the platform on your own terms), you will certainly find generic classes, structures, and delegates located in a given namespace. As well, be on the lookout for generic members of a nongeneric class! C H A P T E R 10 359 Delegates, Events, and Lambda Expressions Up to this point in the text, most of the applications you developed added various bits of code to Main(), which, in some way or another, sent requests to a given object. However, many applications require that an object be able to communicate back to the entity that created it using a callback mechanism. While callback mechanisms can be used in any application, they are especially critical for graphical user interfaces in that controls (such as a button) need to invoke external methods under the correct circumstances (when the button is clicked, when the mouse enters the button surface, and so forth). Under the .NET platform, the delegate type is the preferred means of defining and responding to callbacks within applications. Essentially, the .NET delegate type is a type-safe object that “points to” a method or a list of methods that can be invoked at a later time. Unlike a traditional C++ function pointer, however, .NET delegates are classes that have built-in support for multicasting and asynchronous method invocation. In this chapter, you will learn how to create and manipulate delegate types, then you’ll investigate the C# event keyword, which streamlines the process of working with delegate types. Along the way, you will also examine several delegate- and event-centric language features of C#, including anonymous methods and method group conversions. I wrap up this chapter by examining lambda expressions. Using the C# lambda operator (=>), you can specify a block of code statements (and the parameters to pass to those code statements) wherever a strongly typed delegate is required. As you will see, a lambda expression is little more than an anonymous method in disguise, and provides a simplified approach to working with delegates. Understanding the .NET Delegate Type Before formally defining .NET delegates, let’s gain a bit of perspective. Historically, the Windows API made frequent use of C-style function pointers to create entities termed callback functions, or simply callbacks. Using callbacks, programmers were able to configure one function to report back to (call back) another function in the application. With this approach, Windows developers were able to handle button-clicking, mouse-moving, menu-selecting, and general bidirectional communications between two entities in memory. The problem with standard C-style callback functions is that they represent little more than a raw address in memory. Ideally, you should be able to configure callbacks to include additional type-safe information such as the number of (and types of) parameters and the return type (if any) of the method pointed to. Sadly, this is not the case in traditional callback functions and, as you might suspect, they can therefore be a frequent source of bugs, hard crashes, and other runtime disasters. Nevertheless, callbacks are useful entities. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 360 In the .NET Framework, callbacks are still possible, and this functionality is accomplished in a much safer and more object-oriented manner using delegates. In essence, a delegate is a type-safe object that points to another method (or possibly a list of methods) in the application, which can be invoked at a later time. Specifically, a delegate maintains three important pieces of information: • The address of the method on which it makes calls • The parameters (if any) of this method • The return type (if any) of this method  Note .NET delegates can point to either static or instance methods. After a delegate object has been created and given the necessary information, it may dynamically invoke the method(s) it points to at runtime. Every delegate in the .NET Framework (including your custom delegates) is automatically endowed with the ability to call its methods synchronously or asynchronously. This fact greatly simplifies programming tasks, given that you can call a method on a secondary thread of execution without manually creating and managing a Thread object.  Note We will examine the asynchronous behavior of delegate types during our investigation of threading and asynchronous calls in Chapter 19. In this chapter, we are concerned only with the synchronous aspects of the delegate type. Defining a Delegate Type in C# When you want to create a delegate type in C#, you use the delegate keyword. The name of your delegate type can be whatever you desire. However, you must define the delegate to match the signature of the method(s) it will point to. For example, the following delegate type (named BinaryOp) can point to any method that returns an integer and takes two integers as input parameters (you will build and use this delegate yourself a bit later in this chapter, so hang tight for now): // This delegate can point to any method, // taking two integers and returning an integer. public delegate int BinaryOp(int x, int y); When the C# compiler processes delegate types, it automatically generates a sealed class deriving from System.MulticastDelegate. This class (in conjunction with its base class, System.Delegate) provides the necessary infrastructure for the delegate to hold onto a list of methods to be invoked at a later time. For example, if you were to examine the BinaryOp delegate using ildasm.exe, you would find the class shown in Figure 10-1. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 361 Figure 10-1. The C# delegate keyword represents a sealed class deriving from System.MulticastDelegate As you can see, the compiler-generated BinaryOp class defines three public methods. Invoke() is perhaps the key method, as it is used to invoke each method maintained by the delegate object in a synchronous manner, meaning the caller must wait for the call to complete before continuing on its way. Strangely enough, the synchronous Invoke() method may not need to be called explicitly from your C# code. As you will see in just a bit, Invoke()is called behind the scenes when you make use of the appropriate C# syntax. BeginInvoke() and EndInvoke() provide the ability to call the current method asynchronously on a separate thread of execution. If you have a background in multithreading, you know that one of the most common reasons developers create secondary threads of execution is to invoke methods that require time to complete. Although the .NET base class libraries supply several namespaces devoted to multithreaded and parallel programming, delegates provide this functionality out of the box. Now, how exactly does the compiler know how to define the Invoke(), BeginInvoke(), and EndInvoke() methods? To understand the process, here is the crux of the compiler-generated BinaryOp class type (bold italic marks the items specified by the defined delegate type): sealed class BinaryOp : System.MulticastDelegate { public int Invoke(int x, int y); public IAsyncResult BeginInvoke(int x, int y, AsyncCallback cb, object state); public int EndInvoke(IAsyncResult result); } First, notice that the parameters and return type defined for the Invoke() method exactly match the definition of the BinaryOp delegate. The initial parameters to BeginInvoke() members (two integers, in our case) are also based on the BinaryOp delegate; however, BeginInvoke() will always provide two final parameters (of type AsyncCallback and object) that are used to facilitate asynchronous method invocations. Finally, the return type of EndInvoke() is identical to the original delegate declaration and will always take as a sole parameter an object implementing the IAsyncResult interface. Let’s see another example. Assume you have defined a delegate type that can point to any method returning a string and receiving three System.Boolean input parameters: public delegate string MyDelegate(bool a, bool b, bool c); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 362 This time, the compiler-generated class breaks down as follows: sealed class MyDelegate : System.MulticastDelegate { public string Invoke(bool a, bool b, bool c); public IAsyncResult BeginInvoke(bool a, bool b, bool c, AsyncCallback cb, object state); public string EndInvoke(IAsyncResult result); } Delegates can also “point to” methods that contain any number of out or ref parameters (as well as array parameters marked with the params keyword). For example, assume the following delegate type: public delegate string MyOtherDelegate(out bool a, ref bool b, int c); The signatures of the Invoke() and BeginInvoke() methods look as you would expect; however, check out the EndInvoke() method, which now includes the set of all out/ref arguments defined by the delegate type: public sealed class MyOtherDelegate : System.MulticastDelegate { public string Invoke(out bool a, ref bool b, int c); public IAsyncResult BeginInvoke(out bool a, ref bool b, int c, AsyncCallback cb, object state); public string EndInvoke(out bool a, ref bool b, IAsyncResult result); } To summarize, a C# delegate type definition results in a sealed class with three compiler-generated methods whose parameter and return types are based on the delegate’s declaration. The following pseudo-code approximates the basic pattern: // This is only pseudo-code! public sealed class DelegateName : System.MulticastDelegate { public delegateReturnValue Invoke(allDelegateInputRefAndOutParams); public IAsyncResult BeginInvoke(allDelegateInputRefAndOutParams, AsyncCallback cb, object state); public delegateReturnValue EndInvoke(allDelegateRefAndOutParams, IAsyncResult result); } The System.MulticastDelegate and System.Delegate Base Classes So, when you build a type using the C# delegate keyword, you are indirectly declaring a class type that derives from System.MulticastDelegate. This class provides descendants with access to a list that contains the addresses of the methods maintained by the delegate object, as well as several additional methods (and a few overloaded operators) to interact with the invocation list. Here are some select members of System.MulticastDelegate: public abstract class MulticastDelegate : Delegate { // Returns the list of methods “pointed to.“ CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 363 public sealed override Delegate[] GetInvocationList(); // Overloaded operators. public static bool operator ==(MulticastDelegate d1, MulticastDelegate d2); public static bool operator !=(MulticastDelegate d1, MulticastDelegate d2); // Used internally to manage the list of methods maintained by the delegate. private IntPtr _invocationCount; private object _invocationList; } System.MulticastDelegate obtains additional functionality from its parent class, System.Delegate. Here is a partial snapshot of the class definition: public abstract class Delegate : ICloneable, ISerializable { // Methods to interact with the list of functions. public static Delegate Combine(params Delegate[] delegates); public static Delegate Combine(Delegate a, Delegate b); public static Delegate Remove(Delegate source, Delegate value); public static Delegate RemoveAll(Delegate source, Delegate value); // Overloaded operators. public static bool operator ==(Delegate d1, Delegate d2); public static bool operator !=(Delegate d1, Delegate d2); // Properties that expose the delegate target. public MethodInfo Method { get; } public object Target { get; } } Now, understand that you can never directly derive from these base classes in your code (it is a compiler error to do so). Nevertheless, when you use the delegate keyword, you have indirectly created a class that “is-a” MulticastDelegate. Table 10-1 documents the core members common to all delegate types. Table 10-1. Select Members of System.MultcastDelegate/System.Delegate Member Meaning in Life Method This property returns a System.Reflection.MethodInfo object that represents details of a static method maintained by the delegate. Target If the method to be called is defined at the object level (rather than a static method), Target returns an object that represents the method maintained by the delegate. If the value returned from Target equals null, the method to be called is a static member. Combine() This static method adds a method to the list maintained by the delegate. In C#, you trigger this method using the overloaded += operator as a shorthand notation. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 364 Member Meaning in Life GetInvocationList() This method returns an array of System.Delegate objects, each representing a particular method that may be invoked. Remove() RemoveAll() These static methods remove a method (or all methods) from the delegate’s invocation list. In C#, the Remove() method can be called indirectly using the overloaded -= operator. The Simplest Possible Delegate Example To be sure, delegates can cause some confusion when encountered for the first time. Thus, to get the ball rolling, let’s take a look at a very simple Console Application program (named SimpleDelegate) that makes use of the BinaryOp delegate type you’ve seen previously. Here is the complete code, with analysis to follow: namespace SimpleDelegate { // This delegate can point to any method, // taking two integers and returning an integer. public delegate int BinaryOp(int x, int y); // This class contains methods BinaryOp will // point to. public class SimpleMath { public static int Add(int x, int y) { return x + y; } public static int Subtract(int x, int y) { return x - y; } } class Program { static void Main(string[] args) { Console.WriteLine(“***** Simple Delegate Example *****\n“); // Create a BinaryOp delegate object that // “points to“ SimpleMath.Add(). BinaryOp b = new BinaryOp(SimpleMath.Add); // Invoke Add() method indirectly using delegate object. Console.WriteLine(“10 + 10 is {0}“, b(10, 10)); Console.ReadLine(); } } } Again, notice the format of the BinaryOp delegate type declaration; it specifies that BinaryOp delegate objects can point to any method taking two integers and returning an integer (the actual name of the CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 365 method pointed to is irrelevant). Here, we have created a class named SimpleMath, which defines two static methods that match the pattern defined by the BinaryOp delegate. When you want to insert the target method to a given delegate object, simply pass in the name of the method to the delegate’s constructor: // Create a BinaryOp delegate object that // “points to“ SimpleMath.Add(). BinaryOp b = new BinaryOp(SimpleMath.Add); At this point, you are able to invoke the member pointed to using a syntax that looks like a direct function invocation: // Invoke() is really called here! Console.WriteLine(“10 + 10 is {0}“, b(10, 10)); Under the hood, the runtime actually calls the compiler-generated Invoke() method on your MulticastDelegate derived class. You can verify this for yourself if you open your assembly in ildasm.exe and examine the CIL code within the Main() method: .method private hidebysig static void Main(string[] args) cil managed { ... callvirt instance int32 SimpleDelegate.BinaryOp::Invoke(int32, int32) } C# does not require you to explicitly call Invoke() within your code base. Because BinaryOp can point to methods that take two arguments, the following code statement is also permissible: Console.WriteLine(“10 + 10 is {0}“, b.Invoke(10, 10)); Recall that .NET delegates are type safe. Therefore, if you attempt to pass a delegate a method that does not match the pattern, you receive a compile-time error. To illustrate, assume the SimpleMath class now defines an additional method named SquareNumber(), which takes a single integer as input: public class SimpleMath { ... public static int SquareNumber(int a) { return a * a; } } Given that the BinaryOp delegate can point only to methods that take two integers and return an integer, the following code is illegal and will not compile: // Compiler error! Method does not match delegate pattern! BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); Investigating a Delegate Object Let’s spice up the current example by creating a static method (named DisplayDelegateInfo()) within the Program class. This method will print out the names of the method(s) maintained by a delegate object, as well as the name of the class defining the method. To do this, we will iterate over the System.Delegate array returned by GetInvocationList(), invoking each object’s Target and Method properties: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 366 static void DisplayDelegateInfo(Delegate delObj) { // Print the names of each member in the // delegate's invocation list. foreach (Delegate d in delObj.GetInvocationList()) { Console.WriteLine(“Method Name: {0}“, d.Method); Console.WriteLine(“Type Name: {0}“, d.Target); } } Assuming you have updated your Main() method to actually call this new helper method: BinaryOp b = new BinaryOp(SimpleMath.Add); DisplayDelegateInfo(b); you would find the output shown next: ***** Simple Delegate Example ***** Method Name: Int32 Add(Int32, Int32) Type Name: 10 + 10 is 20 Notice that the name of the target class (SimpleMath) is currently not displayed when calling the Target property. The reason has to do with the fact that our BinaryOp delegate is pointing to a static method and, therefore, there is no object to reference! However, if we update the Add() and Subtract() methods to be nonstatic (simply by deleting the static keywords), we could create an instance of the SimpleMath class and specify the methods to invoke using the object reference: static void Main(string[] args) { Console.WriteLine(“***** Simple Delegate Example *****\n“); // .NET delegates can also point to instance methods as well. SimpleMath m = new SimpleMath(); BinaryOp b = new BinaryOp(m.Add); // Show information about this object. DisplayDelegateInfo(b); Console.WriteLine(“10 + 10 is {0}“, b(10, 10)); Console.ReadLine(); } In this case, we would find the output shown here: ***** Simple Delegate Example ***** Method Name: Int32 Add(Int32, Int32) Type Name: SimpleDelegate.SimpleMath CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 367 10 + 10 is 20  Note The SimpleDelegate project is located under the Chapter 10 subdirectory. Sending Object State Notifications Using Delegates Clearly, the previous SimpleDelegate example was intended to be purely illustrative in nature, given that there would be no compelling reason to define a delegate simply to add two numbers. To provide a more realistic use of delegate types, let’s use delegates to define a Car class that has the ability to inform external entities about its current engine state. To do so, we will take the following steps: 1. Define a new delegate type that will be used to send notifications to the caller. 2. Declare a member variable of this delegate in the Car class. 3. Create a helper function on the Car that allows the caller to specify the method to call back on. 4. Implement the Accelerate() method to invoke the delegate’s invocation list under the correct circumstances. To begin, create a new Console Application project named CarDelegate. Now, define a new Car class that looks initially like this: public class Car { // Internal state data. public int CurrentSpeed { get; set; } public int MaxSpeed { get; set; } public string PetName { get; set; } // Is the car alive or dead? private bool carIsDead; // Class constructors. public Car() { MaxSpeed = 100; } public Car(string name, int maxSp, int currSp) { CurrentSpeed = currSp; MaxSpeed = maxSp; PetName = name; } } Now, consider the following updates, which address the first three points: public class Car { ... // 1) Define a delegate type. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 368 public delegate void CarEngineHandler(string msgForCaller); // 2) Define a member variable of this delegate. private CarEngineHandler listOfHandlers; // 3) Add registration function for the caller. public void RegisterWithCarEngine(CarEngineHandler methodToCall) { listOfHandlers = methodToCall; } } Notice in this example that we define the delegate types directly within the scope of the Car class, which is certainly not necessary, but does help enforce the idea that the delegate works naturally with this particular class. Our delegate type, CarEngineHandler, can point to any method taking a single string as input and void as a return value. Next, note that we declare a private member variable of our delegate (named listOfHandlers), and a helper function (named RegisterWithCarEngine()) that allows the caller to assign a method to the delegate’s invocation list.  Note Strictly speaking, we could have defined our delegate member variable as public, therefore avoiding the need to create additional registration methods. However, by defining the delegate member variable as private, we are enforcing encapsulation services and providing a more type-safe solution. You’ll revisit the risk of public delegate member variables later in this chapter when you look at the C# event keyword. At this point, we need to create the Accelerate() method. Recall, the point here is to allow a Car object to send engine-related messages to any subscribed listener. Here is the update: // 4) Implement the Accelerate() method to invoke the delegate's // invocation list under the correct circumstances. public void Accelerate(int delta) { // If this car is “dead,“ send dead message. if (carIsDead) { if (listOfHandlers != null) listOfHandlers(“Sorry, this car is dead...“); } else { CurrentSpeed += delta; // Is this car “almost dead“? if (10 == (MaxSpeed - CurrentSpeed) && listOfHandlers != null) { listOfHandlers(“Careful buddy! Gonna blow!“); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 369 } if (CurrentSpeed >= MaxSpeed) carIsDead = true; else Console.WriteLine(“CurrentSpeed = {0}“, CurrentSpeed); } } Notice that before we invoke the methods maintained by the listOfHandlers member variable, we are checking it against a null value. The reason is that it will be the job of the caller to allocate these objects by calling the RegisterWithCarEngine() helper method. If the caller does not call this method and we attempt to invoke the delegate’s invocation list, we will trigger a NullReferenceException at runtime. Now that we have the delegate infrastructure in place, observe the updates to the Program class: class Program { static void Main(string[] args) { Console.WriteLine(“***** Delegates as event enablers *****\n“); // First, make a Car object. Car c1 = new Car(“SlugBug“, 100, 10); // Now, tell the car which method to call // when it wants to send us messages. c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent)); // Speed up (this will trigger the events). Console.WriteLine(“***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } // This is the target for incoming events. public static void OnCarEngineEvent(string msg) { Console.WriteLine(“\n***** Message From Car Object *****“); Console.WriteLine(“=> {0}“, msg); Console.WriteLine(“***********************************\n“); } } The Main() method begins by simply making a new Car object. Since we are interested in hearing about the engine events, our next step is to call our custom registration function, RegisterWithCarEngine(). Recall that this method expects to be passed an instance of the nested CarEngineHandler delegate, and as with any delegate, we specify a “method to point to” as a constructor parameter. The trick in this example is that the method in question is located back in the Program class! Again, notice that the OnCarEngineEvent() method is a dead-on match to the related delegate in that it takes a string as input and returns void. Consider the output of the current example: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 370 ***** Delegates as event enablers ***** ***** Speeding up ***** CurrentSpeed = 30 CurrentSpeed = 50 CurrentSpeed = 70 ***** Message From Car Object ***** => Careful buddy! Gonna blow! *********************************** CurrentSpeed = 90 ***** Message From Car Object ***** => Sorry, this car is dead... *********************************** Enabling Multicasting Recall that .NET delegates have the built-in ability to multicast. In other words, a delegate object can maintain a list of methods to call, rather than just a single method. When you want to add multiple methods to a delegate object, you simply make use of the overloaded += operator, rather than a direct assignment. To enable multicasting on the Car class, we could update the RegisterWithCarEngine()method, like so: public class Car { // Now with multicasting support! // Note we are now using the += operator, not // the assignment operator (=). public void RegisterWithCarEngine(CarEngineHandler methodToCall) { listOfHandlers += methodToCall; } ... } When you use the += operator on a delegate object, the compiler resolves this to a call on the static Delegate.Combine() method. In fact, you could call Delegate.Combine() directly; however, the += operator offers a simpler alternative. There is no need to modify your current RegisterWithCarEngine() method, but here is an example if using Delegate.Combine() rather than the += operator: public void RegisterWithCarEngine( CarEngineHandler methodToCall ) { if (listOfHandlers == null) listOfHandlers = methodToCall; else Delegate.Combine(listOfHandlers, methodToCall); } CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 371 In any case, the caller can now register multiple targets for the same callback notification. Here, our second handler prints the incoming message in uppercase, just for display purposes: class Program { static void Main(string[] args) { Console.WriteLine(“***** Delegates as event enablers *****\n“); // First, make a Car object. Car c1 = new Car(“SlugBug“, 100, 10); // Register multiple targets for the notifications. c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent)); c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2)); // Speed up (this will trigger the events). Console.WriteLine(“***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } // We now have TWO methods that will be called by the Car // when sending notifications. public static void OnCarEngineEvent(string msg) { Console.WriteLine(“\n***** Message From Car Object *****“); Console.WriteLine(“=> {0}“, msg); Console.WriteLine(“***********************************\n“); } public static void OnCarEngineEvent2(string msg) { Console.WriteLine(“=> {0}“, msg.ToUpper()); } } Removing Targets from a Delegate’s Invocation List The Delegate class also defines a static Remove() method that allows a caller to dynamically remove a method from a delegate object’s invocation list. This makes it simple to allow the caller to “unsubscribe” from a given notification at runtime. While you could call Delegate.Remove() directly in code, C# developers can use the -= operator as a convenient shorthand notation. Let’s add a new method to the Car class that allows a caller to remove a method from the invocation list: public class Car { ... public void UnRegisterWithCarEngine(CarEngineHandler methodToCall) { listOfHandlers -= methodToCall; CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 372 } } With the current updates to the Car class, we could stop receiving the engine notification on the second handler by updating Main() as follows: static void Main(string[] args) { Console.WriteLine(“***** Delegates as event enablers *****\n“); // First, make a Car object. Car c1 = new Car(“SlugBug“, 100, 10); c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent)); // This time, hold onto the delegate object, // so we can unregister later. Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2); c1.RegisterWithCarEngine(handler2); // Speed up (this will trigger the events). Console.WriteLine(“***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); // Unregister from the second handler. c1.UnRegisterWithCarEngine(handler2); // We won't see the “uppercase“ message anymore! Console.WriteLine(“***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } One difference in Main() is that this time we are creating a Car.CarEngineHandler object and storing it in a local variable so we can use this object to unregister with the notification later on. Thus, the second time we speed up the Car object, we no longer see the uppercase version of the incoming message data, as we have removed this target from the delegate’s invocation list.  Source Code The CarDelegate project is located under the Chapter 10 subdirectory. Method Group Conversion Syntax In the previous CarDelegate example, we explicitly created instances of the Car.CarEngineHandler delegate object in order to register and unregister with the engine notifications: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 373 static void Main(string[] args) { Console.WriteLine(“***** Delegates as event enablers *****\n“); Car c1 = new Car(“SlugBug“, 100, 10); c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent)); Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2); c1.RegisterWithCarEngine(handler2); ... } To be sure, if you need to call any of the inherited members of MulticastDelegate or Delegate, manually creating a delegate variable is the most straightforward way of doing so. However, in most cases, you don’t really need to hang onto the delegate object. Rather, you typically only need to use the delegate object in order to pass in the method name as a constructor parameter. As a simplification, C# provides a shortcut termed method group conversion. This feature allows you to supply a direct method name, rather than a delegate object, when calling methods that take delegates as arguments.  Note As you will see later in this chapter, you can also use method group conversion syntax to simplify how you register with a C# event. To illustrate, create a new Console Application named CarDelegateMethodGroupConversion and insert the file containing the Car class you defined in the CarDelegate project (and update the namespace name in the Car.cs file to match your new namespace name). Now, consider the following Program class, which uses method group conversion to register and unregister from the engine notifications: class Program { static void Main(string[] args) { Console.WriteLine(“***** Method Group Conversion *****\n“); Car c1 = new Car(); // Register the simple method name. c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine(“***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); // Unregister the simple method name. c1.UnRegisterWithCarEngine(CallMeHere); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 374 // No more notifications! for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } static void CallMeHere(string msg) { Console.WriteLine(“=> Message from Car: {0}“, msg); } } Notice that we are not directly allocating the associated delegate object, but rather simply specifying a method that matches the delegate’s expected signature (a method returning void and taking a single string, in this case). Understand that the C# compiler is still ensuring type safety. Thus, if the CallMeHere() method did not take a string and return void, we would be issued a compiler error.  Source Code The CarDelegateMethodGroupConversion project is located under the Chapter 10 subdirectory. Understanding Generic Delegates In the previous chapter, I mentioned that C# allows you to define generic delegate types. For example, assume you want to define a delegate type that can call any method returning void and receiving a single parameter. If the argument in question may differ, you could model this using a type parameter. To illustrate, consider the following code within a new Console Application named GenericDelegate: namespace GenericDelegate { // This generic delegate can call any method // returning void and taking a single type parameter. public delegate void MyGenericDelegate(T arg); class Program { static void Main(string[] args) { Console.WriteLine(“***** Generic Delegates *****\n“); // Register targets. MyGenericDelegate strTarget = new MyGenericDelegate(StringTarget); strTarget(“Some string data“); MyGenericDelegate intTarget = new MyGenericDelegate(IntTarget); intTarget(9); Console.ReadLine(); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 375 } static void StringTarget(string arg) { Console.WriteLine(“arg in uppercase is: {0}“, arg.ToUpper()); } static void IntTarget(int arg) { Console.WriteLine(“++arg is: {0}“, ++arg); } } } Notice that MyGenericDelegate defines a single type parameter that represents the argument to pass to the delegate target. When creating an instance of this type, you are required to specify the value of the type parameter, as well as the name of the method the delegate will invoke. Thus, if you specified a string type, you send a string value to the target method: // Create an instance of MyGenericDelegate // with string as the type parameter. MyGenericDelegate strTarget = new MyGenericDelegate(StringTarget); strTarget(“Some string data“); Given the format of the strTarget object, the StringTarget() method must now take a single string as a parameter: static void StringTarget(string arg) { Console.WriteLine(“arg in uppercase is: {0}“, arg.ToUpper()); }  Source Code The GenericDelegate project is located under the Chapter 10 subdirectory. The Generic Action<> and Func<> Delegates Over the course of this chapter, you have seen that when you want to use delegates to enable callbacks in your applications, you typically follow the steps shown here: • Define a custom delegate that matches the format of the method being pointed to. • Create an instance of your custom delegate, passing in a method name as a constructor argument. • Invoke the method indirectly, via a call to Invoke() on the delegate object. When you take this approach, you typically end up with a number of custom delegates that might never be used beyond the current task at hand (e.g., MyGenericDelegate, CarEngineHandler, and so forth). While it may certainly be the case that you do indeed need to have a custom, uniquely named CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 376 delegate for your project, other times the exact name of the delegate is irrelevant. In many cases, you simply want “some delegate” that takes a set of arguments and possibly has a return value other than void. In these cases, you can make use of the framework’s built-in Action<> and Func<> delegates. To illustrate their usefulness, create a new Console Application project type named ActionAndFuncDelegates. The generic Action<> delegate is defined in the System namespaces of mscorlib.dll and System.Core.dll assemblies. You can use this generic delegate to “point to” a method that takes up to 16 arguments (that ought to be enough!) and returns void. Now recall, because Action<> is a generic delegate, you will need to specify the underlying types of each parameter as well. Update your Program class to define a new static method that takes three (or so) unique parameters, for example: // This is a target for the Action<> delegate. static void DisplayMessage(string msg, ConsoleColor txtColor, int printCount) { // Set color of console text. ConsoleColor previous = Console.ForegroundColor; Console.ForegroundColor = txtColor; for (int i = 0; i < printCount; i++) { Console.WriteLine(msg); } // Restore color. Console.ForegroundColor = previous; } Now, rather than building a custom delegate manually to pass the program’s flow to the DisplayMessage() method, we can use the out-of-the-box Action<> delegate, as so: static void Main(string[] args) { Console.WriteLine(“***** Fun with Action and Func *****“); // Use the Action<> delegate to point to DisplayMessage. Action actionTarget = new Action(DisplayMessage); actionTarget(“Action Message!“, ConsoleColor.Yellow, 5); Console.ReadLine(); } As you can see, using the Action<> delegate saves you the bother of defining a custom delegate. However, recall that the Action<> delegate can point only to methods that take a void return value. If you want to point to a method that does have a return value (and don’t want to bother writing the custom delegate yourself), you can use Func<>. The generic Func<> delegate can point to methods that (like Action<>) take up to 16 parameters and a custom return value. To illustrate, add the following new method to the Program class: // Target for the Func<> delegate. static int Add(int x, int y) { CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 377 return x + y; } Earlier in the chapter, I had you build a custom BinaryOp delegate to “point to” addition and subtraction methods. However, we can simplify our efforts using a version of Func<> that takes a total of three type parameters. Be aware that the final type parameter of Func<> is always the return value of the method. Just to solidify that point, assume the Program class also defines the following method: static string SumToString(int x, int y) { return (x + y).ToString(); } Now, our Main() method can call each of these methods, as so: Func funcTarget = new Func(Add); int result = funcTarget.Invoke(40, 40); Console.WriteLine(“40 + 40 = {0}“, result); Func funcTarget2 = new Func(SumToString); string sum = funcTarget2(90, 300); Console.WriteLine(sum); So, given that Action<> and Func<> can save you the step of manually defining a custom delegate, you might be wondering if you should use them all the time. The answer, like so many aspects of programming is “it depends.” In many cases, Action<> and Func<> will be the preferred course of action (no pun intended). However, if you need a delegate that has a custom name that you feel helps better capture your problem domain, building a custom delegate is as simple as a single code statement. You’ll see both approaches as you work over the remainder of this text.  Note Many important .NET APIs make considerable use of Action<> and Func<> delegates, including the parallel programming framework and LINQ (among others). That wraps up our initial look at the .NET delegate type. We will look at some additional details of working with delegates at the conclusion of this chapter and again in Chapter 19 during our examination of multithreading and asynchronous calls. Next, let’s move on to the related topic of the C# event keyword.  Source Code The ActionAndFuncDelegates project is located under the Chapter 10 subdirectory. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 378 Understanding C# Events Delegates are fairly interesting constructs in that they enable objects in memory to engage in a two-way conversation. However, working with delegates in the raw can entail the creation of some boilerplate code (defining the delegate, declaring necessary member variables, and creating custom registration and unregistration methods to preserve encapsulation, etc.). Moreover, when you use delegates in the raw as your application’s callback mechanism, if you do not define a class’s delegate member variables as private, the caller will have direct access to the delegate objects. In this case, the caller could reassign the variable to a new delegate object (effectively deleting the current list of functions to call) and, worse yet, the caller would be able to directly invoke the delegate’s invocation list. To illustrate this problem, consider the following reworking (and simplification) of the Car class from the previous CarDelegate example: public class Car { public delegate void CarEngineHandler(string msgForCaller); // Now a public member! public CarEngineHandler listOfHandlers; // Just fire out the Exploded notification. public void Accelerate(int delta) { if (listOfHandlers != null) listOfHandlers(“Sorry, this car is dead...“); } } Notice that we no longer have private delegate member variables encapsulated with custom registration methods. Because these members are indeed public, the caller can directly access the listOfHandlers member variable and reassign this type to new CarEngineHandler objects and invoke the delegate whenever it so chooses: class Program { static void Main(string[] args) { Console.WriteLine(“***** Agh! No Encapsulation! *****\n“); // Make a Car. Car myCar = new Car(); // We have direct access to the delegate! myCar.listOfHandlers = new Car.CarEngineHandler(CallWhenExploded); myCar.Accelerate(10); // We can now assign to a whole new object... // confusing at best. myCar.listOfHandlers = new Car.CarEngineHandler(CallHereToo); myCar.Accelerate(10); // The caller can also directly invoke the delegate! myCar.listOfHandlers.Invoke(“hee, hee, hee...“); Console.ReadLine(); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 379 } static void CallWhenExploded(string msg) { Console.WriteLine(msg); } static void CallHereToo(string msg) { Console.WriteLine(msg); } } Exposing public delegate members breaks encapsulation, which not only can lead to code that is hard to maintain (and debug), but could also open your application to possible security risks! Here is the output of the current example: ***** Agh! No Encapsulation! ***** Sorry, this car is dead... Sorry, this car is dead... hee, hee, hee... Obviously, you would not want to give other applications the power to change what a delegate is pointing to or to invoke the members without your permission. Given this, it is common practice to declare private delegate member variables.  Source Code The PublicDelegateProblem project is located under the Chapter 10 subdirectory. The C# event Keyword As a shortcut, so you don’t have to build custom methods to add or remove methods to a delegate’s invocation list, C# provides the event keyword. When the compiler processes the event keyword, you are automatically provided with registration and unregistration methods, as well as any necessary member variables for your delegate types. These delegate member variables are always declared private and, therefore, they are not directly exposed from the object firing the event. To be sure, the event keyword can be used to simplify how a custom class sends out notifications to external objects. Defining an event is a two-step process. First, you need to define a delegate type that will hold the list of methods to be called when the event is fired. Next, you declare an event (using the C# event keyword) in terms of the related delegate type. To illustrate the event keyword, create a new Console Application named CarEvents. In this iteration of the Car class, we will define two events named AboutToBlow and Exploded. These events are associated to a single delegate type named CarEngineHandler. Here are the initial updates to the Car class: public class Car { // This delegate works in conjunction with the // Car's events. public delegate void CarEngineHandler(string msg); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 380 // This car can send these events. public event CarEngineHandler Exploded; public event CarEngineHandler AboutToBlow; ... } Sending an event to the caller is as simple as specifying the event by name, along with any required parameters as defined by the associated delegate. To ensure that the caller has indeed registered with the event, you will want to check the event against a null value before invoking the delegate’s method set. With these points in mind, here is the new iteration of the Car’s Accelerate() method: public void Accelerate(int delta) { // If the car is dead, fire Exploded event. if (carIsDead) { if (Exploded != null) Exploded(“Sorry, this car is dead...“); } else { CurrentSpeed += delta; // Almost dead? if (10 == MaxSpeed - CurrentSpeed && AboutToBlow != null) { AboutToBlow(“Careful buddy! Gonna blow!“); } // Still OK! if (CurrentSpeed >= MaxSpeed) carIsDead = true; else Console.WriteLine(“CurrentSpeed = {0}“, CurrentSpeed); } } With this, you have configured the car to send two custom events without having to define custom registration functions or declare delegate member variables. You will see the usage of this new automobile in just a moment, but first, let’s check the event architecture in a bit more detail. Events Under the Hood When the compiler processes the C# event keyword, it generates two hidden methods, one having an add_ prefix, the other having a remove_ prefix. Each prefix is followed by the name of the C# event. For example, the Exploded event results in two hidden methods named add_Exploded() and remove_Exploded(). If you were to check out the CIL instructions behind add_AboutToBlow(), you would find a call to the Delegate.Combine() method. Consider the partial CIL code: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 381 .method public hidebysig specialname instance void add_AboutToBlow(class CarEvents.Car/CarEngineHandler 'value') cil managed { ... call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine( class [mscorlib]System.Delegate, class [mscorlib]System.Delegate) ... } As you would expect, remove_AboutToBlow() will call Delegate.Remove() on your behalf: .method public hidebysig specialname instance void remove_AboutToBlow(class CarEvents.Car/CarEngineHandler 'value') cil managed { ... call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove( class [mscorlib]System.Delegate, class [mscorlib]System.Delegate) ... } Finally, the CIL code representing the event itself makes use of the .addon and .removeon directives to map the names of the correct add_XXX() and remove_XXX() methods to invoke: .event CarEvents.Car/EngineHandler AboutToBlow { .addon instance void CarEvents.Car::add_AboutToBlow (class CarEvents.Car/CarEngineHandler) .removeon instance void CarEvents.Car::remove_AboutToBlow (class CarEvents.Car/CarEngineHandler) } Now that you understand how to build a class that can send C# events (and are aware that events are little more than a typing time saver), the next big question is how to listen to the incoming events on the caller’s side. Listening to Incoming Events C# events also simplify the act of registering the caller-side event handlers. Rather than having to specify custom helper methods, the caller simply makes use of the += and -= operators directly (which triggers the correct add_XXX() or remove_XXX() method in the background). When you want to register with an event, follow the pattern shown here: // NameOfObject.NameOfEvent += new RelatedDelegate(functionToCall); // Car.CarEngineHandler d = new Car.CarEngineHandler(CarExplodedEventHandler); myCar.Exploded += d; When you want to detach from a source of events, use the -= operator, using the following pattern: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 382 // NameOfObject.NameOfEvent -= new RelatedDelegate(functionToCall); // myCar.Exploded -= d; Given these very predictable patterns, here is the refactored Main() method, now using the C# event registration syntax: class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with Events *****\n“); Car c1 = new Car(“SlugBug“, 100, 10); // Register event handlers. c1.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed); c1.AboutToBlow += new Car.CarEngineHandler(CarAboutToBlow); Car.CarEngineHandler d = new Car.CarEngineHandler(CarExploded); c1.Exploded += d; Console.WriteLine(“***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); // Remove CarExploded method // from invocation list. c1.Exploded -= d; Console.WriteLine(“\n***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } public static void CarAboutToBlow(string msg) { Console.WriteLine(msg); } public static void CarIsAlmostDoomed(string msg) { Console.WriteLine(“=> Critical Message from Car: {0}“, msg); } public static void CarExploded(string msg) { Console.WriteLine(msg); } } To even further simplify event registration, you can use method group conversion. Consider the following iteration of Main(): static void Main(string[] args) { Console.WriteLine(“***** Fun with Events *****\n“); Car c1 = new Car(“SlugBug“, 100, 10); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 383 // Register event handlers. c1.AboutToBlow += CarIsAlmostDoomed; c1.AboutToBlow += CarAboutToBlow; c1.Exploded += CarExploded; Console.WriteLine(“***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); c1.Exploded -= CarExploded; Console.WriteLine(“\n***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } Simplifying Event Registration Using Visual Studio Visual Studio offers assistance with the process of registering event handlers. When you apply the += syntax during event registration, you will find an IntelliSense window displayed, inviting you to hit the Tab key to autocomplete the associated delegate instance (see Figure 10-2), which is captured using method group conversion syntax. Figure 10-2. Delegate selection IntelliSense After you hit the Tab key, you are invited to enter the name of the event handler to be generated (or simply accept the default name), as shown in Figure 10-3. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 384 Figure 10-3. Delegate target format IntelliSense When you hit the Tab key again, you will be provided with stub code in the correct format of the delegate target (note that this method has been declared static due to the fact that the event was registered within a static method): static void newCar_AboutToBlow(string msg) { // Delete the following line and add your code! throw new NotImplementedException(); } IntelliSense is available to all .NET events in the base class libraries. This IDE feature is a massive time saver, given that it saves you from having to search the .NET help system to figure out both the correct delegate to use with a particular event and the format of the delegate target method.  Source Code The CarEvents project is located under the Chapter 10 subdirectory. Creating Custom Event Arguments Truth be told, there is one final enhancement we could make to the current iteration of the Car class that mirrors Microsoft’s recommended event pattern. As you begin to explore the events sent by a given type in the base class libraries, you will find that the first parameter of the underlying delegate is a System.Object, while the second parameter is a descendant of System.EventArgs. The System.Object argument represents a reference to the object that sent the event (such as the Car), while the second parameter represents information regarding the event at hand. The System.EventArgs base class represents an event that is not sending any custom information: public class EventArgs { public static readonly EventArgs Empty; public EventArgs(); } CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 385 For simple events, you can pass an instance of EventArgs directly. However, when you want to pass along custom data, you should build a suitable class deriving from EventArgs. For our example, assume you have a class named CarEventArgs, which maintains a string representing the message sent to the receiver: public class CarEventArgs : EventArgs { public readonly string msg; public CarEventArgs(string message) { msg = message; } } With this, you would now update the CarEngineHandler delegate type definition as follows (the events would be unchanged): public class Car { public delegate void CarEngineHandler(object sender, CarEventArgs e); ... } Here, when firing the events from within the Accelerate() method, you would now need to supply a reference to the current Car (via the this keyword) and an instance of the CarEventArgs type. For example, consider the following partial update: public void Accelerate(int delta) { // If the car is dead, fire Exploded event. if (carIsDead) { if (Exploded != null) Exploded(this, new CarEventArgs(“Sorry, this car is dead...“)); } ... } On the caller’s side, all you would need to do is update your event handlers to receive the incoming parameters and obtain the message via the read-only field. For example: public static void CarAboutToBlow(object sender, CarEventArgs e) { Console.WriteLine(“{0} says: {1}“, sender, e.msg); } If the receiver wants to interact with the object that sent the event, you can explicitly cast the System.Object. From this reference, you can make use of any public member of the object that sent the event notification: public static void CarAboutToBlow(object sender, CarEventArgs e) { // Just to be safe, perform a // runtime check before casting. if (sender is Car) CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 386 { Car c = (Car)sender; Console.WriteLine(“Critical Message from {0}: {1}“, c.PetName, e.msg); } }  Source Code The PrimAndProperCarEvents project is located under the Chapter 10 subdirectory. The Generic EventHandler Delegate Given that so many custom delegates take an object as the first parameter and an EventArgs descendant as the second, you could further streamline the previous example by using the generic EventHandler type, where T is your custom EventArgs type. Consider the following update to the Car type (notice how we no longer need to define a custom delegate type at all): public class Car { public event EventHandler Exploded; public event EventHandler AboutToBlow; ... } The Main() method could then use EventHandler anywhere we previously specified CarEventHandler (or, once again, use method group conversion): static void Main(string[] args) { Console.WriteLine(“***** Prim and Proper Events *****\n“); // Make a car as usual. Car c1 = new Car(“SlugBug“, 100, 10); // Register event handlers. c1.AboutToBlow += CarIsAlmostDoomed; c1.AboutToBlow += CarAboutToBlow; EventHandler d = new EventHandler(CarExploded); c1.Exploded += d; ... } Great! At this point, you have seen the core aspects of working with delegates and events in the C# language. While you could use this information for just about all of your callback needs, we will wrap up this chapter with a look at some final simplifications, specifically anonymous methods and lambda expressions. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 387  Source Code The GenericPrimAndProperCarEvents project is located under the Chapter 10 subdirectory. Understanding C# Anonymous Methods As you have seen, when a caller wants to listen to incoming events, it must define a custom method in a class (or structure) that matches the signature of the associated delegate. For example: class Program { static void Main(string[] args) { SomeType t = new SomeType(); // Assume “SomeDelegate“ can point to methods taking no // args and returning void. t.SomeEvent += new SomeDelegate(MyEventHandler); } // Typically only called by the SomeDelegate object. public static void MyEventHandler() { // Do something when event is fired. } } When you think about it, however, methods such as MyEventHandler() are seldom intended to be called by any part of the program other than the invoking delegate. As far as productivity is concerned, it is a bit of a bother (though in no way a show-stopper) to manually define a separate method to be called by the delegate object. To address this point, it is possible to associate an event directly to a block of code statements at the time of event registration. Formally, such code is termed an anonymous method. To illustrate the syntax, check out the following Main() method, which handles the events sent from the Car class using anonymous methods, rather than specifically named event handlers: class Program { static void Main(string[] args) { Console.WriteLine(“***** Anonymous Methods *****\n“); Car c1 = new Car(“SlugBug“, 100, 10); // Register event handlers as anonymous methods. c1.AboutToBlow += delegate { Console.WriteLine(“Eek! Going too fast!“); }; c1.AboutToBlow += delegate(object sender, CarEventArgs e) { CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 388 Console.WriteLine(“Message from Car: {0}“, e.msg); }; c1.Exploded += delegate(object sender, CarEventArgs e) { Console.WriteLine(“Fatal Message from Car: {0}“, e.msg); }; // This will eventually trigger the events. for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } }  Note The final curly bracket of an anonymous method must be terminated by a semicolon. If you fail to do so, you are issued a compilation error. Again, notice that the Program type no longer defines specific static event handlers such as CarAboutToBlow() or CarExploded(). Rather, the unnamed (a.k.a. anonymous) methods are defined inline at the time the caller is handling the event using the += syntax. The basic syntax of an anonymous method matches the following pseudo-code: class Program { static void Main(string[] args) { SomeType t = new SomeType(); t.SomeEvent += delegate (optionallySpecifiedDelegateArgs) { /* statements */ }; } } When handling the first AboutToBlow event within the previous Main() method, notice that you are not specifying the arguments passed from the delegate: c1.AboutToBlow += delegate { Console.WriteLine(“Eek! Going too fast!“); }; Strictly speaking, you are not required to receive the incoming arguments sent by a specific event. However, if you want to make use of the possible incoming arguments, you will need to specify the parameters prototyped by the delegate type (as shown in the second handling of the AboutToBlow and Exploded events). For example: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 389 c1.AboutToBlow += delegate(object sender, CarEventArgs e) { Console.WriteLine(“Critical Message from Car: {0}“, e.msg); }; Accessing Local Variables Anonymous methods are interesting in that they are able to access the local variables of the method that defines them. Formally speaking, such variables are termed outer variables of the anonymous method. A few important points about the interaction between an anonymous method scope and the scope of the defining method should be mentioned: • An anonymous method cannot access ref or out parameters of the defining method. • An anonymous method cannot have a local variable with the same name as a local variable in the outer method. • An anonymous method can access instance variables (or static variables, as appropriate) in the outer class scope. • An anonymous method can declare local variables with the same name as outer class member variables (the local variables have a distinct scope and hide the outer class member variables). Assume our Main() method defined a local integer named aboutToBlowCounter. Within the anonymous methods that handle the AboutToBlow event, we will increment this counter by one and print out the tally before Main() completes: static void Main(string[] args) { Console.WriteLine(“***** Anonymous Methods *****\n“); int aboutToBlowCounter = 0; // Make a car as usual. Car c1 = new Car(“SlugBug“, 100, 10); // Register event handlers as anonymous methods. c1.AboutToBlow += delegate { aboutToBlowCounter++; Console.WriteLine(“Eek! Going too fast!“); }; c1.AboutToBlow += delegate(object sender, CarEventArgs e) { aboutToBlowCounter++; Console.WriteLine(“Critical Message from Car: {0}“, e.msg); }; ... CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 390 Console.WriteLine(“AboutToBlow event was fired {0} times.“, aboutToBlowCounter); Console.ReadLine(); } After you run this updated Main() method, you will find the final Console.WriteLine() reports the AboutToBlow event was fired twice.  Source Code The AnonymousMethods project is located under the Chapter 10 subdirectory. Understanding Lambda Expressions To conclude our look at the .NET event architecture, we will examine C# lambda expressions. As just explained, C# supports the ability to handle events “inline” by assigning a block of code statements directly to an event using anonymous methods, rather than building a stand-alone method to be called by the underlying delegate. Lambda expressions are nothing more than a very concise way to author anonymous methods and ultimately simplify how we work with the .NET delegate type. To set the stage for our examination of lambda expressions, create a new Console Application named SimpleLambdaExpressions. To begin, consider the FindAll() method of the generic List class. This method can be called when you need to extract out a subset of items from the collection, and is prototyped like so: // Method of the System.Collections.Generic.List class. public List FindAll(Predicate match) As you can see, this method returns a new List that represents the subset of data. Also notice that the sole parameter to FindAll() is a generic delegate of type System.Predicate. This delegate can point to any method returning a bool, and takes a single type parameter as the only input parameter: // This delegate is used by FindAll() method // to extract out the subset. public delegate bool Predicate(T obj); When you call FindAll(), each item in the List is passed to the method pointed to by the Predicate object. The implementation of said method will perform some calculations to see whether the incoming data matches the necessary criteria, and return true or false. If this method returns true, the item will be added to the new List that represents the subset (got all that?). Before we see how lambda expressions can simplify working with FindAll(), let’s work the problem out in longhand notation, using the delegate objects directly. Add a method (named TraditionalDelegateSyntax()) within your Program type that interacts with the System.Predicate type to discover the even numbers in a List of integers: class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with Lambdas *****\n“); TraditionalDelegateSyntax(); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 391 Console.ReadLine(); } static void TraditionalDelegateSyntax() { // Make a list of integers. List list = new List(); list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 }); // Call FindAll() using traditional delegate syntax. Predicate callback = new Predicate(IsEvenNumber); List evenNumbers = list.FindAll(callback); Console.WriteLine(“Here are your even numbers:“); foreach (int evenNumber in evenNumbers) { Console.Write(“{0}\t“, evenNumber); } Console.WriteLine(); } // Target for the Predicate<> delegate. static bool IsEvenNumber(int i) { // Is it an even number? return (i % 2) == 0; } } Here, we have a method (IsEvenNumber()) that is in charge of testing the incoming integer parameter to see whether it is even or odd via the C# modulo operator, %. If you execute your application, you will find the numbers 20, 4, 8, and 44 print out to the console. While this traditional approach to working with delegates behaves as expected, the IsEvenNumber() method is invoked only in very limited circumstances—specifically when we call FindAll(), which leaves us with the baggage of a full method definition. If we were to instead use an anonymous method, our code would clean up considerably. Consider the following new method of the Program class: static void AnonymousMethodSyntax() { // Make a list of integers. List list = new List(); list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 }); // Now, use an anonymous method. List evenNumbers = list.FindAll(delegate(int i) { return (i % 2) == 0; } ); Console.WriteLine(“Here are your even numbers:“); foreach (int evenNumber in evenNumbers) { Console.Write(“{0}\t“, evenNumber); } CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 392 Console.WriteLine(); } In this case, rather than directly creating a Predicate delegate type and then authoring a stand- alone method, we are able to inline a method anonymously. While this is a step in the right direction, we are still required to use the delegate keyword (or a strongly typed Predicate), and we must ensure that the parameter list is a dead-on match: List evenNumbers = list.FindAll( delegate(int i) { return (i % 2) == 0; } ); Lambda expressions can be used to simplify the call to FindAll() even more. When you make use of lambda syntax, there is no trace of the underlying delegate object whatsoever. Consider the following new method to the Program class: static void LambdaExpressionSyntax() { // Make a list of integers. List list = new List(); list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 }); // Now, use a C# lambda expression. List evenNumbers = list.FindAll(i => (i % 2) == 0); Console.WriteLine(“Here are your even numbers:“); foreach (int evenNumber in evenNumbers) { Console.Write(“{0}\t“, evenNumber); } Console.WriteLine(); } In this case, notice the rather strange statement of code passed into the FindAll() method, which is in fact a lambda expression. In this iteration of the example, there is no trace whatsoever of the Predicate delegate (or the delegate keyword, for that matter). All we have specified is the lambda expression: i => (i % 2) == 0 Before I break this syntax down, first understand that lambda expressions can be used anywhere you would have used an anonymous method or a strongly typed delegate (typically with far fewer keystrokes). Under the hood, the C# compiler translates the expression into a standard anonymous method making use of the Predicate delegate type (which can be verified using ildasm.exe or reflector.exe). Specifically, the following code statement: // This lambda expression... List evenNumbers = list.FindAll(i => (i % 2) == 0); is compiled into the following approximate C# code: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 393 // ...becomes this anonymous method. List evenNumbers = list.FindAll(delegate (int i) { return (i % 2) == 0; }); Dissecting a Lambda Expression A lambda expression is written by first defining a parameter list, followed by the => token (C#’s token for the lambda operator found in the lambda calculus), followed by a set of statements (or a single statement) that will process these arguments. From a very high level, a lambda expression can be understood as follows: ArgumentsToProcess => StatementsToProcessThem Within our LambdaExpressionSyntax() method, things break down like so: // “i“ is our parameter list. // “(i % 2) == 0“ is our statement set to process “i“. List evenNumbers = list.FindAll(i => (i % 2) == 0); The parameters of a lambda expression can be explicitly or implicitly typed. Currently, the underlying data type representing the i parameter (an integer) is determined implicitly. The compiler is able to figure out that i is an integer based on the context of the overall lambda expression and the underlying delegate. However, it is also possible to explicitly define the type of each parameter in the expression, by wrapping the data type and variable name in a pair of parentheses, as follows: // Now, explicitly state the parameter type. List evenNumbers = list.FindAll((int i) => (i % 2) == 0); As you have seen, if a lambda expression has a single, implicitly typed parameter, the parentheses may be omitted from the parameter list. If you want to be consistent regarding your use of lambda parameters, you can always wrap the parameter list within parentheses, leaving us with this expression: List evenNumbers = list.FindAll((i) => (i % 2) == 0); Finally, notice that currently our expression has not been wrapped in parentheses (we have of course wrapped the modulo statement to ensure it is executed first before the test for equality). Lambda expressions do allow for the statement to be wrapped as follows: // Now, wrap the expression as well. List evenNumbers = list.FindAll((i) => ((i % 2) == 0)); Now that you have seen the various ways to build a lambda expression, how can we read this lambda statement in human-friendly terms? Leaving the raw mathematics behind, the following explanation fits the bill: // My list of parameters (in this case, a single integer named i) // will be processed by the expression (i % 2) == 0. List evenNumbers = list.FindAll((i) => ((i % 2) == 0)); CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 394 Processing Arguments Within Multiple Statements Our first lambda expression was a single statement that ultimately evaluated to a Boolean. However, as you know, many delegate targets must perform a number of code statements. For this reason, C# allows you to build lambda expressions using multiple statement blocks. When your expression must process the parameters using multiple lines of code, you can do so by denoting a scope for these statements using the expected curly brackets. Consider the following example update to our LambdaExpressionSyntax() method: static void LambdaExpressionSyntax() { // Make a list of integers. List list = new List(); list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 }); // Now process each argument within a group of // code statements. List evenNumbers = list.FindAll((i) => { Console.WriteLine(“value of i is currently: {0}“, i); bool isEven = ((i % 2) == 0); return isEven; }); Console.WriteLine(“Here are your even numbers:“); foreach (int evenNumber in evenNumbers) { Console.Write(“{0}\t“, evenNumber); } Console.WriteLine(); } In this case, our parameter list (again, a single integer named i) is being processed by a set of code statements. Beyond the calls to Console.WriteLine(), our modulo statement has been broken into two code statements for increased readability. Assuming each of the methods we’ve looked at in this section are called from within Main(): static void Main(string[] args) { Console.WriteLine(“***** Fun with Lambdas *****\n“); TraditionalDelegateSyntax(); AnonymousMethodSyntax(); Console.WriteLine(); LambdaExpressionSyntax(); Console.ReadLine(); } we will find the following output: CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 395 ***** Fun with Lambdas ***** Here are your even numbers: 20 4 8 44 Here are your even numbers: 20 4 8 44 value of i is currently: 20 value of i is currently: 1 value of i is currently: 4 value of i is currently: 8 value of i is currently: 9 value of i is currently: 44 Here are your even numbers: 20 4 8 44  Source Code The SimpleLambdaExpressions project can be found under the Chapter 10 subdirectory. Lambda Expressions with Multiple (or Zero) Parameters The lambda expressions you have seen here processed a single parameter. This is not a requirement, however, as a lambda expression may process multiple arguments (or none). To illustrate the first scenario, create a Console Application named LambdaExpressionsMultipleParams. Next, assume the following incarnation of the SimpleMath type: public class SimpleMath { public delegate void MathMessage(string msg, int result); private MathMessage mmDelegate; public void SetMathHandler(MathMessage target) {mmDelegate = target; } public void Add(int x, int y) { if (mmDelegate != null) mmDelegate.Invoke(“Adding has completed!“, x + y); } } Notice that the MathMessage delegate is expecting two parameters. To represent them as a lambda expression, our Main() method might be written as follows: static void Main(string[] args) { // Register with delegate as a lambda expression. CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 396 SimpleMath m = new SimpleMath(); m.SetMathHandler((msg, result) => {Console.WriteLine(“Message: {0}, Result: {1}“, msg, result);}); // This will execute the lambda expression. m.Add(10, 10); Console.ReadLine(); } Here, we are leveraging type inference, as our two parameters have not been strongly typed for simplicity. However, we could call SetMathHandler(), as follows: m.SetMathHandler((string msg, int result) => {Console.WriteLine(“Message: {0}, Result: {1}“, msg, result);}); Finally, if you are using a lambda expression to interact with a delegate taking no parameters at all, you may do so by supplying a pair of empty parentheses as the parameter. Thus, assuming you have defined the following delegate type: public delegate string VerySimpleDelegate(); you could handle the result of the invocation as follows: // Prints “Enjoy your string!“ to the console. VerySimpleDelegate d = new VerySimpleDelegate( () => {return “Enjoy your string!“;} ); Console.WriteLine(d());  Source Code The LambdaExpressionsMultipleParams project can be found under the Chapter 10 subdirectory. Retrofitting the CarEvents Example Using Lambda Expressions Given that the whole reason for lambda expressions is to provide a clean, concise manner to define an anonymous method (and therefore indirectly a manner to simplify working with delegates), let’s retrofit the PrimAndProperCarEvents project we created earlier in this chapter. Here is a simplified version of that project’s Program class, which makes use of lambda expression syntax (rather than the raw delegates) to hook into each event sent from the Car object: static void Main(string[] args) { Console.WriteLine(“***** More Fun with Lambdas *****\n“); // Make a car as usual. Car c1 = new Car(“SlugBug“, 100, 10); // Hook into events with lambdas! c1.AboutToBlow += (sender, e) => { Console.WriteLine(e.msg);}; c1.Exploded += (sender, e) => { Console.WriteLine(e.msg); }; CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS 397 // Speed up (this will generate the events). Console.WriteLine(“\n***** Speeding up *****“); for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); } Hopefully, at this point you can see the overall role of lambda expressions and understand how they provide a “functional manner” to work with anonymous methods and delegate types. Although the lambda operator (=>) might take a bit to get used to, always remember a lambda expression can be broken down to the following simple equation: ArgumentsToProcess => StatementsToProcessThem It is worth pointing out that the LINQ programming model also makes substantial use of lambda expressions to help simplify your coding efforts. You will examine LINQ beginning in Chapter 12.  Source Code The CarEventsWithLambdas project can be found under the Chapter 10 subdirectory. Summary In this chapter, you have examined a number of ways in which multiple objects can partake in a bidirectional conversation. First, you looked at the C# delegate keyword, which is used to indirectly construct a class derived from System.MulticastDelegate. As you saw, a delegate object maintains a list of methods to call when told to do so. These invocations may be made synchronously (using the Invoke() method) or asynchronously (via the BeginInvoke() and EndInvoke() methods). Again, the asynchronous nature of .NET delegate types will be examined in Chapter 19. You then examined the C# event keyword, which, when used in conjunction with a delegate type, can simplify the process of sending your event notifications to waiting callers. As shown via the resulting CIL, the .NET event model maps to hidden calls on the System.Delegate/System.MulticastDelegate types. In this light, the C# event keyword is purely optional in that it simply saves you some typing time. This chapter also explored a C# language feature termed anonymous methods. Using this syntactic construct, you are able to directly associate a block of code statements to a given event. As you have seen, anonymous methods are free to ignore the parameters sent by the event and have access to the “outer variables” of the defining method. You also examined a simplified way to register events using method group conversion. Finally, we wrapped things up by looking at the C# lambda operator, =>. As shown, this syntax is a great shorthand notation for authoring anonymous methods, where a stack of arguments can be passed into a group of statements for processing. Any method in the .NET platform that takes a delegate object as an argument can be substituted with a related lambda expression, which will typically simplify your code base quite a bit. C H A P T E R 11 399 Advanced C# Language Features In this chapter, you’ll deepen your understanding of the C# programming language by examining a number of more advanced syntactic constructs. To begin, you’ll learn how to implement and use an indexer method. This C# mechanism enables you to build custom types that provide access to internal subitems using an array-like syntax. After you learn how to build an indexer method, you’ll see how to overload various operators (+, -, <, >, and so forth), and how to create custom explicit and implicit conversion routines for your types (and you’ll learn why you might want to do this). Next, you’ll examine topics that are particularly useful when working with LINQ-centric APIs (though you can use them outside of the context of LINQ)—specifically extension methods and anonymous types. To wrap things up, you’ll learn how to create an “unsafe” code context in order to directly manipulate unmanaged pointers. While it is certainly true that using pointers in C# applications is a fairly infrequent activity, understanding how to do so can be helpful in some circumstances that involve complex interoperability scenarios. Understanding Indexer Methods As a programmer, you are certainly familiar with the process of accessing individual items contained within a simple array using the index operator ([]). For example: static void Main(string[] args) { // Loop over incoming command-line arguments // using index operator. for(int i = 0; i < args.Length; i++) Console.WriteLine(“Args: {0}“, args[i]); // Declare an array of local integers. int[] myInts = { 10, 9, 100, 432, 9874}; // Use the index operator to access each element. for(int j = 0; j < myInts.Length; j++) Console.WriteLine(“Index {0} = {1} “, j, myInts[j]); Console.ReadLine(); } This code is by no means a major newsflash. However, the C# language provides the capability to design custom classes and structures that may be indexed just like a standard array, by defining an CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 400 indexer method. This particular feature is most useful when you are creating custom collection classes (generic or nongeneric). Before examining how to implement a custom indexer, let’s begin by seeing one in action. Assume you have added support for an indexer method to the custom PersonCollection type developed in Chapter 9 (specifically, the IssuesWithNonGenericCollections project). While you have not yet added the indexer, observe the following usage within a new Console Application named SimpleIndexer: // Indexers allow you to access items in an array-like fashion. class Program { static void Main(string[] args) { Console.WriteLine(“***** Fun with Indexers *****\n“); PersonCollection myPeople = new PersonCollection(); // Add objects with indexer syntax. myPeople[0] = new Person(“Homer“, “Simpson“, 40); myPeople[1] = new Person(“Marge“, “Simpson“, 38); myPeople[2] = new Person(“Lisa“, “Simpson“, 9); myPeople[3] = new Person(“Bart“, “Simpson“, 7); myPeople[4] = new Person(“Maggie“, “Simpson“, 2); // Now obtain and display each item using indexer. for (int i = 0; i < myPeople.Count; i++) { Console.WriteLine(“Person number: {0}“, i); Console.WriteLine(“Name: {0} {1}“, myPeople[i].FirstName, myPeople[i].LastName); Console.WriteLine(“Age: {0}“, myPeople[i].Age); Console.WriteLine(); } } } As you can see, indexers allow you to manipulate the internal collection of subobjects just like a standard array. Now for the big question: how do you configure the PersonCollection class (or any custom class or structure) to support this functionality? An indexer is represented as a slightly modified C# property definition. In its simplest form, an indexer is created using the this[] syntax. Here is the required update for the PersonCollection class: // Add the indexer to the existing class definition. public class PersonCollection : IEnumerable { private ArrayList arPeople = new ArrayList(); // Custom indexer for this class. public Person this[int index] { get { return (Person)arPeople[index]; } set { arPeople.Insert(index, value); } } ... CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 401 } Apart from using the this keyword, the indexer looks just like any other C# property declaration. For example, the role of the get scope is to return the correct object to the caller. Here, we are doing so by delegating the request to the indexer of the ArrayList object, as this class also supports an indexer. The set scope is in charge of adding new Person objects, this is achieved by calling the Insert() method of the ArrayList. Indexers are yet another form of syntactic sugar, given that this functionality can also be achieved using “normal” public methods such as AddPerson() or GetPerson(). Nevertheless, when you support indexer methods on your custom collection types, they integrate well into the fabric of the .NET base class libraries. While creating indexer methods is quite commonplace when you are building custom collections, do remember that generic types give you this very functionality out of the box. Consider the following method, which makes use of a generic List of Person objects. Note that you can simply use the indexer of List directly. For example: static void UseGenericListOfPeople() { List myPeople = new List(); myPeople.Add(new Person(“Lisa“, “Simpson“, 9)); myPeople.Add(new Person(“Bart“, “Simpson“, 7)); // Change first person with indexer. myPeople[0] = new Person(“Maggie“, “Simpson“, 2); // Now obtain and display each item using indexer. for (int i = 0; i < myPeople.Count; i++) { Console.WriteLine(“Person number: {0}“, i); Console.WriteLine(“Name: {0} {1}“, myPeople[i].FirstName, myPeople[i].LastName); Console.WriteLine(“Age: {0}“, myPeople[i].Age); Console.WriteLine(); } }  Source Code The SimpleIndexer project is located under the Chapter 11 subdirectory. Indexing Data Using String Values The current PersonCollection class defined an indexer that allowed the caller to identify subitems using a numerical value. Understand, however, that this is not a requirement of an indexer method. Suppose you’d prefer to contain the Person objects using a System.Collections.Generic.Dictionary rather than an ArrayList. Given that Dictionary types allow access to the contained types using a string token (such as a person’s first name), you could define an indexer as follows: CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 402 public class PersonCollection : IEnumerable { private Dictionary listPeople = new Dictionary(); // This indexer returns a person based on a string index. public Person this[string name] { get { return (Person)listPeople[name]; } set { listPeople[name] = value; } } public void ClearPeople() { listPeople.Clear(); } public int Count { get { return listPeople.Count; } } IEnumerator IEnumerable.GetEnumerator() { return listPeople.GetEnumerator(); } } The caller would now be able to interact with the contained Person objects as shown here: static void Main(string[] args) { Console.WriteLine(“***** Fun with Indexers *****\n“); PersonCollection myPeople = new PersonCollection(); myPeople[“Homer“] = new Person(“Homer“, “Simpson“, 40); myPeople[“Marge“] = new Person(“Marge“, “Simpson“, 38); // Get “Homer“ and print data. Person homer = myPeople[“Homer“]; Console.WriteLine(homer.ToString()); Console.ReadLine(); } Again, if you were to use the generic Dictionary type directly, you’d gain the indexer method functionality out of the box, without building a custom, nongeneric class supporting a string indexer. Nevertheless, do understand that the data type of any indexer will be based on how the supporting collection type allows the caller to retrieve subitems.  Source Code The StringIndexer project is located under the Chapter 11 subdirectory. CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 403 Overloading Indexer Methods Understand that indexer methods may be overloaded on a single class or structure. Thus, if it makes sense to allow the caller to access subitems using a numerical index or a string value, you might define multiple indexers for a single type. By way of example, in ADO.NET (.NET’s native database-access API), the DataSet class supports a property named Tables, which returns to you a strongly typed DataTableCollection type. As it turns out, DataTableCollection defines three indexers to get and set DataTable objects—one by ordinal position, and the others by a friendly string moniker and optional containing namespace, as shown here: public sealed class DataTableCollection : InternalDataCollectionBase { ... // Overloaded indexers! public DataTable this[string name] { get; } public DataTable this[string name, string tableNamespace] { get; } public DataTable this[int index] { get; } } It is very common for types in the base class libraries to support indexer methods. So be aware, even if your current project does not require you to build custom indexers for your classes and structures, that many types already support this syntax. Indexers with Multiple Dimensions You can also create an indexer method that takes multiple parameters. Assume you have a custom collection that stores subitems in a 2D array. If this is the case, you may define an indexer method as follows: public class SomeContainer { private int[,] my2DintArray = new int[10, 10]; public int this[int row, int column] { /* get or set value from 2D array */ } } Again, unless you are building a highly stylized custom collection class, you won’t have much need to build a multidimensional indexer. Still, once again ADO.NET showcases how useful this construct can be. The ADO.NET DataTable is essentially a collection of rows and columns, much like a piece of graph paper or the general structure of a Microsoft Excel spreadsheet. While DataTable objects are typically populated on your behalf using a related “data adapter,” the following code illustrates how to manually create an in-memory DataTable containing three columns (for the first name, last name, and age of each record). Notice how once we have added a single row to the DataTable, we use a multidimensional indexer to drill into each column of the first (and only) row. (If you are following along, you’ll need to import the System.Data namespace into your code file.) static void MultiIndexerWithDataTable() { // Make a simple DataTable with 3 columns. DataTable myTable = new DataTable(); myTable.Columns.Add(new DataColumn(“FirstName“)); myTable.Columns.Add(new DataColumn(“LastName“)); CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 404 myTable.Columns.Add(new DataColumn(“Age“)); // Now add a row to the table. myTable.Rows.Add(“Mel“, “Appleby“, 60); // Use multidimension indexer to get details of first row. Console.WriteLine(“First Name: {0}“, myTable.Rows[0][0]); Console.WriteLine(“Last Name: {0}“, myTable.Rows[0][1]); Console.WriteLine(“Age : {0}“, myTable.Rows[0][2]); } Do be aware that we’ll take a rather deep dive into ADO.NET beginning with Chapter 21, so if some of the previous code seems unfamiliar, fear not. The main point of this example is that indexer methods can support multiple dimensions, and if used correctly, can simplify the way you interact with contained subobjects in custom collections. Indexer Definitions on Interface Types Indexers can be defined on a given .NET interface type to allow supporting types to provide a custom implementation. Here is a simple example of an interface that defines a protocol for obtaining string objects using a numerical indexer: public interface IStringContainer { string this[int index] { get; set; } } With this interface definition, any class or structure that implements this interface must now support a read/write indexer that manipulates subitems using a numerical value. Here is a partial implementation of such as class: class SomeClass : IStringContainer { private List myStrings = new List(); public string this[int index] { get { return myStrings[index]; } set { myStrings.Insert(index, value); } } } That wraps up the first major topic of this chapter. Now let’s examine a language feature that lets you build custom classes or structures that respond uniquely to the intrinsic operators of C#. Next, allow me to introduce the concept of operator overloading. Understanding Operator Overloading C#, like any programming language, has a canned set of tokens that are used to perform basic operations on intrinsic types. For example, you know that the + operator can be applied to two integers in order to yield a larger integer: CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 405 // The + operator with ints. int a = 100; int b = 240; int c = a + b; // c is now 340 Once again, this is no major newsflash, but have you ever stopped and noticed how the same + operator can be applied to most intrinsic C# data types? For example, consider this code: // + operator with strings. string s1 = “Hello“; string s2 = “ world!“; string s3 = s1 + s2; // s3 is now “Hello world!“ In essence, the + operator functions in specific ways based on the supplied data types (strings or integers, in this case). When the + operator is applied to numerical types, the result is the summation of the operands. However, when the + operator is applied to string types, the result is string concatenation. The C# language gives you the capability to build custom classes and structures that also respond uniquely to the same set of basic tokens (such as the + operator). While not every possible C# operator can be overloaded, many can, as shown in Table 11-1. Table 11-1. Overloadability of C# Operators C# Operator Overloadability +, -,! , ~, ++, --, true, false These unary operators can be overloaded. +, -, *, /, %, &, |, ^, <<, >> These binary operators can be overloaded. ==,!=, <, >, <=, >= These comparison operators can be overloaded. C# demands that “like” operators (i.e., < and >, <= and >=, == and !=) are overloaded together. [] The [] operator cannot be overloaded. As you saw earlier in this chapter, however, the indexer construct provides the same functionality. () The () operator cannot be overloaded. As you will see later in this chapter, however, custom conversion methods provide the same functionality. +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= Shorthand assignment operators cannot be overloaded; however, you receive them as a freebie when you overload the related binary operator. Overloading Binary Operators To illustrate the process of overloading binary operators, assume the following simple Point class is defined in a new Console Application named OverloadedOps: CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 406 // Just a simple, everyday C# class. public class Point { public int X {get; set;} public int Y {get; set;} public Point(int xPos, int yPos) { X = xPos; Y = yPos; } public override string ToString() { return string.Format(“[{0}, {1}]“, this.X, this.Y); } } Now, logically speaking, it makes sense to “add” Points together. For example, if you added together two Point variables, you should receive a new Point that is the summation of the X and Y values. Of course, it might also be helpful to subtract one Point from another. Ideally, you would like to be able to author the following code: // Adding and subtracting two points? static void Main(string[] args) { Console.WriteLine(“***** Fun with Overloaded Operators *****\n“); // Make two points. Point ptOne = new Point(100, 100); Point ptTwo = new Point(40, 40); Console.WriteLine(“ptOne = {0}“, ptOne); Console.WriteLine(“ptTwo = {0}“, ptTwo); // Add the points to make a bigger point? Console.WriteLine(“ptOne + ptTwo: {0} “, ptOne + ptTwo); // Subtract the points to make a smaller point? Console.WriteLine(“ptOne - ptTwo: {0} “, ptOne - ptTwo); Console.ReadLine(); } However, as our Point now stands, we will receive compile-time errors, as the Point type does not know how to respond to the + or - operators. To equip a custom type to respond uniquely to intrinsic operators, C# provides the operator keyword, which you can use only in conjunction with the static keyword. When you overload a binary operator (such as + and -), you will most often pass in two arguments that are the same type as the defining class (a Point in this example), as illustrated in the following code update: // A more intelligent Point type. public class Point { ... CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 407 // Overloaded operator +. public static Point operator + (Point p1, Point p2) { return new Point(p1.X + p2.X, p1.Y + p2.Y); } // Overloaded operator -. public static Point operator - (Point p1, Point p2) { return new Point(p1.X - p2.X, p1.Y - p2.Y); } } The logic behind operator + is simply to return a brand-new Point object based on the summation of the fields of the incoming Point parameters. Thus, when you write pt1 + pt2, under the hood you can envision the following hidden call to the static operator + method: // Pseudo-code: Point p3 = Point.operator+ (p1, p2) Point p3 = p1 + p2; Likewise, p1 – p2 maps to the following: // Pseudo-code: Point p4 = Point.operator- (p1, p2) Point p4 = p1 - p2; With this update, our program now compiles, and we find we are able to add and subtract Point objects, as seen in the following output: ptOne = [100, 100] ptTwo = [40, 40] ptOne + ptTwo: [140, 140] ptOne - ptTwo: [60, 60] When you are overloading a binary operator, you are not required to pass in two parameters of the same type. If it makes sense to do so, one of the arguments can differ. For example, here is an overloaded operator + that allows the caller to obtain a new Point that is based on a numerical adjustment: public class Point { ... public static Point operator + (Point p1, int change) { return new Point(p1.X + change, p1.Y + change); } public static Point operator + (int change, Point p1) { return new Point(p1.X + change, p1.Y + change); } } CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 408 Notice that you need both versions of the method if you want the arguments to be passed in either order (i.e., you can’t just define one of the methods and expect the compiler to automatically support the other one). We are now able to use these new versions of operator + as follows: // Prints [110, 110]. Point biggerPoint = ptOne + 10; Console.WriteLine(“ptOne + 10 = {0}“, biggerPoint); // Prints [120, 120]. Console.WriteLine(“10 + biggerPoint = {0}“, 10 + biggerPoint); Console.WriteLine(); And What of the += and –+ Operators? If you are coming to C# from a C++ background, you might lament the loss of overloading the shorthand assignment operators (+=, -=, and so forth). Don’t despair. In terms of C#, the shorthand assignment operators are automatically simulated if a type overloads the related binary operator. Thus, given that the Point structure has already overloaded the + and - operators, you can write the following: // Overloading binary operators results in a freebie shorthand operator. static void Main(string[] args) { ... // Freebie += Point ptThree = new Point(90, 5); Console.WriteLine(“ptThree = {0}“, ptThree); Console.WriteLine(“ptThree += ptTwo: {0}“, ptThree += ptTwo); // Freebie -= Point ptFour = new Point(0, 500); Console.WriteLine(“ptFour = {0}“, ptFour); Console.WriteLine(“ptFour -= ptThree: {0}“, ptFour -= ptThree); Console.ReadLine(); } Overloading Unary Operators C# also allows you to overload various unary operators, such as ++ and --. When you overload a unary operator, you also must use the static keyword with the operator keyword; however, in this case you simply pass in a single parameter that is the same type as the defining class/structure. For example, if you were to update the Point with the following overloaded operators: public class Point { ... // Add 1 to the X/Y values for the incoming Point. public static Point operator ++(Point p1) { return new Point(p1.X+1, p1.Y+1); } CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 409 // Subtract 1 from the X/Y values for the incoming Point. public static Point operator --(Point p1) { return new Point(p1.X-1, p1.Y-1); } } you could increment and decrement Point’s x and y values like this: static void Main(string[] args) { ... // Applying the ++ and -- unary operators to a Point. Point ptFive = new Point(1, 1); Console.WriteLine(“++ptFive = {0}“, ++ptFive); // [2, 2] Console.WriteLine(“--ptFive = {0}“, --ptFive); // [1, 1] // Apply same operators as postincrement/decrement. Point ptSix = new Point(20, 20); Console.WriteLine(“ptSix++ = {0}“, ptSix++); // [20, 20] Console.WriteLine(“ptSix-- = {0}“, ptSix--); // [21, 21] Console.ReadLine(); } Notice in the preceding code example we are applying our custom ++ and -- operators in two different manners. In C++, it is possible to overload pre- and postincrement/decrement operators separately. This is not possible in C#. However, the return value of the increment/decrement is automatically handled “correctly” free of charge (i.e., for an overloaded ++ operator, pt++ has the value of the unmodified object as its value within an expression, while ++pt has the new value applied before use in the expression). Overloading Equality Operators As you might recall from Chapter 6, System.Object.Equals() can be overridden to perform value-based (rather than referenced-based) comparisons between reference types. If you choose to override Equals() (and the often related System.Object.GetHashCode() method), it is trivial to overload the equality operators (== and !=). To illustrate, here is the updated Point type: // This incarnation of Point also overloads the == and != operators. public class Point { ... public override bool Equals(object o) { return o.ToString() == this.ToString(); } public override int GetHashCode() { return this.ToString().GetHashCode(); } CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 410 // Now let's overload the == and != operators. public static bool operator ==(Point p1, Point p2) { return p1.Equals(p2); } public static bool operator !=(Point p1, Point p2) { return !p1.Equals(p2); } } Notice how the implementation of operator == and operator != simply makes a call to the overridden Equals() method to get the bulk of the work done. Given this, you can now exercise your Point class as follows: // Make use of the overloaded equality operators. static void Main(string[] args) { ... Console.WriteLine(“ptOne == ptTwo : {0}“, ptOne == ptTwo); Console.WriteLine(“ptOne != ptTwo : {0}“, ptOne != ptTwo); Console.ReadLine(); } As you can see, it is quite intuitive to compare two objects using the well-known == and != operators, rather than making a call to Object. Equals(). If you do overload the equality operators for a given class, keep in mind that C# demands that if you override the == operator, you must also override the != operator (if you forget, the compiler will let you know). Overloading Comparison Operators In Chapter 8, you learned how to implement the IComparable interface in order to compare the relationship between two like objects. You can, in fact, also overload the comparison operators (<, >, <=, and >=) for the same class. As with the equality operators, C# demands that if you overload <, you must also overload >. The same holds true for the <= and >= operators. If the Point type overloaded these comparison operators, the object user could now compare Points, as follows: // Using the overloaded < and > operators. static void Main(string[] args) { ... Console.WriteLine(“ptOne < ptTwo : {0}“, ptOne < ptTwo); Console.WriteLine(“ptOne > ptTwo : {0}“, ptOne > ptTwo); Console.ReadLine(); } Assuming you have implemented the IComparable interface (or better yet, the generic equivalent), overloading the comparison operators is trivial. Here is the updated class definition: // Point is also comparable using the comparison operators. public class Point : IComparable { CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 411 ... public int CompareTo(Point other) { if (this.X > other.X && this.Y > other.Y) return 1; if (this.X < other.X && this.Y < other.Y) return -1; else return 0; } public static bool operator <(Point p1, Point p2) { return (p1.CompareTo(p2) < 0); } public static bool operator >(Point p1, Point p2) { return (p1.CompareTo(p2) > 0); } public static bool operator <=(Point p1, Point p2) { return (p1.CompareTo(p2) <= 0); } public static bool operator >=(Point p1, Point p2) { return (p1.CompareTo(p2) >= 0); } } Final Thoughts Regarding Operator Overloading As you have seen, C# provides the capability to build types that can respond uniquely to various intrinsic, well-known operators. Now, before you go and retrofit all your classes to support such behavior, you must be sure that the operator(s) you are about to overload make some sort of logical sense in the world at large. For example, let’s say you overloaded the multiplication operator for the MiniVan class. What exactly would it mean to multiply two MiniVan objects? Not much. In fact, it would be very confusing for teammates to see the following use of MiniVan objects: // Huh?! This is far from intuitive... MiniVan newVan = myVan * yourVan; Overloading operators is generally useful only when you’re building atomic data types. Text, points, rectangles, fractions, and hexagons make good candidates for operator overloading. People, managers, cars, database connections, and web pages do not. As a rule of thumb, if an overloaded operator makes it harder for the user to understand a type’s functionality, don’t do it. Use this feature wisely.  Source Code The OverloadedOps project is located under the Chapter 11 subdirectory. CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 412 Understanding Custom Type Conversions Let’s now examine a topic closely related to operator overloading: custom type conversions. To set the stage for the discussion, let’s quickly review the notion of explicit and implicit conversions between numerical data and related class types. Recall: Numerical Conversions In terms of the intrinsic numerical types (sbyte, int, float, etc.), an explicit conversion is required when you attempt to store a larger value in a smaller container, as this could result in a loss of data. Basically, this is your way to tell the compiler, “Leave me alone, I know what I am trying to do.” Conversely, an implicit conversion happens automatically when you attempt to place a smaller type in a destination type that will not result in a loss of data: static void Main() { int a = 123; long b = a; // Implicit conversion from int to long. int c = (int) b; // Explicit conversion from long to int. } Recall: Conversions Among Related Class Types As shown in Chapter 6, class types may be related by classical inheritance (the “is-a” relationship). In this case, the C# conversion process allows you to cast up and down the class hierarchy. For example, a derived class can always be implicitly cast to a base type. However, if you want to store a base class type in a derived variable, you must perform an explicit cast, like so: // Two related class types. class Base{} class Derived : Base{} class Program { static void Main(string[] args) { // Implicit cast between derived to base. Base myBaseType; myBaseType = new Derived(); // Must explicitly cast to store base reference // in derived type. Derived myDerivedType = (Derived)myBaseType; } } This explicit cast works due to the fact that the Base and Derived classes are related by classical inheritance. However, what if you have two class types in different hierarchies with no common parent (other than System.Object) that require conversions? Given that they are not related by classical inheritance, typical casting operations offer no help. CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 413 On a related note, consider value types (structures). Assume you have two .NET structures named Square and Rectangle. Given that structures cannot leverage classic inheritance (as they are always sealed), you have no natural way to cast between these seemingly related types. While you could create helper methods in the structures (such as Rectangle.ToSquare()), C# lets you build custom conversion routines that allow your types to respond to the () casting operator. Therefore, if you configured the structures correctly, you would be able to use the following syntax to explicitly convert between them as follows: // Convert a Rectangle to a Square! Rectangle rect; rect.Width = 3; rect.Height = 10; Square sq = (Square)rect; Creating Custom Conversion Routines Begin by creating a new Console Application named CustomConversions. C# provides two keywords, explicit and implicit, that you can use to control how your types respond during an attempted conversion. Assume you have the following structure definitions: public struct Rectangle { public int Width {get; set;} public int Height {get; set;} public Rectangle(int w, int h) : this() { Width = w; Height = h; } public void Draw() { for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { Console.Write(“*“); } Console.WriteLine(); } } public override string ToString() { return string.Format(“[Width = {0}; Height = {1}]“, Width, Height); } } public struct Square { public int Length {get; set;} CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 414 public Square(int l) : this() { Length = l; } public void Draw() { for (int i = 0; i < Length; i++) { for (int j = 0; j < Length; j++) { Console.Write(“*“); } Console.WriteLine(); } } public override string ToString() { return string.Format(“[Length = {0}]“, Length); } // Rectangles can be explicitly converted // into Squares. public static explicit operator Square(Rectangle r) { Square s = new Square(); s.Length = r.Height; return s; } }  Note You’ll notice in the Square and Rectangle constructors, I am explicitly chaining to the default constructor. The reason is that if you have a structure, which makes use of automatic property syntax (as we do here), the default constructor must be explicitly called (from all custom constructors) to initialize the private backing fields. Yes, this is a very quirky rule of C#, but after all, this is an advanced topics chapter. Notice that this iteration of the Square type defines an explicit conversion operator. Like the process of overloading an operator, conversion routines make use of the C# operator keyword, in conjunction with the explicit or implicit keyword, and must be defined as static. The incoming parameter is the entity you are converting from, while the operator type is the entity you are converting to. In this case, the assumption is that a square (being a geometric pattern in which all sides are of equal length) can be obtained from the height of a rectangle. Thus, you are free to convert a Rectangle into a Square, as follows: static void Main(string[] args) { Console.WriteLine(“***** Fun with Conversions *****\n“); CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 415 // Make a Rectangle. Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); // Convert r into a Square, // based on the height of the Rectangle. Square s = (Square)r; Console.WriteLine(s.ToString()); s.Draw(); Console.ReadLine(); } The output can be seen here: ***** Fun with Conversions ***** [Width = 15; Height = 4] *************** *************** *************** *************** [Length = 4] **** **** **** **** While it may not be all that helpful to convert a Rectangle into a Square within the same scope, assume you have a function that has been designed to take Square parameters. // This method requires a Square type. static void DrawSquare(Square sq) { Console.WriteLine(sq.ToString()); sq.Draw(); } Using your explicit conversion operation on the Square type, you can now pass in Rectangle types for processing using an explicit cast, like so: static void Main(string[] args) { ... // Convert Rectangle to Square to invoke method. Rectangle rect = new Rectangle(10, 5); CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 416 DrawSquare((Square)rect); Console.ReadLine(); } Additional Explicit Conversions for the Square Type Now that you can explicitly convert Rectangles into Squares, let’s examine a few additional explicit conversions. Given that a square is symmetrical on all sides, it might be helpful to provide an explicit conversion routine that allows the caller to cast from an integer type into a Square (which, of course, will have a side length equal to the incoming integer). Likewise, what if you were to update Square such that the caller can cast from a Square into an int? Here is the calling logic: static void Main(string[] args) { ... // Converting an int to a Square. Square sq2 = (Square)90; Console.WriteLine(“sq2 = {0}“, sq2); // Converting a Square to an int. int side = (int)sq2; Console.WriteLine(“Side length of sq2 = {0}“, side); Console.ReadLine(); } and here is the update to the Square class: public struct Square { ... public static explicit operator Square(int sideLength) { Square newSq = new Square(); newSq.Length = sideLength; return newSq; } public static explicit operator int (Square s) {return s.Length;} } To be honest, converting from a Square into an integer may not be the most intuitive (or useful) operation. However, it does point out a very important fact regarding custom conversion routines: the compiler does not care what you convert to or from, as long as you have written syntactically correct code. Thus, as with overloading operators, just because you can create an explicit cast operation for a given type does not mean you should. Typically, this technique will be most helpful when you’re creating .NET structure types, given that they are unable to participate in classical inheritance (where casting comes for free). CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 417 Defining Implicit Conversion Routines So far, you have created various custom explicit conversion operations. However, what about the following implicit conversion? static void Main(string[] args) { ... Square s3 = new Square(); s3.Length = 83; // Attempt to make an implicit cast? Rectangle rect2 = s3; Console.ReadLine(); } This code will not compile, given that you have not provided an implicit conversion routine for the Rectangle type. Now here is the catch: it is illegal to define explicit and implicit conversion functions on the same type if they do not differ by their return type or parameter set. This might seem like a limitation; however, the second catch is that when a type defines an implicit conversion routine, it is legal for the caller to make use of the explicit cast syntax! Confused? To clear things up, let’s add an implicit conversion routine to the Rectangle structure using the C# implicit keyword (note that the following code assumes the width of the resulting Rectangle is computed by multiplying the side of the Square by 2): public struct Rectangle { ... public static implicit operator Rectangle(Square s) { Rectangle r = new Rectangle(); r.Height = s.Length; // Assume the length of the new Rectangle with // (Length x 2). r.Width = s.Length * 2; return r; } } With this update, you are now able to convert between types, as follows: static void Main(string[] args) { ... // Implicit cast OK! Square s3 = new Square(); s3.Length= 7; Rectangle rect2 = s3; Console.WriteLine(“rect2 = {0}“, rect2); CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 418 // Explicit cast syntax still OK! Square s4 = new Square(); s4.Length = 3; Rectangle rect3 = (Rectangle)s4; Console.WriteLine(“rect3 = {0}“, rect3); Console.ReadLine(); } That wraps up our look at defining custom conversion routines. As with overloaded operators, remember that this bit of syntax is simply a shorthand notation for “normal” member functions, and in this light it is always optional. When used correctly, however, custom structures can be used more naturally, as they can be treated as true class types related by inheritance.  Source Code The CustomConversions project is located under the Chapter 11 subdirectory. Understanding Extension Methods .NET 3.5 introduced the concept of extension methods, which allow you to add new methods or properties to a class or structure, without modifying the original type in any direct manner. So where might this be helpful? Consider the following possibilities. First off, say you have a given class that is in production. It becomes clear over time that this class should support a handful of new members. If you were to modify the current class definition directly, you risk the possibility of breaking backward compatibility with older code bases making use of it, as they might not have been compiled with the latest and greatest class definition. One way to ensure backward compatibility would be to create a new derived class from the existing parent; however, now you have two classes to maintain. As we all know, code maintenance is least glamorous part of a software engineer’s job description. Now consider this situation. Let’s say you have a structure (or maybe a sealed class) and would like to add new members so that it behaves polymorphically in your system. Since structures and sealed classes cannot be extended, your only choice is to add the members to the type, once again risking backward compatibility! Using extension methods, you are able to modify types without subclassing and without modifying the type directly. To be sure, this technique is essentially a smoke and mirror show. The new functionality is only offered to a type if the extension methods have been referenced for use in your current project. Defining Extension Methods When you define extension methods, the first restriction is that they must be defined within a static class (see Chapter 5) and, therefore, each extension method must be declared with the static keyword. The second point is that all extension methods are marked as such by using the this keyword as a modifier on the first (and only the first) parameter of the method in question. The “this qualified” parameter represents the item being extended. To illustrate, create a new Console Application named ExtensionMethods. Now, assume you are authoring a class named MyExtensions that defines two extension methods. The first method allows any CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 419 object to use a brand-new method named DisplayDefiningAssembly() that makes use of types in the System.Reflection namespace to display the name of the assembly containing the type in question.  Note You will formally examine the reflection API in Chapter 15. If you are new to the topic, simply understand that reflection allows you to discover the structure of assemblies, types, and type members at runtime. The second extension method, named ReverseDigits(), allows any int to obtain a new version of itself where the value is reversed digit by digit. For example, if an integer with the value 1234 called ReverseDigits(), the integer returned is set to the value 4321. Consider the following class implementation (be sure to import the System.Reflection namespace if you are following along): static class MyExtensions { // This method allows any object to display the assembly // it is defined in. public static void DisplayDefiningAssembly(this object obj) { Console.WriteLine(“{0} lives here: => {1}\n“, obj.GetType().Name, Assembly.GetAssembly(obj.GetType()).GetName().Name); } // This method allows any integer to reverse its digits. // For example, 56 would return 65. public static int ReverseDigits(this int i) { // Translate int into a string, and then // get all the characters. char[] digits = i.ToString().ToCharArray(); // Now reverse items in the array. Array.Reverse(digits); // Put back into string. string newDigits = new string(digits); // Finally, return the modified string back as an int. return int.Parse(newDigits); } } Again, note how the first parameter of each extension method has been qualified with the this keyword, before defining the parameter type. It is always the case that the first parameter of an extension method represents the type being extended. Given that DisplayDefiningAssembly() has been prototyped to extend System.Object, every type now has this new member, as Object is the parent to all types in the .NET platform. However, ReverseDigits() has been prototyped to only extend integer types and, therefore, if anything other than an integer attempts to invoke this method, you will receive a compile- time error. CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 420  Note Understand that a given extension method can have multiple parameters, but only the first parameter can be qualified with this. The additional parameters would be treated as normal incoming parameters for use by the method. Invoking Extension Methods Now that we have these extension methods in place, consider the following Main() method: static void Main(string[] args) { Console.WriteLine(“***** Fun with Extension Methods *****\n“); // The int has assumed a new identity! int myInt = 12345678; myInt.DisplayDefiningAssembly(); // So has the DataSet! System.Data.DataSet d = new System.Data.DataSet(); d.DisplayDefiningAssembly(); // And the SoundPlayer! System.Media.SoundPlayer sp = new System.Media.SoundPlayer(); sp.DisplayDefiningAssembly(); // Use new integer functionality. Console.WriteLine(“Value of myInt: {0}“, myInt); Console.WriteLine(“Reversed digits of myInt: {0}“, myInt.ReverseDigits()); Console.ReadLine(); } Here is the output (minus the beeping): ***** Fun with Extension Methods ***** Int32 lives here: => mscorlib DataSet lives here: => System.Data SoundPlayer lives here: => System Value of myInt: 12345678 Reversed digits of myInt: 87654321 CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 421 Importing Extension Methods When you define a class containing extension methods, it will no doubt be defined within a .NET namespace. If this namespace is different from the namespace using the extension methods, you will need to make use of the expected C# using keyword. When you do, your code file has access to all extension methods for the type being extended. This is important to remember, because if you do not explicitly import the correct namespace, the extension methods are not available for that C# code file. In effect, although it can appear on the surface that extension methods are global in nature, they are in fact limited to the namespaces that define them or the namespaces that import them. Thus, if we wrap the MyExtensions class into a namespace named MyExtensionMethods, as follows: namespace MyExtensionMethods { static class MyExtensions { ... } } other namespaces in the project would need to explicitly import the MyExtensionMethods namespace to gain the extension methods defined by our class.  Note It is common practice to not only isolate extension methods into a dedicated .NET namespace, but into a dedicated class library. In this way, new applications can “opt-in” to extensions by explicitly referencing the correct library. Chapter 14 will examine the details of building and using custom .NET class libraries. The IntelliSense of Extension Methods Given the fact that extension methods are not literally defined on the type being extended, it is certainly possible to become confused when examining an existing code base. For example, assume you have imported a namespace that defined some number of extension methods authored by a teammate. As you are authoring your code, you might create a variable of the extended type, apply the dot operator, and find dozens of new methods that are not members of the original class definition! Thankfully, Visual Studio’s IntelliSense mechanism marks all extension methods with a unique, down-arrow icon (see Figure 11-1). CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 422 Figure 11-1. The IntelliSense of extension methods Any method marked with this visual icon is a friendly reminder that the method is defined outside of the original class definition via an extension method.  Source Code The ExtensionMethods project can be found under the Chapter 11 subdirectory. Extending Types Implementing Specific Interfaces At this point, you have seen how to extend classes (and, indirectly, structures that follow the same syntax) with new functionality via extension methods. It is also possible to define an extension method that can only extend a class or structure that implements the correct interface. For example, you could say something to the effect of “if a class or structure implements IEnumerable, then that type gets the following new members.” Of course, it is possible to demand that a type support any interface at all, including your own custom interfaces. To illustrate, create a new Console Application named InterfaceExtensions. The goal here is to add a new method to any type that implements IEnumerable, which would include any array and many nongeneric collection classes (recall from Chapter 8 that the generic IEnumerable interface extends the nongeneric IEnumerable interface). Add the following extension class to your new project: static class AnnoyingExtensions { public static void PrintDataAndBeep(this System.Collections.IEnumerable iterator) { foreach (var item in iterator) { Console.WriteLine(item); Console.Beep(); CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 423 } } } Given that the PrintDataAndBeep() method can be used by any class or structure that implements IEnumerable, we could test via the following Main() method: static void Main( string[] args ) { Console.WriteLine(“***** Extending Interface Compatible Types *****\n“); // System.Array implements IEnumerable! string[] data = { “Wow“, “this“, “is“, “sort“, “of“, “annoying“, “but“, “in“, “a“, “weird“, “way“, “fun!“}; data.PrintDataAndBeep(); Console.WriteLine(); // List implements IEnumerable! List myInts = new List() {10, 15, 20}; myInts.PrintDataAndBeep(); Console.ReadLine(); } That wraps up our examination of C# extension methods. Remember that this particular language feature can be very useful whenever you want to extend the functionality of a type, but do not want to subclass (or can not subclass if the type is sealed), for the purposes of polymorphism. As you will see later in the text, extension methods play a key role for LINQ APIs. In fact, you will see that under the LINQ APIs, one of the most common items being extended is a class or structure implementing (surprise!) the generic version of IEnumerable.  Source Code The InterfaceExtension project can be found under the Chapter 11 subdirectory. Understanding Anonymous Types As an OO programmer, you know the benefits of defining classes to represent the state and functionality of a given item you are attempting to model. To be sure, whenever you need to define a class that is intended to be reused across projects and provides numerous bits of functionality through a set of methods, events, properties, and custom constructors, creating a new C# class is common practice. However, there are other times when you would like to define a class simply to model a set of encapsulated (and somehow related) data points without any associated methods, events, or other specialized functionality. Furthermore, what if this type is to be used only by a handful of methods in your program? It would be rather a bother to define a full class definition as shown below; when you know full well this class will be used only in a handful of places. To accentuate this point, here is the rough outline of what you might need to do when you need to create a “simple” data type that follows typical value-based semantics: CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 424 class SomeClass { // Define a set of private member variables... // Make a property for each member variable... // Override ToString() to account for key member variables... // Override GetHashCode() and Equals() to work with value-based equality... } As you can see, it is not necessarily so simple. Not only do you need to author a fair amount of code, but you have another class to maintain in your system. For temporary data such as this, it would be useful to whip up a custom data type on the fly. For example, let’s say that you need to build a custom method that receives a set of incoming parameters. You would like to take these parameters and use them to create a new data type for use in this method scope. Further, you would like to quickly print out this data using the typical ToString() method and perhaps use other members of System.Object. You can do this very thing using anonymous type syntax. Defining an Anonymous Type When you define an anonymous type, you do so by making use of the var keyword (see Chapter 3) in conjunction with object initialization syntax (see Chapter 5). We must use the var keyword because the compiler will automatically generate a new class definition at compile time (and we never see the name of this class in our C# code). The initialization syntax is used to tell the compiler to create private backing fields and (read-only) properties for the newly created type. To illustrate, create a new Console Application named AnonymousTypes. Now, add the following method to your Program class, which composes a new type, on the fly, using the incoming parameter data: static void BuildAnonType( string make, string color, int currSp ) { // Build anon type using incoming args. var car = new { Make = make, Color = color, Speed = currSp }; // Note you can now use this type to get the property data! Console.WriteLine(“You have a {0} {1} going {2} MPH“, car.Color, car.Make, car.Speed); // Anon types have custom implementations of each virtual // method of System.Object. For example: Console.WriteLine(“ToString() == {0}“, car.ToString()); } You can call this method from Main(), as expected. However, do note that an anonymous type can also be created using hard-coded values, as seen here: static void Main(string[] args) { Console.WriteLine(“***** Fun with Anonymous Types *****\n“); // Make an anonymous type representing a car. var myCar = new { Color = “Bright Pink“, Make = “Saab“, CurrentSpeed = 55 }; CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 425 // Now show the color and make. Console.WriteLine(“My car is a {0} {1}.“, myCar.Color, myCar.Make); // Now call our helper method to build anonymous type via args. BuildAnonType(“BMW“, “Black“, 90); Console.ReadLine(); } So, at this point, simply understand that anonymous types allow you to quickly model the “shape” of data with very little overhead. This technique is little more than a way to whip up a new data type on the fly, which supports barebones encapsulation via properties and acts according to value-based semantics. To understand that last point, let’s see how the C# compiler builds out anonymous types at compile time, and specifically, how it overrides the members of System.Object. The Internal Representation of Anonymous Types All anonymous types are automatically derived from System.Object and, therefore, support each of the members provided by this base class. Given this, we could invoke ToString(), GetHashCode(), Equals(), or GetType() on the implicitly typed myCar object. Assume our Program class defines the following static helper function: static void ReflectOverAnonymousType(object obj) { Console.WriteLine(“obj is an instance of: {0}“, obj.GetType().Name); Console.WriteLine(“Base class of {0} is {1}“, obj.GetType().Name, obj.GetType().BaseType); Console.WriteLine(“obj.ToString() == {0}“, obj.ToString()); Console.WriteLine(“obj.GetHashCode() == {0}“, obj.GetHashCode()); Console.WriteLine(); } Now assume we invoke this method from Main(), passing in the myCar object as the parameter, like so: static void Main(string[] args) { Console.WriteLine(“***** Fun with Anonymous Types *****\n“); // Make an anonymous type representing a car. var myCar = new {Color = “Bright Pink“, Make = “Saab“, CurrentSpeed = 55}; // Reflect over what the compiler generated. ReflectOverAnonymousType(myCar); ... Console.ReadLine(); } CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 426 The output will look similar to the following: ***** Fun with Anonymous Types ***** obj is an instance of: <>f__AnonymousType0`3 Base class of <>f__AnonymousType0`3 is System.Object obj.ToString() = { Color = Bright Pink, Make = Saab, CurrentSpeed = 55 } obj.GetHashCode() = -439083487 First, notice that in this example, the myCar object is of type <>f__AnonymousType0`3 (your name may differ). Remember that the assigned type name is completely determined by the compiler and is not directly accessible in your C# code base. Perhaps most important, notice that each name/value pair defined using the object initialization syntax is mapped to an identically named read-only property and a corresponding private read-only backing field. The following C# code approximates the compiler-generated class used to represent the myCar object (which again can be verified using ildasm.exe): internal sealed class <>f__AnonymousType0<j__TPar, j__TPar, j__TPar> { // Read-only fields. private readonly j__TPar i__Field; private readonly j__TPar i__Field; private readonly j__TPar i__Field; // Default constructor. public <>f__AnonymousType0(j__TPar Color, j__TPar Make, j__TPar CurrentSpeed); // Overridden methods. public override bool Equals(object value); public override int GetHashCode(); public override string ToString(); // Read-only properties. public j__TPar Color { get; } public j__TPar CurrentSpeed { get; } public j__TPar Make { get; } } The Implementation of ToString() and GetHashCode() All anonymous types automatically derive from System.Object and are provided with an overridden version of Equals(), GetHashCode(), and ToString(). The ToString() implementation simply builds a string from each name/value pair. For example: CHAPTER 11  ADVANCED C# LANGUAGE FEATURES 427 public override string ToString() { StringBuilder builder = new StringBuilder(); builder.Append(“{ Color = “); builder.Append(this.i__Field); builder.Append(“, Make = “); builder.Append(this.i__Field); builder.Append(“, CurrentSpeed = “); builder.Append(this.i__Field); builder.Append(“ }“); return builder.ToString(); } The GetHashCode() implementation computes a hash value using each anonymous type’s member variables as input to the System.Collections.Generic.EqualityComparer type. Using this implementation of GetHashCod