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)
Before
Section titled “Before”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"; // string
Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Don’t overuse; keep clarity. Avoid
var
when 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).