var / const
Variables and immutable constants.
Syntax
var name = value; // const Type NAME = value;
Description
With var, the type is inferred from the value, and the value can change later. const is a compile-time constant and cannot be reassigned. You can also explicitly write the type (int, double, string, bool).
Example
Loading editor…
Expected output
C# 20
When to use?
General variables; use const for values that shouldn't change.
Common mistakes
- ⚠️ Trying to reassign a const constant causes a compilation error.