Click here to Skip to main content
15,936,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is var Keyword in C# and Why Resharper keep suggesting to use var ?
Posted
Comments
Rob Philpott 13-Nov-13 4:37am    
Don't get me started. Tell Resharper to shut up.

What about reading the documentation[^]?
 
Share this answer
 
I can't tell you why ReSharper praise var over strongly typed version (there is an endless debate over which one to use, and it seem that ReSharper took side in favor of var), but I can tell you that there are no difference after compilation (no var in CLR, it's a compiler feature). If the compiler can't figure out the type (or create an anonymous type for i) it will throw an error.

The var 'type' was introduced to handle cases where the code you wrote creates a new, but anonymous type, and you are too lazy (IMO) to declare a type for it and use it...

See here
http://msdn.microsoft.com/en-us/library/vstudio/bb383973.aspx[^]
 
Share this answer
 
Like almost all of ReSharper's auto-type/intellisense facilities, the end-user can modify how 'var is handled, choosing from six options (!) including just having R# suggest using it, never suggest it, or automatically using it (the default):

"You can disable this suggestion through "ReSharper | Options | Code Inspection | Inspection Severity:" [^].

To me using 'var makes the most sense where the Type definition ... without 'var ... would appear on both left and right hand sides of the assignment operator.

If I were going to write:
Dictionary<dictionary><string,>, List<int>> dctDctList = new Dictionary<dictionary><string,>, List<int>>();</int></dictionary></int></dictionary>
Then, I do appreciate being able to write:
var dctDctList = new Dictionary<dictionary><string,>, List<int>>();</int></dictionary>
But, the other major use of 'var, as a "placeholder" for a run-time compiler-generated complex Type, is just as valid.
 
Share this answer
 
Var is used to declare implicitly typed local variable means it tells compiler to figure out the type of the variable at compile time. 
Since, 'var' is anonymous type, hence it is used where we don't know the type of output 

I think Resharper always suggest to use var because using var made your code more readable and there are no performance issues between these two it is just a style of Coding ....
Hope It Helps..... 
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 13-Nov-13 8:43am    
"Resharper always suggest to use var because using var made your code more readable..."

It's really a matter of opinion. For myself I like to know what type a variable is just by looking on its declaration...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900