Search code examples
c#resharpervarpolicy

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.


Solution

  • 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