Skip to content

C# Feature – var (Implicit typing)

var lets the compiler infer the exact type of a local variable from the initializer, improving readability without sacrificing static types.

C# 3 (2007)

Dictionary<string, List<int>> map = new Dictionary<string, List<int>>();
var map = new Dictionary<string, List<int>>();
var n = 42; // int
var s = "text"; // string
  • Don’t overuse; keep clarity. Avoid var when the type is not obvious from the right-hand side.
  • Works with target-typed new (C# 9).