C# Tutorial: Variables
Declare variables with var and explicit types, use const and readonly, and understand C# type inference.
Storing data
Variables are named containers for data. C# is statically typed — every variable has a type known at compile time.
You can be explicit (`int x = 5;`) or use `var` to let the compiler infer the type (`var x = 5;`).
What you'll learn in this C# variables tutorial
This interactive C# tutorial has 8 hands-on exercises. Estimated time: 15 minutes.
- Declare and assign — Declare a variable with its type and name:
- Type inference with var — `var` lets the compiler infer the type from the initial value.
- Reassigning variables — Variables can change. Just use `=` (not `var` or type again).
- Constants — `const` creates a compile-time constant. The value can never change.
- Multiple assignments — Declare multiple variables on separate lines or assign several at once.
- Arithmetic operators — Standard operators: `+`, `-`, `*`, `/`, `%`.
- Increment and decrement — `++` and `--` increment and decrement by 1.
- Checking types with GetType — Every variable has a type. Use `.GetType()` to inspect it at runtime.
C# Variables concepts covered
- Storing data