C# Canonical Forms
|Visual C# Tutorials|
C# Canonical Forms
© 2006 Weldon W. Nash, III
|This tutorial—CSharp Canonical Forms—is from Accelerated C# 2005, by Trey Nash. Copyright © 2006 Weldon W. Nash, III. All rights reserved. This article is reproduced by permission. This tutorial has been edited especially for C# Online.NET. Read the book review!|
In Search of C# Canonical Forms
Many object-oriented languages—C# included—don’t offer anything to force developers to create well-designed software. There is no better example of this than when using C++ to implement an OO design. C# is a little more structured than C++; for example, you cannot create free static functions that exist outside the context of a defined type. Still, C# doesn’t force you to create software that adheres to well-known practices of good software design.
The C++ community quickly identified some canonical forms useful for designing types to meet a specific purpose. Really and truly, these canonical forms are merely checklists, or recipes, you can use while designing new classes. Before a pilot can clear an airplane to back out of the gate, he must go through a strict checklist. The goal of this chapter is to identify such checklists for creating robust types in the C# world.
When you explore these checklists, you need to consider what sorts of behaviors are required of objects of the new type you’re creating. For example, is your new type going to be cloneable? In other words, can it be copied? Does your new type support ordering if instances of it are placed in a collection? What does it mean to compare two references of this object’s type for equality? In other words, do you want to know if the two references refer to the same instance? Or do you want to know if two instances referred to have exactly the same state? These are the types of questions you should ask yourself when you create a new type.
Note This chapter is rather long, but it’s important to keep so much useful and related information together. Overall, the chapter is sectioned into two partitions. The first partition covers reference types, while the latter covers value types. I cover the longer partition on reference types first, since some material applies to both reference types and value types. Finally, the chapter concludes with a checklist to go through when designing new types.