Variables and Value Types
Declaring variables and understanding the difference between value types and reference types in C#.
2 min read
Every variable in C# has a fixed type, declared up front:
int age = 30;
double price = 19.99;
char grade = 'A';
bool isActive = true;
string name = "Ada";int, double, char, and bool are value types — the variable holds the actual data directly. string (along with classes, arrays, and most other objects) is a reference type — the variable holds a reference pointing to where the data lives in memory. This distinction matters more than it looks like it should.
Why value vs. reference matters
Copy a value type, and you get an independent copy:
int a = 5;
int b = a;
b = 10;
Console.WriteLine(a); // 5 -- unaffected by changing bCopy a reference type, and both variables point at the same underlying object:
var list1 = new List<int> { 1, 2, 3 };
var list2 = list1;
list2.Add(4);
Console.WriteLine(list1.Count); // 4 -- list1 sees the change toolist1 and list2 aren't two lists — they're two names for the same list. Mutating through one is visible through the other. This is the single most common source of confusion for people new to C# (or any language with this split), so it's worth internalizing early.
Common built-in types
| Type | Category | Example |
|---|---|---|
| int | Value | 42 |
| double | Value | 3.14 |
| decimal | Value | 19.99m |
| bool | Value | true |
| char | Value | 'A' |
| string | Reference | "hello" |
decimal deserves a special mention: use it for money and anything requiring exact base-10 precision. double uses binary floating-point, which can't represent values like 0.1 exactly — fine for scientific calculations, risky for currency.
decimal price = 19.99m; // the 'm' suffix marks a decimal literalconst and readonly
For values that never change:
const double Pi = 3.14159; // fixed at compile time
readonly DateTime StartedAt = DateTime.Now; // fixed once, at constructionconst must be known at compile time and can't depend on runtime values. readonly is more flexible — it can be assigned once, either inline or in a constructor, based on something computed at runtime. Reach for const for true constants like math values, and readonly for values that are fixed per-instance but computed when the object is created.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.