Camp 1 · Step 3 of 12
Variables and types
Declaring values with explicit types or var, and the everyday types C# programs run on.
C# is strongly typed — every variable has a type the compiler enforces — but it stays friendly, with inference when you want it.
Explicit types
State the type, then the name:
The everyday types:
| Type | Holds | Example |
|---|---|---|
int | whole numbers | 42 |
double | decimals | 3.14 |
string | text | "hello" |
bool | true / false | true |
char | one character | 'A' |
var: let the compiler infer
When the value makes the type obvious, var saves repetition:
var is not loose typing — the type is fixed at compile time, just
inferred rather than spelled out. var n = 12; n = "hi"; is still an
error. Use var when the right-hand side is clear, explicit types when
they aid readability.
The compiler has your back
Assigning a string to an int is caught before the program runs — the
same strong-typing safety you met in TypeScript, Java, and Swift, here
with C#'s polish.
Is var in C# the same as untyped/dynamic?
Which type best stores a price like 4.99?