C# Reference ยท Types and Literals
Value types and reference types
Reference for the C# type system: how value types and reference types differ in copying, equality, null, boxing, memory and parameters, with runnable examples.
Every C# type is either a value type or a reference type. The difference decides what happens when you assign a variable, pass it to a method, compare it or set it to null.
A variable of a value type holds the data itself. Copying it copies the data. A variable of a reference type holds a reference to an object elsewhere. Copying it copies only the reference, so both variables see the same object.
Value types: int, double, bool, char, decimal, enum types, struct types (including DateTime, Guid, tuples and Nullable<T>). Reference types: class (including string), interface, delegate, arrays, record classes and object.
int a = 1; int b = a; b++; // a is still 1 var p = new Person(); var q = p; // p and q refer to one object
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Concepts
Where the data lives
A local variable of a value type is stored where it is declared: on the stack for locals, inside the object for fields, inside the array for elements. An object of a reference type lives on the managed heap, and variables hold a reference to it. The runtime frees heap objects with the garbage collector; value-type locals disappear when the method ends.
This is a simplification that is usually right. Value types inside classes live on the heap with their object, and the JIT may keep locals in registers.
Equality
== on built-in value types compares values. On a class it compares references unless the class overloads == or is a record. Structs have a default Equals that compares fields, but no default ==; record struct generates both.
Choosing
- Use a
structfor small, immutable values that behave like numbers (points, colors, money, ids). Keep them under about 16 bytes when you can. - Use a
classfor objects with identity, mutable state, inheritance or large data. - Use a
recordwhen you want value-based equality on a reference type, andrecord structfor a value type.
Usage notes
- Passing a reference type by value copies the reference. The method can change the object's contents but cannot make the caller's variable point to a different object unless you use
ref. - Boxing allocates. Avoid it in hot paths: use generics (
List<int>) instead ofArrayListorobjectparameters. - Mutable structs are risky: calling a method on a copy changes the copy. Prefer
readonly struct. - Value types cannot inherit from other types, but they can implement interfaces.
- All types derive from
object; value types derive fromSystem.ValueType. - With nullable reference types enabled,
string?documents that null is allowed. For value types useint?. Span<T>and otherref structtypes are value types that must stay on the stack.