访问自动属性 ​​- c#

max*_*axp 5 c# properties

自动属性被添加到about .net 3中的语言中,无论如何,使用代码创建了一个"私有"字段:

public string foo {get;set;}
Run Code Online (Sandbox Code Playgroud)

是否有可能实际获得对此私有字段的任何引用?

我想做点什么

public string foo {get{/*some code to check foo for nulls etc*/};set;}
Run Code Online (Sandbox Code Playgroud)

不失去这个自动属性功能并编写类似的东西

private string _foo = null;
public string foo{get{_foo==null?_foo="hello"; return _foo;}set{_foo=value;}}
Run Code Online (Sandbox Code Playgroud)

Bol*_*ock 6

自动属性的支持字段是匿名的 ; 你无法从其getter或setter中访问它.

如果您需要在getter或setter中实现自己的逻辑,那么无论如何都不会将您的属性视为自动.

自动属性只是为了节省打字的乏味,以及观看的大量,这些:

private object _x;

public object X
{
    get { return _x; }
    set { _x = value; }
}
Run Code Online (Sandbox Code Playgroud)