<p>In the past we declared properties like this:</p>

<pre><code>public class MyClass
{
    private int _age;

    public int Age
    {
          get{ return _age;  }
          set{ _age = value; }
    }
}
</code></pre>

<p>Now we can do:</p>

<pre><code>public class MyClass
{
    public int Age {get; set;} 
}
</code></pre>

<p>My question is, how can I access the private variable that is created automatically using this notation?  </p>

<p>I would rather access the private variable and not the public accessor 'Age'. Is there a default notation to access the private variable, or it is just not possible?</p>

[{'answer_id': 61484, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 2, 'selected': False, 'text': "<p>You can't, it's a language feature as opposed to a IDE feature. To be honest i'd prefer then IDE to add the private variable in for you. I agree that it is slightly weird for the class to internally have to use the public entry point to access its own variables. Hence I don't use this new feature that much myself.</p>\n"}, {'answer_id': 61485, 'author': 'macbirdie', 'author_id': 5049, 'author_profile': 'https://Stackoverflow.com/users/5049', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>Behind the scenes what happens is the injection of a private member variable, prefixed with &lt;>k__AutomaticallyGeneratedPropertyField#</p>\n</blockquote>\n\n<p>From <a href="http://community.bartdesmet.net/blogs/bart/archive/2007/03/03/c-3-0-automatic-properties-explained.aspx" rel="noreferrer">C# 3.0 Automatic Properties explained</a></p>\n\n<p>Although it may be possible to use that private member directly, it\'s very hacky and unnecessary.</p>\n'}, {'answer_id': 61493, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 4, 'selected': False, 'text': '<p>This syntax is commonly called "syntax sugar", which means that the compiler takes that syntax and translates it into something else. In your example, the compiler would generate code that looks something like this:</p>\n\n<pre><code>[CompilerGenerated]\nprivate int &lt;Age&gt;k_BackingField;\n\npublic int Age\n{\n   [CompilerGenerated]\n   get\n   {\n      return this.&lt;Age&gt;k_BackingField;\n   }\n   [CompilerGenerated]\n   set\n   {\n      this.&lt;Age&gt;k_BackingField = value;\n   }\n</code></pre>\n\n<p>Even knowing all of that, you could <strong>probably</strong> access the backing field directly but that sort of defeats the purpose of using automatic properties. I say probably here because you then depend on an implementation detail that could change at any point in a future release of the C# compiler.</p>\n'}, {'answer_id': 61494, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 5, 'selected': False, 'text': "<p>Your usage of automatic properties implies that you do not need any getting/setting logic for the property thus a private backing variable is unneccessary.</p>\n\n<p>Don't use automatic properties if you have any complex logic in your class. Just go <code>private int _age</code> and normal getters/setters as you normally would.</p>\n\n<p>IMO, automatic properties are more suited for quickly implementing throwaway objects or temporary data capsules like:</p>\n\n<pre><code>public class TempMessage {\n    public int FromID { get; set; }\n    public int ToID { get; set; }\n    public string Message { get; set; }\n}\n</code></pre>\n\n<p>Where you don't need much logic.</p>\n"}, {'answer_id': 61503, 'author': 'Wilka', 'author_id': 1367, 'author_profile': 'https://Stackoverflow.com/users/1367', 'pm_score': 7, 'selected': False, 'text': "<p>The aim of the new automatic properties is to reduce the amount of boilerplate code you need to write when you just have a simple property that doesn't need any special logic in the get or the set. </p>\n\n<p>If you want to access the private member that these properties use, that's usually for a few reasons:</p>\n\n<ul>\n<li>You need to more than just a simple get/set - in this case, you should just avoid using automatic properties for this member.</li>\n<li>You want to avoid the performance hit of going through the get or set and just use the member directly - in this case, I'd be surprised if there really was a performance hit. The simple get/set members are very very easy to inline, and in my (admittedly limited) testing I haven't found a difference between using the automatic properties and accessing the member directly. </li>\n<li><p>You only want to have public read access (i.e. just a 'get') and the class write to the member directly - in this case, you can use a private set in your automatic property. i.e.</p>\n\n<pre><code>public class MyClass\n{\n    public int Age {get; private set;} \n}</code></pre></li>\n</ul>\n\n<p>This usually covers most the reasons for wanting to directly get to the backing field used by the automatic properties.</p>\n"}, {'answer_id': 61625, 'author': 'Wedge', 'author_id': 332, 'author_profile': 'https://Stackoverflow.com/users/332', 'pm_score': 3, 'selected': False, 'text': "<p>You shouldn't, and it's very unlikely you need to. If you need to access the property, just use the public property (e.g. this.Age). There's nothing special about the private field backing the public property, using it in preference to the property is just superstition.</p>\n"}]
['https://Stackoverflow.com/questions/61480', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1368/']