Previous Section  < Day Day Up >  Next Section

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.

Table 3-5. Comparison of Structure and Class
 

Structure

Class

Default access level of the type

Internal

Internal

Default access level for data members

Public

Private

Default access level for properties and methods

Private

Private

Value or reference type

Value

Reference

Can be a base for new types

No

Yes

Implement interfaces

Yes

Yes

Raise and handle events

Yes

Yes

Scope of members

Structure

Class

Instance initialization

Constructor梬ith or without parameters. A struct cannot contain a custom parameterless constructor.

Constructor梬ith or without parameters.

Can be nested

Yes

Yes

Has a destructor

No

Yes (Finalizer)


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.

  • The type needs to serve as a base for deriving other types.

  • The type needs to inherit from another class. Note that although a structure cannot explicitly specify a base class, it does implicitly inherit from the System.ValueType and may override the inherited methods.

  • The type is frequently passed as a method parameter. Performance degrades as copies of the structure are created with each call. An exception to this is when the structure exists inside an array. Because an array is a reference type, only a pointer to the array is passed.

  • The type is used as the return type of methods. In the case where the return type is a structure, the system must copy the structure from the called function to the calling program.

    Previous Section  < Day Day Up >  Next Section