What is the difference between a variable declared with var and an explicit type?
Short answer
var uses compile-time type inference; the compiler still gives it a single static type. It is not dynamic — the type is fixed once inferred.
Advertisement
AModel answer
var uses compile-time type inference; the compiler still gives it a single static type. It is not dynamic — the type is fixed once inferred.
var count = 5; // int
var name = "Sam"; // string
// count = "x"; // compile errorReal-world example
Using var for obvious right-hand types (new lists, LINQ results) keeps code concise without losing type safety.