How can I access the backing variable of an auto-implemented property?

前端 未结 6 1970
逝去的感伤
逝去的感伤 2020-12-23 02:21

In the past we declared properties like this:

public class MyClass
{
    private int _age;

    public int Age
    {
          get{ return _age;  }
                  


        
6条回答
  •  再見小時候
    2020-12-23 02:44

    Behind the scenes what happens is the injection of a private member variable, prefixed with <>k__AutomaticallyGeneratedPropertyField#

    From C# 3.0 Automatic Properties explained

    Although it may be possible to use that private member directly, it's very hacky and unnecessary.

提交回复
热议问题