Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper create property with backing field

Tags:

resharper

How do you create property with a backing field in Resharper?

This is the first thing you would want to do with a class and I cannot find how to it.

Its so simple.

like image 483
Malcolm Avatar asked Dec 01 '22 05:12

Malcolm


2 Answers

Within the body of the class, type prop and hit Tab. Supply the property's type and name. This will create an autoproperty (these days, it is typically one of these you would want to create).

Then, with the cursor on the property name, hit Alt+Enter and choose To property with backing field.

like image 74
AakashM Avatar answered May 05 '23 04:05

AakashM


The quickest way I've found is to type your property as if it exists already:

this.MyProperty = "hello";

Then Alt-Return on the property name, and choose Create Property 'MyProperty' then hit tab to choose between auto-property, managed backing field, or default member body.

I believe this is quicker than using the prop shortcut, hitting tab, specifying the property type, hitting tab twice, specifying the name, and then pressing Alt-Return on the property name and choosing the 'to property with backing field' selection.

You could also create a Live Template so that there is slightly less work, e.g:

/// <summary>
/// Private backing field for $Property$ property
/// </summary>
private $Type$ $BackingField$;

public $Type$ $Property$
{
    get
    {
        return this.$BackingField$;
    }

    set
    {
        this.$BackingField$ = value;     
    }
}

The parameter $BackingField$ can be generated automatically from $Property$ by the "first character in lower case" macro of the template editor and setting it to "Not editable".

But I believe the first method is the quickest and easiest.

like image 34
devdigital Avatar answered May 05 '23 04:05

devdigital