Preferred method to set get only property value Property: constructor vs backing field

Change . Although I accepted David 's answer, Jon's answer should also be considered.

Which method is preferable for setting a read-only value (receive only?) Property: use a support field or use a constructor? Suppose the design is for a property, not a field (there may be an update in the future that requires the property to have a setter that would prevent the use of the field).

Given the following simple example, which method is preferred? If someone prefers another, why?

Option 1 (support field) :

class SomeObject
{
    // logic
}

class Foo
{
    private SomeObject _myObject;
    public SomeObject MyObject
    {
        get
        {
            if( _myObject == null )
            {
                _myObject = new SomeObject();
            }
            return _myObject;
        }
    }

    public Foo()
    {
        // logic
    }
}

2 ():

class SomeObject
{
    // logic
}

class Foo
{
    public SomeObject MyObject { get; private set; }

    public Foo()
    {
        MyObject = new SomeObject();
        // logic 
    }
}
+3
4

, "new SomeObject();" , .

MyObject Foo(), 1 - , . , Google Chrome, .

MyObject , getter , 2.

+7

. , , , - , , .

"" , , ... . , , . , :

public class Person
{
    private readonly string name;
    public string Name { get { return name; } }

    public Person(string name)
    {
        this.name = name;
    }
}

, - . , , , , , . , , # 4, .

, :

class Foo
{
    private readonly MyObject myObject;
    public SomeObject MyObject { get { return myObject; } }

    public Foo()
    {
        myObject = new MyObject();
        // logic 
    }
}

, SomeObject . , , , .

, , . , , , , , . , :

class Foo
{
    private SomeObject _myObject;
    public SomeObject MyObject
    {
        get
        {
            if( _myObject == null )
            {
                _myObject = new SomeObject();
            }
            return _myObject;
        }
        set
        {
            // Do you want to only replace it if
            // value is non-null? Or if _myObject is null?
            // Whatever logic you want would go here
            _myObject = value;
        }
    }

    public Foo()
    {
        // logic
    }
}

, :

  • , ?
  • ?
  • - ?

, . "", "" .

+5

:

class Foo
{
    public SomeObject MyObject { get; private set; }

    public Foo()
    {
        MyObject = new SomeObject();
    }
}
+2

, .

class Foo
{
    private SomeObject myObject;

    public SomeObject MyObject
    {
        get { return myObject; }
    }

    public Foo()
    {
        myObject = new SomeObject();
    }
}

Why? I personally don’t use it private set, simply because I need to remember which properties have fallback variables and which don’t, which means that in all code, some fields use lowercase and some uppercase. It seems to me that these minor “inconsistencies” bother my OCD.

This is only a minor style. Six of them, half a dozen others. I have nothing against option number 2, and do not mind the idiom private set.

0
source

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


All Articles