Chapter 7. Structs
A struct is a simple user-defined type, a lightweight alternative to a class. Structs are similar to classes in that they may contain constructors, properties, methods, fields, operators, nested types, and indexers (see Chapter 9).
There are also significant differences between classes and structs. For instance, structs don’t support inheritance or destructors. More important, although a class is a reference type, a struct is a value type. (See Chapter 3 for more information about classes and types.) Thus, structs are useful for representing objects that don’t require reference semantics.
The consensus view is that you ought to use structs only for types that are small, simple, and similar in their behavior and characteristics to built-in types.
C++ programmers take note: the meaning of C#’s struct construct is very different from C++’s. In C++ a struct is exactly like a class, except that the visibility (public versus private) is different by default. In C#, structs are value types, while classes are reference types, and C# structs have other limitations as described in this chapter.
Structs are somewhat more efficient in their use of memory in arrays (see Chapter 9). However, they can be less efficient when used in some collections. Collections that take objects expect references, and structs must be boxed. There is overhead in boxing and unboxing, and classes might be more efficient in some large collections.
In this chapter, you will learn how to define ...