Click here to Skip to main content
15,879,053 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

I am using resharpper tool in visual studio which alwasy says that use var.
Even though I am using non complex thing it still ask me to use var. Is it good coding practise??

below are for example, it can be anything like simple int=10 like that,

C#
var sr = new StreamReader(csvPath);
               var csv = new CsvReader(sr);


Resharpper always suggest to use var
Posted
Updated 28-Aug-15 19:52pm
v2
Comments
Sergey Alexandrovich Kryukov 29-Aug-15 1:45am    
Where, in what language? Can you give us exact code sample where it is requires or advised, and what is exact message?
—SA
Torakami 29-Aug-15 1:53am    
Yes , I have updated my question now . Sorry for limited description
Sergey Alexandrovich Kryukov 29-Aug-15 2:07am    
No problem; thank you for the clarification; I'll answer the question.
—SA

The advice for mandatory use var is totally baseless. It's really the matter of taste; this feature is nothing but syntactic sugar and has its pro and contra.

Please see: https://en.wikipedia.org/wiki/Syntactic_sugar[^].

First, make sure you really understand what it does:
https://msdn.microsoft.com/en-us/library/bb384061.aspx[^],
https://msdn.microsoft.com/en-us/library/bb383973.aspx[^].

So, the benefits of var are quite apparent. I don't want to explain its drawbacks; it would be more correct to explain some benefits of explicit type declaration, without var. Consider this example:
C#
TheComplicatedTypeWhichIsEasyToForget myInstance = SomeFunction(/* ... */);
myInstance.//...What to do with it?

Note that SomeFunction is not a constructor, it is a factory function, so its name does not tell you the type. With Visual Studio, you may want to click on "Go to definition", to see what this type does. It's faster than doing the same on function and then on its return type. And it's more informative (and can be also faster) than Intellisense, because Intellisense may just give you too long list of members.

This is just one argument demonstrating that using explicit type declaration can be beneficial. Some other developer may list some other arguments.

You know, this is not the first time where I find recommendations of "additional" development tools quite questionable. I think I caught FxCope with quite unreasonable rules. And I found some StyleCope rules just idiotic. I would advise to be very careful with those "extras". The can be quite useful, but your critical thinking is a lot more important.

—SA
 
Share this answer
 
v2
Comments
Mehdi Gholam 29-Aug-15 3:49am    
5'ed
Sergey Alexandrovich Kryukov 29-Aug-15 5:44am    
Thank you, Mehdi.
—SA
Torakami 3-Sep-15 2:14am    
Thanks for clear explanation.
Sergey Alexandrovich Kryukov 3-Sep-15 2:30am    
You are very welcome.
Good luck, call again.
—SA
I personally think it is bad to use var everywhere. There are three types of var usage

1 mandatory - sometimes you have to use var, such as for anonymous types. This is fine.

2 to tidy the code - some linq statements, like grouping, return some pretty complicated and ugly types and you might want to use var to simplify the code. This is usually ok

3 everywhere else - normally bad.

The problem with var everywhere is you don't know what types are just by looking at the code, which I think is bad.

C#
var name = GetName()


What's name? A string? A Name class?

C#
var name = GetNames()


What's name? Is it IEnumerable<string>? Is it a List? I might treat the data differently depending on if it is IEnumerable or List. Sure you can easily find out the type, but you can't tell just by looking.

The other issue isn't exactly related to var itself, but I think for people that are starting coding, or don't yet fully understand types and the framework, var can almost prevent people fully understand types. More junior coders seem to think there is something magical about var and it is something that means types no longer exist, everything is the same. Look at any question involving casting\type mismatches and you can guarantee the person is using var.
 
Share this answer
 
The C# compiler does a lot of work on your behalf, the var keyword is one of them.

People like and dislike it, but for a statically compiled language like c# it is pretty good since you type less and the compiler will do the rest and give you errors if you make a mistake in usage.
 
Share this answer
 
Comments
Torakami 29-Aug-15 2:06am    
ok so you prefer to use var keyword most of the times ryt ??
Mehdi Gholam 29-Aug-15 2:09am    
For variables inside a method, yes, not for properties/fields etc.
Sergey Alexandrovich Kryukov 29-Aug-15 2:28am    
You did not really answer the question, I think. Is it good to use it all the time?
Anyway, please see Solution 2.
—SA
I find that it's not just about typing less, I mean after all the intellisense on its own reduces this by a lot.
It is about making the code more readable. In some cases var can actually increase the readability just by removing the additional noise of the type name.
For example in those statements that you wrote it's quite clear from the right side of the statement what type it is.

[EDIT]
To answer the question: Should you always use var?
No, a nice example of that is shown in the Sergey's answer, when it is ambiguous what type is the result of the statement's right side then you're actually making things worse.
[/EDIT]
 
Share this answer
 
v3

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