var / const
Try it yourself →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
using System; var name = "C#"; int score = 10; score = 20; Console.WriteLine(name); Console.WriteLine(score);
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.