kcoul
kcoul

Reputation: 150

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.

Upvotes: 5

Views: 1280

Answers (2)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

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.

Upvotes: 1

Pierre-Luc Pineault
Pierre-Luc Pineault

Reputation: 9201

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

Upvotes: 5

Related Questions