<p><em>Note: This was posted when I was starting out C#. With 2014 knowledge, I can truly say that auto-properties are among the best things that ever happened to the C# language.</em></p>

<p>I am used to create my properties in C# using a private and a public field:</p>

<pre><code>private string title;
public string Title
{
    get { return title;  }
    set { title = value;  }
}
</code></pre>

<p>Now, with <a href="http://en.wikipedia.org/wiki/.NET_Framework" rel="noreferrer">.NET</a> 3.0, we got auto-properties:</p>

<pre><code>public string Title { get; set; }
</code></pre>

<p>I know this is more a philosophical/subjective questions, but is there any reason to use these auto-properties except from saving five lines of code for each field? My personal gripe is that those properties are hiding stuff from me, and I am not a big fan of black magic.</p>

<p>In fact, the hidden private field does not even show up in the debugger, which is OK given the fact that the get/set functions do nothing. But when I want to actually implement some getter/setter logic, I have to use the private/public pair anyway.</p>

<p>I see the benefit that I save a lot of code (one vs six lines) without losing the ability to change the getter/setter logic later, but then again I can already do that by simply declaring a public field "Public string Title" without the need of the { get; set; } block, thus even saving more code.</p>

<p>So, what am I missing here? Why would anyone actually want to use auto-properties?</p>

[{'answer_id': 9308, 'author': 'Ray', 'author_id': 233, 'author_profile': 'https://Stackoverflow.com/users/233', 'pm_score': 5, 'selected': False, 'text': "<p>I personally love auto-properties. What's wrong with saving the lines of code? If you want to do stuff in getters or setters, there's no problem to convert them to normal properties later on.</p>\n\n<p>As you said you could use fields, and if you wanted to add logic to them later you'd convert them to properties. But this might present problems with any use of reflection (and possibly elsewhere?).</p>\n\n<p>Also the properties allow you to set different access levels for the getter and setter which you can't do with a field.</p>\n\n<p>I guess it's the same as the var keyword. A matter of personal preference.</p>\n"}, {'answer_id': 9309, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 6, 'selected': False, 'text': "<p>The three big downsides to using fields instead of properties are:</p>\n\n<ol>\n<li>You can't databind to a field whereas you can to a property</li>\n<li>If you start off using a field, you can't later (easily) change them to a property</li>\n<li>There are some attributes that you can add to a property that you can't add to a field</li>\n</ol>\n"}, {'answer_id': 9310, 'author': 'Jeff Atwood', 'author_id': 1, 'author_profile': 'https://Stackoverflow.com/users/1', 'pm_score': 8, 'selected': True, 'text': '<p>We use them all the time in Stack Overflow.</p>\n\n<p>You may also be interested in a discussion of <a href="http://blog.codinghorror.com/properties-vs-public-variables/" rel="noreferrer">Properties vs. Public Variables</a>. IMHO that\'s really what this is a reaction to, and for that purpose, it\'s great.</p>\n'}, {'answer_id': 9315, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 3, 'selected': False, 'text': '<p>I use auto-properties all the time. Before C#3 I couldn\'t be bothered with all the typing and just used public variables instead.</p>\n\n<p>The only thing I miss is being able to do this:</p>\n\n<pre><code>public string Name = "DefaultName";\n</code></pre>\n\n<p>You have to shift the defaults into your constructors with properties. tedious :-(</p>\n'}, {'answer_id': 9342, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 3, 'selected': False, 'text': '<p>I think any construct that is intuitive AND reduces the lines of code is a big plus.</p>\n\n<p>Those kinds of features are what makes languages like Ruby so powerful (that and dynamic features, which also help reduce excess code).</p>\n\n<p>Ruby has had this all along as:</p>\n\n<pre><code>attr_accessor :my_property\nattr_reader :my_getter\nattr_writer :my_setter\n</code></pre>\n'}, {'answer_id': 9398, 'author': 'David Wengier', 'author_id': 489, 'author_profile': 'https://Stackoverflow.com/users/489', 'pm_score': 2, 'selected': False, 'text': '<p>The only problem I have with them is that they don\'t go far enough. The same release of the compiler that added automatic properties, added partial methods. Why they didnt put the two together is beyond me. A simple "partial On&lt;PropertyName&gt;Changed" would have made these things really really useful.</p>\n'}, {'answer_id': 9529, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 6, 'selected': False, 'text': '<p>Yes, it does <em>just</em> save code.  It\'s miles easier to read when you have loads of them. They\'re quicker to write and easier to maintain. Saving code is always a good goal.</p>\n\n<p>You can set different scopes:</p>\n\n<pre><code>public string PropertyName { get; private set; }\n</code></pre>\n\n<p>So that the property can only be changed inside the class. This isn\'t really immutable as you can still access the private setter through reflection.</p>\n\n<p>As of C#6 you can also create true <code>readonly</code> properties - i.e. immutable properties that cannot be changed outside of the constructor:</p>\n\n<pre><code>public string PropertyName { get; }\n\npublic MyClass() { this.PropertyName = "whatever"; }\n</code></pre>\n\n<p>At compile time that will become:</p>\n\n<pre><code>readonly string pName;\npublic string PropertyName { get { return this.pName; } }\n\npublic MyClass() { this.pName = "whatever"; }\n</code></pre>\n\n<p>In immutable classes with a lot of members this saves a lot of excess code.</p>\n'}, {'answer_id': 13992, 'author': 'pbh101', 'author_id': 1266, 'author_profile': 'https://Stackoverflow.com/users/1266', 'pm_score': 1, 'selected': False, 'text': '<p>One thing to note here is that, to my understanding, this is <em>just</em> syntactic sugar on the C# 3.0 end, meaning that the IL generated by the compiler is the same.  I agree about avoiding black magic, but all the same, fewer lines for the same thing is usually a good thing.</p>\n'}, {'answer_id': 14006, 'author': 'ESV', 'author_id': 150, 'author_profile': 'https://Stackoverflow.com/users/150', 'pm_score': 1, 'selected': False, 'text': '<p>In my opinion, you should always use auto-properties instead of public fields.  That said, here\'s a compromise:</p>\n\n<p>Start off with an <a href="http://msdn.microsoft.com/en-us/library/7c5ka91b.aspx" rel="nofollow noreferrer">internal</a> field using the naming convention you\'d use for a property.  When you first either</p>\n\n<ul>\n<li>need access to the field from outside its assembly, or </li>\n<li>need to attach logic to a getter/setter</li>\n</ul>\n\n<p>Do this:</p>\n\n<ol>\n<li>rename the field</li>\n<li>make it private</li>\n<li>add a public property</li>\n</ol>\n\n<p>Your client code won\'t need to change.</p>\n\n<p>Someday, though, your system will grow and you\'ll decompose it into separate assemblies and multiple solutions.  When that happens, any exposed fields will come back to haunt you because, as Jeff mentioned, <a href="http://blogs.msdn.com/abhinaba/archive/2006/04/11/572694.aspx" rel="nofollow noreferrer">changing a public field to a public property is a breaking API change</a>.</p>\n'}, {'answer_id': 14048, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 3, 'selected': False, 'text': "<p>Auto-properties are as much a black magic as anything else in C#. Once you think about it in terms of compiling down to IL rather than it being expanded to a normal C# property first it's a lot less black magic than a lot of other language constructs.</p>\n"}, {'answer_id': 19624, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 0, 'selected': False, 'text': "<p>I use CodeRush, it's faster than auto-properties.</p>\n\n<p>To do this:</p>\n\n<pre><code> private string title;\npublic string Title\n{\n    get { return title;  }\n    set { title = value;  }\n}\n</code></pre>\n\n<p>Requires eight keystrokes total.</p>\n"}, {'answer_id': 30747, 'author': 'Domenic', 'author_id': 3191, 'author_profile': 'https://Stackoverflow.com/users/3191', 'pm_score': 4, 'selected': False, 'text': '<p>One thing nobody seems to have mentioned is how auto-properties are unfortunately not useful for immutable objects (usually immutable structs). Because for that you really should do:</p>\n\n<pre><code>private readonly string title;\npublic string Title\n{\n    get { return this.title; }\n}\n</code></pre>\n\n<p>(where the field is initialized in the constructor via a passed parameter, and then is read only.)</p>\n\n<p>So this has advantages over a simple <code>get</code>/<code>private set</code> autoproperty.</p>\n'}, {'answer_id': 35909, 'author': 'Andrei Rînea', 'author_id': 1796, 'author_profile': 'https://Stackoverflow.com/users/1796', 'pm_score': 0, 'selected': False, 'text': "<p>@Domenic : I don't get it.. can't you do this with auto-properties?:</p>\n\n<pre><code>public string Title { get; }\n</code></pre>\n\n<p>or</p>\n\n<pre><code>public string Title { get; private set; }\n</code></pre>\n\n<p>Is this what you are referring to?</p>\n"}, {'answer_id': 48237, 'author': 'Omer van Kloeten', 'author_id': 4979, 'author_profile': 'https://Stackoverflow.com/users/4979', 'pm_score': 2, 'selected': False, 'text': "<p>It's simple, it's short and if you want to create a real implementation inside the property's body somewhere down the line, it won't break your type's external interface.</p>\n\n<p>As simple as that.</p>\n"}, {'answer_id': 182226, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 5, 'selected': False, 'text': '<p>From Bjarne Stroustrup, creator of C++:</p>\n\n<blockquote>\n  <p>I particularly dislike classes with a lot of get and set functions. That is often an indication that it shouldn\'t have been a class in the first place. It\'s just a data structure. And if it really is a data structure, make it a data structure.</p>\n</blockquote>\n\n<p>And you know what? He\'s right. How often are you simply wrapping private fields in a get and set, without actually doing anything within the get/set, simply because it\'s the "object oriented" thing to do. This is Microsoft\'s solution to the problem; they\'re basically public fields that you can bind to.</p>\n'}, {'answer_id': 343414, 'author': 'Johnno Nolan', 'author_id': 1116, 'author_profile': 'https://Stackoverflow.com/users/1116', 'pm_score': 0, 'selected': False, 'text': '<p>My biggest gripe with auto-properties is that they are designed to save time but I often find I have to expand them into full blown properties later.</p>\n\n<p>What VS2008 is missing is an <strong>Explode Auto-Property</strong> refactor.</p>\n\n<p>The fact we have an <strong>encapsulate field</strong> refactor makes the way I work quicker to just use public fields.</p>\n'}, {'answer_id': 343495, 'author': 'Theo', 'author_id': 43402, 'author_profile': 'https://Stackoverflow.com/users/43402', 'pm_score': 4, 'selected': False, 'text': "<p>I always create properties instead of public fields because you can use properties in an interface definition, you can't use public fields in an interface definition. </p>\n"}]
['https://Stackoverflow.com/questions/9304', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/91/']