Answers
You can use both classes and structures to define custom data types to use as the building blocks of your program’s code.
However, structure instances are always passed by value , and class instances are always passed by reference . This means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that you need for a project, decide whether each data construct should be defined as a class or as a structure.
As a general guideline, consider creating a structure when one or more of these conditions apply:
- The structure’s primary purpose is to encapsulate a few relatively simple data values.
- It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.
- Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.
- The structure does not need to inherit properties or behavior from another existing type.
Examples of good candidates for structures include:
-
The size of a geometric shape, perhaps encapsulating a
width
property and aheight
property, both of typeDouble
. -
A way to refer to ranges within a series, perhaps encapsulating a
start
property and alength
property, both of typeInt
. -
A point in a 3D coordinate system, perhaps encapsulating
x
,y
andz
properties, each of typeDouble
.
In all other cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.