2

Possible Duplicates:
What's the point of the var keyword?
ReSharper and var

I am using ReSharper Tool for code cleanup. it always prompt to use var instated of actual type name. it looks good but is there any advantages are there?

Examples:

when we use

Dictionary<long, Profile> dicProfile = new Dictionary<long, Profile>

it prompt to convert it to ::

 var dicProfile = new Dictionary<long, Profile>
Community
  • 1
  • 1
Posto
  • 7,362
  • 7
  • 44
  • 61

3 Answers3

3

ReSharper has two types of warnings when the type can be inferred. You get a squiggly line when the type of your variable is exactly the same as the instantiated type:

Dictionary<long, Profile> dicProfile = new Dictionary<long, Profile>();

And you get a shorter underscored line when the variable's type is a base class of the instantiated type (like the IDictionary<> interface in this case):

IDictionary<long, Profile> dicProfile = new Dictionary<long, Profile>();

Whether you want this, is personal preference. I like this feature, especially in the first case, because it seems redundant to me to explicitly declare the type of a variable when it can be inferred.

If you don't want this you can turn this of by going here:

ReSharper -> Options -> Code Inspection -> Inspection Severity -> Language Usage Upportunities -> Use 'var' keyword when initializer explicitly declares type

And here:

ReSharper -> Options -> Code Inspection -> Inspection Severity -> Language Usage Upportunities -> Use 'var' keyword when possible

Rian Schmits
  • 3,096
  • 3
  • 28
  • 44
0

Quite a few resharper options are opinion based.

  • Use var instead of actual type name.
  • whatever == true is redundant.
  • this. is redundant
  • etc..

You can turn them all of in the resharper settings, or at the time it prompts you.

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
0

Its up to you, if you want to follow what re sharper suggestion.

But Re-sharper really help .net developer to:

clean you codes 
promotes coding standards
fix syntax/refactor
remove  redundancy object declarationy using var: 

Object data = new Object to  
var data = new Object()

 And Many More..

Same Post

Regards

Community
  • 1
  • 1
Crimsonland
  • 2,194
  • 3
  • 24
  • 42