Value Types vs Reference Types
See how each is stored and copied.
Value Types vs Reference Types is a free C# Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
Two Families of Types
C# types split into value types (int, double, bool, structs) and reference types (classes, strings, arrays). They differ in how they are stored and copied.
using System;
class Program
{
static void Main()
{
int valueType = 5; // value type
int[] referenceType = { 1, 2, 3 }; // reference type
Console.WriteLine(valueType + ", length " + referenceType.Length);
}
}Value Types Hold the Data
A value-type variable directly contains its data. Typically these live on the stack or inline within their containing object.
using System;
class Program
{
static void Main()
{
int a = 10;
double b = 3.14;
bool c = true;
Console.WriteLine(a + ", " + b + ", " + c);
}
}Reference Types Hold an Address
A reference-type variable holds a reference (address) to data stored on the heap. The variable and the object are separate.
using System;
class Box { public int Value; }
class Program
{
static void Main()
{
Box b = new Box(); // b refers to a heap object
b.Value = 42;
Console.WriteLine(b.Value);
}
}Copy Semantics: Value Types
Assigning a value type copies the data. Changing the copy never affects the original.
using System;
class Program
{
static void Main()
{
int a = 1;
int b = a; // copy
b = 99;
Console.WriteLine("a=" + a + ", b=" + b);
}
}Copy Semantics: Reference Types
Assigning a reference type copies the reference, not the object. Both variables then point to the same object.
using System;
class Box { public int Value; }
class Program
{
static void Main()
{
Box a = new Box { Value = 1 };
Box b = a; // same object
b.Value = 99;
Console.WriteLine("a.Value=" + a.Value + ", b.Value=" + b.Value);
}
}Structs Are Value Types
A struct you define is a value type, so it copies on assignment just like int.
using System;
struct Point { public int X, Y; }
class Program
{
static void Main()
{
Point p1 = new Point { X = 1, Y = 2 };
Point p2 = p1; // full copy
p2.X = 99;
Console.WriteLine("p1.X=" + p1.X + ", p2.X=" + p2.X);
}
}Passing to Methods
By default, value types are passed by copy and reference types pass a copy of the reference. So a method can change a class object the caller sees, but not a plain int.
using System;
class Counter { public int N; }
class Program
{
static void BumpInt(int x) { x++; }
static void BumpObj(Counter c) { c.N++; }
static void Main()
{
int i = 0;
BumpInt(i);
var c = new Counter();
BumpObj(c);
Console.WriteLine("i=" + i + ", c.N=" + c.N);
}
}null and Reference Types
Only reference types can be null (the absence of an object). Value types always hold a value unless declared nullable with ?.
using System;
class Program
{
static void Main()
{
string text = null; // reference type can be null
int? maybe = null; // nullable value type
Console.WriteLine((text == null) + ", " + (maybe == null));
}
}Equality Differences
For value types, == compares the data. For reference types, == usually compares identity (same object), unless overridden.
using System;
class Program
{
static void Main()
{
int a = 5, b = 5;
Console.WriteLine("ints: " + (a == b)); // True - same value
object o1 = new object();
object o2 = new object();
Console.WriteLine("objects: " + (o1 == o2)); // False - different identity
}
}Default Values
Value types have a zero-like default (0, false); reference types default to null.
using System;
class Program
{
static void Main()
{
int defaultInt = default;
bool defaultBool = default;
string defaultString = default;
Console.WriteLine(defaultInt + ", " + defaultBool + ", null? " + (defaultString == null));
}
}Putting It Together
Knowing whether a type is value or reference predicts copy behavior, nullability, and how methods can mutate it. This shapes correct, bug-free code.
using System;
struct Money { public decimal Amount; }
class Wallet { public decimal Amount; }
class Program
{
static void Main()
{
Money m1 = new Money { Amount = 10 };
Money m2 = m1; m2.Amount = 99;
Wallet w1 = new Wallet { Amount = 10 };
Wallet w2 = w1; w2.Amount = 99;
Console.WriteLine("Money copy: " + m1.Amount + " (independent)");
Console.WriteLine("Wallet ref: " + w1.Amount + " (shared)");
}
}Quick Check
Test your understanding of value vs reference types.
Recap
Value types (int, double, bool, structs) store data directly and copy on assignment; reference types (classes, strings, arrays) store a reference to a heap object and copy the reference. Only reference types are nullable by default, and assignment behavior plus equality differ between the two families.
using System;
struct V { public int N; }
class R { public int N; }
class Program
{
static void Main()
{
V v1 = new V { N = 1 }; V v2 = v1; v2.N = 9;
R r1 = new R { N = 1 }; R r2 = r1; r2.N = 9;
Console.WriteLine(v1.N + " vs " + r1.N);
}
}Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Value Types vs Reference Types” lesson free?
Yes — the full text of “Value Types vs Reference Types” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Value Types vs Reference Types”?
See how each is stored and copied. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Value Types vs Reference Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Value Types vs Reference Types
- Boxing and Unboxing
- Implicit and Explicit Conversions
- The Convert Class and Parsing