Auto Property Access - C #

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

public string foo {get;set;}

Can I get any link to this private field?

I want to do something like

public string foo {get{/*some code to check foo for nulls etc*/};set;}

Without losing this automatic property function and writing something like

private string _foo = null;
public string foo{get{_foo==null?_foo="hello"; return _foo;}set{_foo=value;}}
+5
source share
2 answers

The auto property support field is anonymous ; you cannot access it from your recipient or setter.

If you need to implement your own logic in your getter or setter, your property is no longer considered automatic.

, , :

private object _x;

public object X
{
    get { return _x; }
    set { _x = value; }
}
+6

"" get "" ( "" "" ). "", "".

+3

All Articles