- Can support inheritance
- Are reference (pointer) types
- The reference can be null
- Have memory overhead per new instance
- Cannot support inheritance
- Are value types
- Cannot have a null reference (unless Nullable is used)
- Do not have a memory overhead per new instance – unless ‘boxed’
- Cannot have explicit parameterless constructor
- Cannot have destructor
Both Classes and Structs:
- Are compound data types typically used to contain a few variables that have some logical relationship
- Can contain methods and events
- Can support interfaces
CONSIDER a struct instead of a class:
- If instances of the type are small and commonly short-lived or are commonly embedded in other objects.
AVOID a struct unless the type has all of the following characteristics:
- It logically represents a single value, similar to primitive types (int, double, etc.).
- It has an instance size under 16 bytes.
- It is immutable. (cannot be changed)
- It will not have to be boxed frequently.