
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
86
Chapter 3
CHAPTER 3
Classes and Structures
3.0 Introduction
Structures, like any other value type, implicitly inherit from System.ValueType. At
first glance, a structure is similar to a class but is actually very different. Knowing
when to use a structure over a class will help tremendously when designing an appli-
cation. Using a structure incorrectly can result in inefficient and hard-to-modify
code.
Structures have two performance advantages over reference types. First, if a struc-
ture 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 onto the heap in order to get
at their data. However, this performance advantage pales in comparison to the sec-
ond performance advantage of structures: namely, that cleaning up the memory allo-
cated 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; however, the cost of the garbage collec-
tor is deferred so that it’s not immediately ...