[ Team LiB ] |
Chapter 3. Classes and StructuresStructures, like any other value type, implicitly inherit from System.ValueType. At first glance, a structure is similar to a class, but they are actually very different. Knowing when to use a structure over a class will help tremendously when designing an application. Using a structure incorrectly can result in inefficient and hard-to-modify code. Both structures and simple types inherit from ValueType. Structures have two performance advantages over reference types. First, if a structure is allocated on the stack (i.e., it is not contained within a reference type), access to the structure and its data is somewhat faster than access to a reference type on the heap. Reference type objects must follow their reference, or pointer, onto the heap in order to get at their data. However, this performance advantage pales in comparison to the second performance advantage of structures: namely, that cleaning up the memory allocated to a structure on the stack requires a simple change of the address to which the stack pointer points, which is done at the return of a method call. This call is extremely fast compared to allowing the garbage collector to automatically clean up reference types for you in the managed heap. Structure performance falls short in comparison to that of classes when they are passed by value to other methods. Because they reside on the stack, a structure and its data have to be copied to a new local variable (the method's parameter that is used to receive the structure) when it is passed by value to a method. This copying takes more time than passing a single reference to a class's object by value to a method—unless the structure is the same size as or smaller than the machine's pointer size; thus, a structure with a size of 32 bits is just as cheap to pass as a reference (which happens to be the size of a pointer) on a 32-bit machine. Keep this in mind when choosing between a class and a structure. While creating, accessing, and destroying a class's object may take longer, it also might not balance the performance hit when a structure is passed by value a large number of times to one or more methods. Keeping the size of the structure small minimizes the performance hit of passing it around by value. Structures can also cause degradation in performance when they are passed to methods that require a reference type, such as any of the collection types in the FCL. Passing a structure (or any simple type, for that matter) into a method requiring a reference type causes the structure to be boxed. Boxing is wrapping a value type in an object. When the method returns, the value type will be unboxed, which means that the value type will be extracted from its object wrapper. Both of these operations are time-consuming and may degrade performance. As concerns the object-oriented capabilities of classes and structures, classes have far more flexibility. A structure cannot contain a user-defined default constructor, since the C# compiler automatically provides a default constructor that initializes all the fields in the structure to their default values. This is also why no field initializers can be added to a structure. If you need to override the default field values, a structure might not be the way to go. However, a parameterized constructor can be created that initializes the structure's fields to any value that is necessary. Structures, like classes, can inherit from interfaces; but unlike classes, structures cannot inherit from a class or a structure. This limitation precludes creating structure hierarchies, as you can with classes. Polymorphism as implemented through an abstract base class is also prohibited when using a structure, since a structure cannot inherit from another class. Use a class if:
Use a structure if:
|
[ Team LiB ] |