Structures
The content of this chapter so far has been focused on classes. Types that are defined as classes are reference types. However, in .NET and C#, there is another category of types: value types. Value types have value semantics, meaning that the value of the object, and not a reference to the object, is copied on assignment.
Value types are defined using the struct
keyword instead of class
. In most aspects, structures are identical to classes and the characteristics presented in this chapter for classes apply to structures too. However, there are several key differences:
- Structures do not support inheritance. Although a structure can implement any number of interfaces, it cannot derive from another structure. For this reason, structure members cannot have the
protected
access modifier. Also, a structured method or property cannot beabstract
orvirtual
. - A structure cannot declare a default (parameterless) constructor.
- Structures can be instantiated without...