|< Day Day Up >|
3.12. Structure Versus Class
Many developers instinctively select a class to represent data and the operations performed on it. However, there are cases where a struct is a better choice, as evidenced by its use in the Framework Class Library to represent simple data types. This section compares the two and offers general guidelines to consider when choosing between the two.
It is clear from the table that structures possess many of the features and capabilities of classes. Consequently, a developer may have difficulty deciding which is the better choice. The answer lies in understanding the few梑ut significant梔ifferences between the two.
Structures Are Value Types and Classes Are Reference Types
As mentioned, classes are allocated space from the managed heap when the object or class instance is created. The address of the object (on the heap) is returned to the variable representing the object. In contrast, a variable set to a struct type contains the structure's actual data梟ot a pointer. The ramifications of this are most pronounced when passing arguments to functions. Reference types simply require that a pointer be passed, whereas structures require that a copy of all fields be made and passed to the function.
The structure does have some advantages with regard to memory allocation and Garbage Collection. Structure instances are allocated in a thread's stack rather than the managed heap, thus avoiding the associated overhead of managing pointers. Memory management is also simpler. When a copy of a structure is no longer reachable, its memory is collected and made available. In contrast, classes often require special code to handle unmanaged resources or invoke the system's garbage collection routine. Garbage Collection is covered in the next chapter.
Unlike a Class, a Structure Cannot Be Inherited
This is a departure from C++ conventions that permit a class to use an existing structure as a base class and permit a structure to use a class as a base. Is the .NET lack of support for this a step backward? Not really. It better delineates the role of the structure versus the class梞aking the structure less of a pseudo-class and more of a data structure. In addition, it provides for a more efficient implementation of the structure. The restriction enables the compiler to minimize the amount of administrative code that would be required to support inheritance. For example, because the compiler knows that a structure's methods cannot be overridden by subclasses, it can optimize the method invocation by expanding the method code inline rather than executing a method call.
General Rules for Choosing Between a Structure and a Class
The easiest way to make the choice is to compare the features of the type that you are designing with the following checklist. If any of these are true, you should use a class.
|< Day Day Up >|