Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper Policy - how to recommend var conditionally?

I want to create a custom policy for Resharper so that it recommends 'var' iff the actual type is specified on the right hand side of '=' immediately after the 'new' keyword, and to disable the recommendation to use 'var' otherwise.

Is this possible? If so, how?

Thanks.

like image 289
kcoul Avatar asked Apr 06 '15 20:04

kcoul


2 Answers

You don't need a custom rule for that, there's already an option in ReSharper 9. I cannot confirm it's there for the previous versions.

You can find it by going to the ReSharper tab, select Options..., and in the pop-up go to Code Editing, C# and finally Code Style.

Change the options for the first section from Use 'var' to Use 'var' when evident.

ReSharper var option

This results in the following:

Conditionnal var option recommendation

like image 59
Pierre-Luc Pineault Avatar answered Oct 02 '22 14:10

Pierre-Luc Pineault


You actually have two different type of notifications:

notifications

The first one is a suggestion, the second one is a hint.

So you can simply click on the (second in this case) notification to show the bulb:

bulb

Then select "Do not show". It won't affect the other suggestion.

options

You can save to the settings layer you want after that.

like image 27
Lucas Trzesniewski Avatar answered Oct 02 '22 14:10

Lucas Trzesniewski