Skip to content
KodeTrail

Camp 1 · Step 3 of 12

Variables and types

Declaring values with explicit types or var, and the everyday types C# programs run on.

14 min+50 XP

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:

C#Cloud run

The everyday types:

TypeHoldsExample
intwhole numbers42
doubledecimals3.14
stringtext"hello"
booltrue / falsetrue
charone character'A'

var: let the compiler infer

When the value makes the type obvious, var saves repetition:

C#Cloud run

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

C#Cloud run

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.

Checkpoint

Is var in C# the same as untyped/dynamic?

Checkpoint

Which type best stores a price like 4.99?