How can I automate the creation of immutable properties using ReSharper?

I find that I am creating many properties from a pattern:

private readonly MyType sth;

public MyClass(MyType sth) //class constructor
{
  this.sth = sth;
}

public MyType Sth
{
  get { return sth; }
}

Is there an easy way to automate the creation of these properties? Usually I:

  • enter field including readonly keyword
  • Select "initialize constructor options
  • Choose Encapsulate

Is it possible to do it faster?

+4
source share
4 answers

Please consider this live template:

private readonly $MyType$ $FieldName$;

public $ClassName$($MyType$ $FieldName$) //class constructor
{
  this.$FieldName$ = $FieldName$;
}

public $MyType$ $PropName$
{
  get { return $FieldName$; }
}

where is the order of the parameters:

  • Property_name
  • ClassName (on the Select Macro menu, select Contained Type Name)
  • Mytype
  • FieldName ( " " " ", PropName) - " "

http://screencast.com/t/aRQi0xVezXMb

, !

+2

# 6 :

private MyType sth { get; }

public MyClass(MyType sth) //class constructor
{
  this.sth = sth;
}

- getter. , " "; .

https://msdn.microsoft.com/en-us/magazine/dn879355.aspx

-, Getter, , , - , . , # 6.0, . , , - , .

+3

, .

public readonly MyType Sth {get; private set;}

public MyClass(MyType sth)
{
    Sth = sth;
}
0

R # ( prop):

public $TYPE$ $NAME$ { get; private set; }

, , .

The alt + enter "To readonly" option will be nice.

Attribute alt + enter> 'Property with Support Field', then delete setter + add readonly. Not very automatic.

0
source

Source: https://habr.com/ru/post/1535582/


All Articles