Correct use of C# properties

后端 未结 12 700
生来不讨喜
生来不讨喜 2020-12-16 15:19
private List _dates;

public List Dates
{
    get { return _dates; }
    set { _dates = value; }
}

OR

publi         


        
12条回答
  •  别那么骄傲
    2020-12-16 16:09

    As long as you don't have to make sure the List is initialized in the getter you are fine with both versions. I just suggest that you are using one version and you don't mix it in your code...

    If you need it initialized you have to go for the first version...

    private List _dates; 
    
    public List Dates 
    { 
        get { if (_dates == null) _dates = new List(); return _dates; } 
        set { _dates = value; } 
    } 
    

提交回复
热议问题