tags:

views:

347

answers:

5

Possible Duplicates:
What to use: var or object name type?
Use of var keyword in C#
What’s the point of the var keyword?
Should I always favour implictly typed local variables in C# 3.0?

I have just installed a trial version of ReSharper to evaluate it for my company. One thing that I have noticed is it is suggesting that I change the following (made up example):

string s = "";

to

var s = "";

Is it best practice to use the var keyword rather than using the Object Type when declaring variables? What advantages does it give. For context I am a former Java developer who has just transitioned to the .Net works.

+3  A: 

I find it helpful in some cases where the type declaration is very long, for example:

Dictionary<int, string> item = new Dictionary<int, string>();

becomes

var item = new Dictionary<int, string>();
CodeSpeaker
+4  A: 

It amounts to the same thing because using the var keyword, the variable is implicity typed and the compiler infers the type at build time. I prefer to specify the type rather than use var in most cases, so I change my resharper settings.

Charlie
-1: The questioner hows that and wants to know the basis on which would go about deciding which way to fall.
Ruben Bartelink
I don't think my answer is useless and deserves a downvote though
Charlie
Fine, downvote undone - seems others are of a similar view (or are they trying to counter my 'nastiness' ? I gave it the -1 to counter a single +1 at the time -- I believe answers that dont add much arent the end of the world as long as they stay at zero and dont potentially make it take longer to get to an answer that actually has something insightful to say. IOW There's a reason why http://stackoverflow.com/questions/1205329/c-var-keyword-usage/1205396#1205396 has my +1 and others' too.
Ruben Bartelink
Personally I'd only downvote wrong or misleading answers, its up to you though. The question is about best practise anyway so the answers are going to be subjective. I said I prefer explicity types variables in most cases. As codespeaker says, it can look neater when the type decleration is long, I'd probably use var in that case to improve readability.
Charlie
What I was getting at is that on a 'best practice' question where the answer is really 'it depends', saying yes or no is not enough - it needs to be "I always choose Y/N because ....[, even though I know some people say ... - which I disagree with because ...". The Resharper settings point is a unique and useful point in this context though .... I'll actually flip flop and go +1 on that basis upon mature reflection!
Ruben Bartelink
+1  A: 

As i remember it Resharper just says that it could be written this way. It doesn't say that you should.

Alot of suggestions is just suggestions which I turned of the first time i saw them.. That being one of them.

Another was that it said was that it is redundant to write "this.someProperty", while i think the code gets easier to read by doing that.

ullmark
A: 

Resharper also has suggestion to transform if statement to true or not true. Almost the same thing, but it depends on your style of coding. So, when you find using var more comfortable - use this suggestion.

Sorantis
+4  A: 

I think it's fine to use var where it makes the code easier to read, which for me would mean that the type that var is replacing must be completely obvious.

For example, this would be a good use of var (contrived example):

var thing = new Dictionary<int, KeyValuePair<string, int>>();

However this would be a bad use of var:

var thing = GetThingFromDatabase();
Jon Grant