C# Value types and Reference Type.

Value Types

Value type is either struct type or enumeration type. All value types implicitly inherit from object class. Value types are implicitly sealed thus it is not possible for any type to derive from value type:

  • A variable of a value type always contains a value of that type. Unlike reference types, it is not possible for a value of a value type to be null, or to reference an object of a more derived type.

  • Assignment to a variable of a value type creates a copy of value being assigned. This differs from assignment to a variable of a reference type, which copies the reference but not the object identified by the reference.

  • All value types implicitly inherit from System.ValueType class, which, in turn, inherits from System.Objectclass.

  • Note that System.ValueType is not itself value-type. Rather, it is class-type from which all value-types are derived.

Reference Type

A reference type is a class type, an interface type, an array type, or a delegate type.

  • A reference type is reference to instance of type, the latter known as an object.

  • Special value null is compatible with all reference types and indicates absence of an instance.

  • Reference types allocated on managed heap its reference address is maintained in stack.

  • The special value null indicate that stack memory do not have any reference to heap memory.
Next Recommended Reading Data Types & Type Conversions in C#