C# Feature β var (Implicit typing)
Overview
Section titled βOverviewβvar lets the compiler infer the exact type of a local variable from the initializer, improving readability without sacrificing static types.
Introduced In
Section titled βIntroduced InβC# 3 (2007)
Dictionary<string, List<int>> map = new Dictionary<string, List<int>>();var map = new Dictionary<string, List<int>>();Annotated Example
Section titled βAnnotated Exampleβvar n = 42; // intvar s = "text"; // stringGotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Donβt overuse; keep clarity. Avoid
varwhen the type is not obvious from the right-hand side.
Related Features
Section titled βRelated FeaturesβVersion Notes
Section titled βVersion Notesβ- Works with target-typed new (C# 9).