When using "var" to declare variables in C#, is there boxing/unboxing taking place?
Also is there a chance of a runtime type mis-match when using var to declare variables?
When using "var" to declare variables in C#, is there boxing/unboxing taking place?
Also is there a chance of a runtime type mis-match when using var to declare variables?
No. using var is compiled exactly as if you specified the exact type name. e.g.
var x = 5;
and
int x = 5;
are compiled to the same IL code.
var is just a convenience keyword that tells the compiler to figure out what the type is and replace "var" with that type.
It is most useful when used with technologies like LINQ where the return type of a query is sometimes difficult to determine.
It is also useful (saves some typing) when using nested generic declarations or other long declarations:
var dic = new Dictionary<string, Dictionary<int, string>>();