Learn C# in Y Minutes
C# 14 · .NET 10

07. Value vs Reference

Every type in C# is either a value type or a reference type. The difference decides what happens when you copy a variable or pass it to a method. It is the source of many beginner bugs, so it is worth getting right early.

Value types: copies

Primitives (int, double, bool...), structs, and enums are value types. The variable holds the value itself. Assigning it to another variable copies the value, and from then on the two live separate lives.

app/07-value-vs-reference/ValueCopy.cs
int a = 10;
int b = a;
b++;

Console.WriteLine($"a = {a}");
Console.WriteLine($"b = {b}");
a = 10
b = 11

Reference types: shared objects

Classes, arrays, and strings are reference types. The object lives elsewhere in memory, and the variable only holds a reference to it, like an address. Assigning it copies the address, not the object. Both variables now point at the same object, so a change through one is visible through the other.

app/07-value-vs-reference/ReferenceCopy.cs
int[] first = [1, 2, 3];
int[] second = first;
second[0] = 99;
Console.WriteLine($"first[0] = {first[0]}");

var p1 = new Person { Name = "Ada", Age = 36 };
var p2 = p1;
p2.Age = 37;
Console.WriteLine($"p1.Age = {p1.Age}");

class Person
{
    public string Name = "";
    public int Age;
}
first[0] = 99
p1.Age = 37
Left: a sheet with 42 is photocopied and only the copy is scribbled on. Right: two people hold cards with the same address pointing to one house whose door is repainted.
Copying a value makes a photocopy. Copying a reference shares the address of one house.

Stack and heap

Under the hood, local variables live on the stack, a fast, short-lived memory area. Objects created with new live on the heap, a larger area cleaned up automatically by the garbage collector once nothing references them anymore.

stackheapa10b11p1p2PersonName: "Ada", Age: 37
a and b are two separate values. p1 and p2 are two references to one object.

Passing to methods

The same rule applies when you pass arguments. A method receives a copy of a value type, so it cannot change your variable. It receives a copy of the reference for a reference type, so it can change the object you gave it.

app/07-value-vs-reference/PassToMethod.cs
int number = 1;
Increment(number);
Console.WriteLine($"number = {number}");

var person = new Person { Age = 20 };
MakeOlder(person);
Console.WriteLine($"person.Age = {person.Age}");

static void Increment(int value) => value += 10;

static void MakeOlder(Person p) => p.Age += 10;

class Person
{
    public int Age;
}
number = 1
person.Age = 30

Struct vs class, side by side

This is the real difference between the struct and the class from lesson 06. Same fields, same code, different result:

app/07-value-vs-reference/StructVsClass.cs
var pointA = new PointStruct { X = 1 };
var pointB = pointA;
pointB.X = 50;

var boxA = new PointClass { X = 1 };
var boxB = boxA;
boxB.X = 50;

Console.WriteLine($"struct: pointA.X = {pointA.X}");
Console.WriteLine($"class:  boxA.X   = {boxA.X}");

struct PointStruct
{
    public int X;
}

class PointClass
{
    public int X;
}
struct: pointA.X = 1
class:  boxA.X   = 50

string is a reference type that behaves like a value: it can never be modified. Methods like ToUpper() return a new string instead, so sharing one is always safe.

In a Web API, services and database contexts are objects shared by reference across your code. When a method receives an entity and changes it, it changes the one object that Entity Framework Core is tracking, which is exactly how updates get saved.

ProductService.cs
public void ApplyDiscount(Product product, decimal percent)
{
    product.Price -= product.Price * percent / 100;
}

Quick quiz