Access automatic property - c#

前端 未结 2 1824
梦谈多话
梦谈多话 2021-01-18 23:43

Automatic properties were added to the language in about .net 3 which create a \'private\' field, anyway, using the code:

public string foo {get;set;}


        
相关标签:
2条回答
  • 2021-01-19 00:09

    The backing field of an automatic property is anonymous; you can't access it from within its getter or setter.

    If you need to implement your own logic in your getter or setter, your property isn't considered automatic anymore anyway.

    Auto properties are simply there to save the tedium of typing, and eyesore of seeing, multitudes of these:

    private object _x;
    
    public object X
    {
        get { return _x; }
        set { _x = value; }
    }
    
    0 讨论(0)
  • 2021-01-19 00:17

    You can't have a an "automatic" get and a "manual" set (or a "manual" get with an "automatic" set). You must have both "manual" or both "automatic".

    0 讨论(0)
提交回复
热议问题