var declares an implicitly typed local variable, whose type the compiler infers from the initializer. The variable is still strongly typed, and its type is fixed at compile time.
It only works for local variables initialized where they are declared. Use it when the type is obvious from the right side, like new List<int>(). It is required for anonymous types, which have no name you can write. It also shortens LINQ queries with long type names. Avoid it when the type is not clear from the code.
var number = 42; // int
var text = "Hello"; // string
var list = new List<int>(); // List<int>
// Required for anonymous types
var person = new { Name = "John", Age = 30 };
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
The diagram below the answer is the concept .
Jump to it ↓
Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:
Saved in this browser - sign in to keep your review list.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Keep going - a few more words and AI can grade it.
Interview lens
How interviewers actually use this question
Likely follow-ups
Why won't `var x = null;` compile, and can you use var for a field or a method's return type?The initializer must give the compiler a type, and null has none. var is for locals only, so fields, parameters and return types need an explicit type.
How is var different from dynamic?var is fixed at compile time and fully checked. dynamic skips compile-time checks and binds at runtime, so mistakes show up as runtime exceptions.
With `var list = new List<int>();`, what type is list, and what if you wanted it typed as IList<int>?var takes the exact type of the initializer, so List<int>. If you want the variable typed as IList<int>, you have to write that type out.
Does using var change the compiled code or runtime performance?No. The compiler works out the type at build time, so the IL is the same as the explicitly typed version. It's a readability choice, not a performance one.
var lets the compiler work out a local variable's type from the value you assign, so you don't write the type yourself.
It's still strongly typed. The type is fixed at compile time, so var number = 42 is an int and stays an int.
It only works for local variables you initialize where you declare them, so no fields and no declaring one empty.
I use it when the right side already shows the type, like new List<int>(), and it shortens LINQ code that has long type names.
It's required for anonymous types, because the compiler generates that type and there's no name you could write for it.
I avoid it when the type isn't clear from the code, like var result = Process(), because then the reader has to go look the type up.
Weak answers to avoid
Says var makes the variable dynamically typedThat's dynamic, not var. Say the compiler fixes the type at compile time, so after var n = 42 you can't assign a string to n.
Use var everywhere, or neverA blanket rule suggests you haven't thought about it. Say you use it when the type is obvious from the right side and write the type out when it isn't.
Forgets anonymous typesAnonymous types are the one case where var is required, not just a style choice. Say the compiler generates the type and there's no name you could write for it.
Claims var works anywhere a type goesvar only works for local variables that have an initializer. Using it for fields, parameters or return types, or writing var x = null, won't compile.
Interview lens
Likely follow-ups, what you can say, and the weak answers to avoid.