Resharper 9 C# var changes

What changed in the new Resharper 9 to destroy my var settings?

What I want is very simple, and worked fine in the last version.  Use var when the type is explicit.

Apparently there used to be a setting for this (but now it's gone?).  Now there is some nonsensical var settings like "for built-in types", "when type is simple", and "elsewhere".  Which make literally no sense what-so ever.  I don't care if it's a simple type.  I don't care if it's a built-in type.  That has absolutely nothing to do with var usage.  I care that the type is explicit.

Can anyone help me?  Thanks.

5 comments
Comment actions Permalink

Check out the Code Style page in Options, under C#. It's pulled the settings for var (and other things) from Code Cleanup and Inspections, and put them in the same place, so it's easier to find (incidentally, open the options page, type "var" in the search box and you'll see all settings related to var).

0
Comment actions Permalink

I see those settings.  And they don't do what I want or make any sense to me.  You get to control var usage by "built-in types", "simple types" and "elsewhere".  What?!  Why in the world would I care if it's a simple type or not?  Mind blown.  The keyword var has absolutely nothing to do with type.  If you're a dev on my team and using var only for certain types it's time to take away your keyboard.

I'm sure Resharper imported my settings when it upgraded.  It looks like they have a breaking change with this new major release that destroyed their var usage.

If I can't find a fix for this I'm switching to a competitor.  It's a bit ridiculous.  All I want is to use "var" when the type is explicit and not implicit (no need to mouse over).  Resharper 8 and before did this perfectly fine.  What is wrong with Resharper 9?  Why did they fix what isn't broken?

The old Resharper option, I believe, was "use var when initializer explicitly declares type".  I would attach a screenshot of my options page but apparently the "Insert Image" functionality on this page is broken.

0
Comment actions Permalink

I think the new scheme does pretty much everything the old scheme does, just stated differently. The reason you can specify var settings per type kind is that this type kind is not always obvious from usage. E.g. var things = GetThings(); - does this return a list (simple class) or an array ("elsewhere")? var name = GetName(); - does this return a string (built-in) or a Name class (simple)? If you don't care about these differences, just set them all to the same value.

As for the values themselves, they give you the same options you had in ReSharper 8 - always use 'var', always use explicit type, or "use var when evident". This last one sounds like it's what you want - use var in an expression such as var foo = new Foo(); but use the explicit type for something like Foo foo = GetFoo();

The only real difference with ReSharper 8 is that you no longer get to specify different settings for "foreach" and local variable declarations, probably due to changes in the language for C#6 and the ability to declare variables in different locations.

0
Comment actions Permalink

Thanks.  I found the setting.  I was just looking at "Hint", "Suggestion", etc in the Inspection Severity section.  And when you pointed to the Code Style section it looked redundant and Inever thought to check the drop down.  It was a little bit hidden to me.  You're right that I want "Use var when evident" for all settings.

Why it got changed to be type-based I will never understand.  Does "var foo = GetFoo()" return a string, generic class or simple type?  If someone can infer a type (or subset of types) because of the var usage I'm very, very scared.  But hey as long as I never work with coders who use var according to type I'm happy.

I made the changes and the first thing I've noticed is now it seems to enforce it both ways.  Resharper 8 used to just suggest to use var in some cases.  Now Resharper 9 also suggests NOT to use var as well.  That's a big difference but I can live with it.

Anyways, this is close enough for me.  Thanks again for your help.

0
Comment actions Permalink

Does "var foo = GetFoo()" return a string, generic class or simple type?  If someone can infer a type (or subset of types) because of the var usage I'm very, very scared.  


It should return a Foo. Whether that's an IFoo, a FooSimple, or a ComplexFoo is irrelevant. If your method returns anything other than a "Foo" it's bad design.

I think R# needs to get even more intelligent about var analysis, it should actually do inspection based on the method names and return types. var foo is valid for all of IFoo, FooSimple and ComplexFoo, it should yell about var foo being assigned a Bar.

0

Please sign in to leave a comment.