Access automatic property - c#

前端 未结 2 1823
梦谈多话
梦谈多话 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; }
    }
    

提交回复
热议问题