David McClelland
David McClelland

Reputation: 2756

Formatting Resharper backing fields for properties in C#

So this is a follow-up to Keith Nicholas' question from 2 years ago:

Formatting Resharper backing fields for properties in C#

My guess is this would probably involve a custom Type Members Layout. Is there a way to to this yet in the latest Resharper version (as of now, it's 5.1) yet?

Upvotes: 11

Views: 2439

Answers (4)

Lakerfield
Lakerfield

Reputation: 1091

Version 9.1 has a new option:

Resharper > Options > Code Editing > Place backing field above property

https://youtrack.jetbrains.com/issue/RSRP-411980#comment=27-961304

Upvotes: 8

Nick Josevski
Nick Josevski

Reputation: 4216

In Resharper 6.1 I have this kind of a template, that may solve the question, or at least help others.

Using the 'shortuct' + TAB combination, I call mine nprop, and got further in adding a comment section.

private $TYPE$ _$NAMEP$;

/// <summary>
/// The $CLASS$ $NAMEC$
/// </summary>
public $TYPE$ $NAME$
{
    get { return _$NAMEP$; }
    set { _$NAMEP$ = value; }
}

Here's an image of the extra customisation to help rename things so it's just a matter of typing 2 values.

property with template how-to

Upvotes: 1

Handcraftsman
Handcraftsman

Reputation: 6983

Sure, at least with Resharper 5.1.1727 you can add an entry like the following

<!--fields-->
<Entry>
  <Match>
      <Kind Is="field"/>
  </Match>
  <Sort>
    <Static/>
    <Readonly/>
    <Name/>
  </Sort>
</Entry>

to the Type Members Layout to indicate where you want the backing fields to appear in the class.

For example, if you want fields at the bottom of the class insert that section as the very last entry in the Default Pattern section:

  <!--Default pattern-->
  <Pattern>

Resharper 5.1 comes with a default entry that includes fields:

<!--fields and constants-->
<Entry>
  <Match>
    <Or>
      <Kind Is="constant"/>
      <Kind Is="field"/>
      <Kind Is="event"/>
    </Or>
  </Match>
  <Sort>
    <Kind Order="constant field"/>
    <Static/>
    <Readonly/>
    <Name/>
  </Sort>
</Entry>

so that it will not conflict with your new rule, remove field from the default entry e.g.

<!--events and constants-->
<Entry>
  <Match>
    <Or>
      <Kind Is="constant"/>
      <Kind Is="event"/>
    </Or>
  </Match>
  <Sort>
    <Kind Order="constant event"/>
    <Static/>
    <Readonly/>
    <Name/>
  </Sort>
</Entry>

Upvotes: 0

Related Questions