Variables & Data Types
C# is a strongly typed language. Every variable has a type that determines what values it can hold.
Value Types
Value types store data directly on the stack:
int count = 42;
double price = 9.99;
bool isActive = true;
char grade = 'A';
decimal salary = 75_000.50m;
Reference Types
Reference types store a pointer to data on the heap:
string name = "Andi";
object obj = 42; // boxed int
int[] numbers = { 1, 2, 3 };
Type Inference with var
The compiler can infer the type from the right-hand side:
var message = "Hello"; // string
var total = 100; // int
var items = new List<string>(); // List<string>
var is not dynamic — the type is determined at compile time. Use it when the type is obvious from context.
Common Type Summary
| Type | Size | Range / Notes |
|---|---|---|
int |
4 bytes | -2.1B to 2.1B |
long |
8 bytes | Very large integers |
double |
8 bytes | 15-digit precision |
decimal |
16 bytes | 28-digit precision (finance) |
bool |
1 byte | true / false |
char |
2 bytes | Single Unicode character |
string |
varies | Immutable text |
Nullable Value Types
Value types can be made nullable with ?:
int? age = null;
if (age.HasValue)
Console.WriteLine(age.Value);