Skip to content
Tutorials Center
My learning

Topics

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.

C#
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

C# Copy semantics

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.

C# null, boxing and arrays

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 struct for small, immutable values that behave like numbers (points, colors, money, ids). Keep them under about 16 bytes when you can.
  • Use a class for objects with identity, mutable state, inheritance or large data.
  • Use a record when you want value-based equality on a reference type, and record struct for 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 of ArrayList or object parameters.
  • 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 from System.ValueType.
  • With nullable reference types enabled, string? documents that null is allowed. For value types use int?.
  • Span<T> and other ref struct types are value types that must stay on the stack.