Use private or used properties? FROM#

Note that the following code in a class is one class

private string _fee;
private string _receipt;

public string Fee
{
    get { return _fee; }
    private set { _fee = value; }
}

public string Receipt
{
    get { return _receipt; }
    private set { _receipt = value;}
}

public MyValue(string fee, string receipt) : this()
{
    _fee = int.Parse(receipt).ToString();
    _receipt = receipt;
}

As you can see, my property does nothing, so I have to use

_fee = int.Parse(fee).ToString();
_receipt = receipt;

or

Fee = int.Parse(fee).ToString();
Receipt = receipt;
+3
source share
4 answers

Use properties, and if you are in C # 3, you should use automatically implemented properties as follows:

public string Fee
{
    get; private set;
}

public string Receipt
{
    get; private set;
}

public MyValue(string fee, string receipt) : this()
{
    this.Fee = int.Parse(fee).ToString();
    this.Receipt = receipt;
}
+16
source

I would always use properties - this gives you more flexibility:

  • you can later create more complex getter and setter methods, if necessary
  • you can specify different visibility for getter and setter
  • ,
  • ,

, , .NET , , , - , : -)

, - , , ( , ..) - , . - . , !: -)

+10

, .

, , virtual, , . , , .


: , OP. marc_s , .

+4

.

, , , .

This can get even worse if the properties are virtual, as mentioned by womp.

0
source

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


All Articles