I have checked 5 or more post in stackoverflow regarding var usage but I am still looking for an answer regarding var usage. I am used to use Concrete type instead of var, but my Resharper complains to change to var. Is var a choice of type - even when concrete type is known?
-
2You can configure ReSharper to only make a suggestion about that. But, BTW, ReSharper is always right. – John Saunders Dec 05 '13 at 05:10
-
In terms of saving hard disk space and key strokes var is better. But i don't know how much time var saves for compiler. – last-Programmer Jun 19 '19 at 20:37
3 Answers
The following is an extract from msdn...
The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code. One example where var is helpful in this manner is with nested generic types such as those used with group operations. In the following query, the type of the query variable is IEnumerable>. As long as you and others who must maintain your code understand this, there is no problem with using implicit typing for convenience and brevity.
However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.
Reference: http://msdn.microsoft.com/en-us/library/bb384061.aspx
Good Luck!

- 2,397
- 19
- 21
I prefer to use var
when the type is obvious to improve readability:
var myVariable = new List<string>();
var myVariable2 = (int) someOtherVariable;
If the type is not obvious, I specify it explicitly. For example the return value of a method:
int myVariable3 = GetSomeValue();

- 8,485
- 1
- 42
- 61
From
Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
I think the important part here for you is
An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.
Further to that, from Implicitly Typed Local Variables (C# Programming Guide)
Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library. It is important to understand that the var keyword does not mean "variant" and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type.

- 162,879
- 31
- 289
- 284