C# Class Design Handbook: Coding Effective Classes ISBN:1590592573 by Richard Conway et al Apress © 2003 This text shows how to create robust, flexible, reusable classes with C# and learn how to understand different types you can create in C#, as well as how classes relate to the NET type framework Table of Contents C# Class Design Handbook—Coding Effective Classes Introduction Chapter 1 - Defining Types Chapter 2 - Type Members Chapter 3 - Methods Chapter 4 - Properties and Operators Chapter 5 - Constructors and the Object Lifecycle Chapter 6 - Events and Delegates Chapter 7 - Inheritance and Polymorphism Chapter 8 - Code Organization and Metadata Appendix A - Support, Errata, and Code Download Index List of Figures Back Cover The mission of the C# Class Design Handbook is to provide you with a critical understanding of designing classes, making you better equipped to take full advantage of C#’s power to create robust, flexible, reusable classes This comprehensive guide lifts the lid on syntax and examines what’s really going on behind the scenes Specific topics include the role of types in NET, the different kinds of types C# can create, the fundamental role of methods as containers of program logic, and the workings behind NET’s delegate-based event system It will also show you how to control and exploit inheritance in your types and how to create logical and physical code organization through namespaces and assemblies Designing classes that don’t have to be revisited and revised over and over again is an art This handbook aims to put that art in your hands, giving you a deeper understanding of the decisions you must make to design classes, and design them effectively About the Authors Richard Conway started programming BASIC with the ZX81 at an early age, later graduating to using BASIC and 6502 assembly language, COMAL, and Pascal for the BBC B and Archimedes RISC machines He is an independent software consultant who lives and works in London He has been using Microsoft technologies for many years and has architected and built enterprise systems for IBM, Merrill Lynch, and Reuters He has focused his development on Windows DNA including various tools and languages, such as COM+, VB, XML, C++, J++, BizTalk and, more recently, data warehousing He has been actively involved in EAP trials with Microsoft for NET My Services and the NET Compact Framework His special area of interest is network security and cryptography Richard is a contributor to both C# Today and ASP Today, and he is currently involved in a product development and consultancy alliance specializing in data warehousing and security products Teun Duynstee lives in the Netherlands and works with Macaw as a lead software developer Ben Hyrman works as a Program Architect for Best Buy in Minneapolis, Minnesota Roger Rowland is a freelance IT Consultant based in the UK He has 25 years of software development experience on a variety of platforms, and is a regular contributor to the Wrox C# Today web site He currently specializes in Microsoft technologies including VC++, VB, C#, SQL, and ASP Roger is a member of the Institution of Analysts and Programmers, a professional member of the British Computer Society, and a member of the IEEE Computer Society He holds a Masters Degree in computing and is currently undertaking a part-time PhD at the University of East Anglia researching into medical imaging and computer assisted surgery Research techniques include 3D graphics and volume rendering using OpenGL, and he has published a number of academic papers James Speer has been a software developer since 1987, beginning his career in programming in BCPL and C++ He currently specializes in distributed NET component development, particularly – C#, NET Remoting, Serviced Components and MSMQ C# Class Design Handbook—Coding Effective Classes Richard Conway Teun Duynstee Ben Hyrman Roger Rowland James Speer of Charteris plc Apress™ Copyright © 2003 Apress All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher (pbk): 1-59059-257-3 Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co KG, Tiergartenstr 17, 69112 Heidelberg, Germany In the United States: phone 1-800-SPRINGER, email orders@springerny.com, or visit http://www.springer-ny.com Outside the United States: fax +49 6221 345229, email orders@springer.de, or visit http://www.springer.de For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710 Phone 510-549-5930, fax 510-549-5939, email info@apress.com, or visit http://www.apress.com The information in this book is distributed on an "as is" basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work The source code for this book is available to readers at http://www.apress.com in the Downloads section Some material first published in a different form in Visual Basic NET Class Design Handbook (1-86100-708-6), May 2002 Credits Editorial Board Dan Appleman Craig Berry Gary Cornell Tony Davis Steven Rycroft Julian Skinner Martin Streicher Jim Sumser Karen Watterson Gavin Wright John Zukowski Additional Material Damon Allison Andy Olsen Steven Sartain Commissioning Editor James Hart Technical Editors Fatema Beheranwala Nilesh Parmar Additional Editing Andrew Polshaw Technical Reviewers Andrew Krowczyk Roger Rowland Managing Editor Emma Batch Index Michael Brinkman Project Manager Beckie Stones Production Coordinator Sarah Hall Proof Reader Chris Smith Cover Natalie O'Donnell About the Authors Richard Conway Richard Conway started programming BASIC with the ZX81 at an early age later graduating to using BASIC and 6502 assembly language, COMAL, and Pascal for the BBC B and Archimedes RISC machines He is an independent software consultant who lives and works in London He has been using Microsoft technologies for many years and has architected and built enterprise systems for the likes of IBM, Merrill Lynch, and Reuters He has focused his development on Windows DNA including various tools and languages such as COM+, VB, XML, C++, J++, BizTalk, and more recently, Data Warehousing He has been actively involved in EAP trials with Microsoft for NET My Services and the NET Compact Framework He has spent the last two and a half years since the release of the technical preview (of VS.NET) programming proof-ofconcept and enterprise system projects in C# His special area of interest is Network Security and Cryptography Richard is a contributor to both C# Today and ASP Today He is currently involved in a product development and consultancy alliance – http://www.vertexion.co.uk – specializing in data warehousing and security products He can be contacted at richard.conway@vertexon.co.uk Teun Duynstee Teun Duynstee lives in the Netherlands He works with Macaw as a lead software developer and loves programming, his girlfriend Marjolein, and Arnie the cat Ben Hyrman Ben works as a Program Architect for Best Buy, in tropical Minneapolis, Minnesota Ben enjoys the balmy Minnesota weather with his loving wife, Dawn, and an overactive mutt of a dog, Bandit When they're not busy with work or off on road trips, Ben and Dawn enjoy painting their house and arguing over database design patterns I would like to thank Damon Allison for being my sounding board for all of my crazy ideas I'd also like to thank Richard Scott, because he's British and he asked me to Lastly, I'd like to thank Wrox for this excellent opportunity Roger Rowland Roger Rowland is a freelance IT Consultant based in the UK He has 25 years of software development experience on a variety of platforms, and is a regular contributor to the Wrox C# Today web site He currently specializes in Microsoft technologies including VC++, VB, C#, SQL, and ASP Roger is a member of the Institution of Analysts and Programmers, a professional member of the British Computer Society, and a member of the IEEE Computer Society He holds a Masters Degree in computing and is currently undertaking a part-time PhD at the University of East Anglia researching into medical imaging and computer assisted surgery Research techniques include 3D graphics and volume rendering using OpenGL, and he has published a number of academic papers Married, with two children and always incredibly busy, Roger may nevertheless be contacted at roger.rowland@rmrsystems.co.uk James Speer of Charteris plc James has been a software developer since 1987, beginning his career programming in BCPL and C++ He currently specializes in NET component development, particularly - C#, NET Remoting, Serviced Components and MSMQ James is currently employed by Charteris plc (http://www.charteris.com) as a Senior Developer and can be reached at james.speer@charteris.com Thanks to Mom and Dad for the Acorn Electron and June for lending me your Vic 20 C# Class Design Handbook The book takes a top-down look at what exactly makes up a class in NET We begin by describing what a type is, and how classes relate to the NET type framework Then we examine what makes up types: type members We devote the majority of the book to looking at the different mechanisms C# provides for defining type members (methods, constructors, properties, operators, and events), and finally examine how types go together to make up assemblies Introduction C# is a language that follows in a grand tradition of programming language design; it draws its influences from C++ and Java, and even Delphi and Visual Basic – a rich inheritance, which provides it with much that is familiar to many developers, but also much that is alien or unexpected Programmers unfamiliar with object-oriented, C-family, ‘curly-bracket’ languages, perhaps coming to C# from a background with Visual Basic 6 or ASP VBScript, often find the scope of the object-oriented features in C# daunting Those coming from other object-oriented platforms – C++ or Java for example – find some of C#'s additional facilities surprising or confusing, while other, seemingly familiar syntaxes can behave in curiously different ways This book takes the lid off C#'s object-oriented model, and examines how we use C# as a language for creating classes (and, indeed, other kinds of types) Since everything we code in C# is a type, all our logic belongs to methods of types, and the state of our program at any moment is tied up in the values stored in the fields of instances of types in memory A good understanding of how to create those types is therefore fundamental to good C# programming We'll explore what options C# gives us in declaring types and type members in our code, and the impact our decisions will have on code that uses our types We'll see how we can code differently when our types are for public use, and when types are for use within our own code We'll look at what we can do to ensure our types are only used in ways we design for, and how we can expose functionality from our types in a consistent, logical, predictable, and user-friendly manner, for other code to exploit documentation for assembly, 341 strongly typed language, 8 structures compared to class types, 23, 34 implementing interfaces, 26 primitive types and, 16 subscribing to events, 239 symetrical operator overloads operators_symmetrical example, 161 sysobject_equals_bankaccount example Equals method, Object class, 78 sysobject_gethashcode_bankaccount example GetHashCode method, Object class, 80 sysobject_tostring_bankaccount example ToString method, Object class, 76 sysobject_tostring_bankaccount2 example ToString method, Object class, 77 System namespace, 298 ApplicationException class, 111 ArgumentException class, 112 AsyncCallback delegate, 238 Delegate class, 224, 230, 244 Double class, 19 Enum class, 30 Environment class, 45 EventArgs class, 247 EventHandler class, 242, 243, 248, 249 FileNotFoundException class, 114 FlagsAttribute class, 30 IAsyncResult interface, 238 IComparable interface, 27 IDisposable interface, 184 IFormattable interface, 29 Int32 structure, 18 InvalidCastException class, 50 MulticastDelegate class, 222, 229 NullReferenceException class, 50 Object class, 199 structures, 16 SystemException class, 111 ValueType class, 25 System.Collections namespace, 146 ArrayList class, 48, 147 ICollection interface, 268 interface inheritance, 268 System.Console class WriteLine method, 22, 27 System.Data namespace description, 298 nested namespaces, 312 System.Data.Common namespace, 312 System.Data.OleDb namespace, 312 System.Data.SqlClient namespace, 312 System.Data namespaces nested namespaces System.Data.SqlTypes namespace, 312 System.Data.Common namespace nested System.Data namespaces, 312 System.Data.OleDb namespace nested System.Data namespaces, 312 System.Data.SqlClient namespace nested System.Data namespaces, 312 System.Data.SqlTypes namespace nested System.Data namespaces, 312 System.Diagnostics namespace classes for debugging methods, 87 StackFrame class, 86 StackTrace class, 86 System.Drawing namespace Graphics class, 218 System.IO namespace FileStream class, 201 System.Reflection namespace, 198 MethodBase class, 112 System.Runtime.Serialization namespace SerializationException class, 201 System.Runtime.Serialization.Formatters.So ap namespace SoapFormatter class, 201 System.Text namespace StringBuilder class, 105 System.Threading namespace, 234 System.Web namespace description, 298 System.Windows namespace description, 298 System.Windows.Forms namespace Control class, 220 System.Xml namespace description, 298 XmlDocument class, 232, 233 System.Xml.Serialization namespace XmlSerializer class, 203 SystemException class, System namespace, 111 read-only properties, 111 Index T TargetSite property, SystemException class, 112 Teacher class example copy constructor, 197 ICloneable interface, System namespace, 199 instance constructors, 191 private constructors, 189 static constructors, 191 this keyword instance methods, 87 use in chaining constructors, 187 throw statement throwing exceptions, 113 ToString method, Object class, 75, 76 overriding methods, 25 sysobject_tostring_bankaccount2, 77 try…catch…finally statement exception handling, 112 type definition metadata, 320 type members, 55 accessibility levels, 56 constants, 56 constructors, 73 destructors, 74 events and delegates, 72 fields, 62 accessibility levels, 62 methods, 67 properties, 64 static read-only variables, 60 static type members, 69 support for, 56 type safety, 7 delegates and, 215 function pointers, 215, 216 typeof operator primitive types and, 18 types CLS (Common Language Specification), 9 CTS (Common Type System), 9 defining, 7, 35 description, 8 interfaces, 51 memory usage, NET, 14 reference types, 12 arrays, 33, 42 class types, 33 delegates, 33, 41 strings, 33, 45 strongly typed language, 8 value types, 11 enumerations, 16, 28 primitive types, 15, 16 user-defined value types, 15, 22 using as reference types, 48 Index U UML (Universal Modeling Language), 262 designing namespaces, 301 packages, modeling, 301 UML diagrams abstact classes and, 266 inheritance, 263 showing class relationships, 263 unary operators parameters, 153 table and description, 152 unboxing advantages and disadvantages, 49 value types and, 48 Unified modeling Language see UML user-defined value types, 15 defining, 23 example of using, 24 inheritance, 25 introduction, 22 Index V value equality, 162, 164 Equals method, Object class, 78 value keyword setting when properties are set, 65 value types, 9 boxing and unboxing, 48 cleaning up, 22 compared to reference types, 166 copy-by-value semantics, 12 DateTime structure example, 11 description, 11 enumerations, 28 inheritance, 25 managed heap, 14 passing, 95 compared to passing reference types, 95 passing by reference, 98 passing by value, 96 primitive types, 15, 16 reference types and, 11 stack and, 11 user-defined value types, 15, 22 user-enumerations, 16 using as reference types, 48 boxing and unboxing, 48 value types as objects, 48 ValueType class, 25 ValueType class, System namespace inheritance example, 270 invoking default constructor, 178 overridable public methods, 25 value types inherit from, 25 variables see fields virtual method dispatching deriving classes, 279 virtual methods abstract_teacher example, 274 Visual Basic NET using with C# in multi-file assembly, 322 Visual Studio NET creating shared assembly application, 337 installing shared assembly into GAC, 339 setting namespace default namespace, 299 setting namespaces, 298 setting namespaces in Visual Studio NET Namespace statements, writing, 299 Index W WriteLine method, System.Console class, 22, 27 WriteOnly properties scalar properties, 131 compiling into MSIL, 133 defining, example, 131 Index X-Z XCopy deployment, 328 applications, deploying as single assemblies, 328 XML configuration files application configuration file, 331 using in NET Framework, 331 XML schema inferring, 203 XmlDocument class, System.Xml namespace, 232 Load method, 233 XmlSerializer class, System.Xml.Serialization namespace, 203 xsd.exe tool inferring XML schemas, 203 List of Figures Chapter 1: Defining Types Figure 1 Figure 2 Figure 3 Chapter 7: Inheritance and Polymorphism Figure 1 Figure 2 Figure 3 Figure 4 Figure 5 Figure 6 Figure 7 Figure 8 Figure 9 Figure 10 Figure 11 Chapter 8: Code Organization and Metadata Figure 1 Figure 2 ... 1987, beginning his career in programming in BCPL and C+ + He currently specializes in distributed NET component development, particularly – C# , NET Remoting, Serviced Components and MSMQ C# Class Design Handbook Coding Effective Classes... methods are coded, and how we can use them to control what code can create instances of a type We'll also examine object cloning, conversion operators, and some common coding techniques for controlling the creation of instances of our classes Chapter 6 – Events and Delegates... All of the other types combine the above two things C+ + Note: Java Note: Interfaces are akin to pure abstract classes Unlike in C+ +, a C# class may only inherit from a single base class However, a C# class may additionally inherit from multiple