CHAPTER 1 THE PHILOSOPHY OF .NET
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.
17