<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx</link><description>Here's a question I got from a C# user last year, a question I get fairly frequently: User: With “regular” explicit properties, I tend to use the private backing field directly from within the class. Of course, with an automatic property, you can’t do</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9319182</link><pubDate>Wed, 14 Jan 2009 21:33:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319182</guid><dc:creator>Patrick Dewane</dc:creator><description>&lt;p&gt;Perhaps the user was concerned with the unnecessary stack push needed when calling a property, when all the user really wants is the private backing field. I blogged about this a while back, and while I like the idea of automatic properties I'm not sure the savings in keystrokes is worth the stack push. Am I wrong about this?&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9319500</link><pubDate>Thu, 15 Jan 2009 01:00:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319500</guid><dc:creator>JD</dc:creator><description>&lt;p&gt;Patrick: I'd expect the jitter to inline the property access, so I'd be surprised if you actually end up incurring any runtime overhead (unless optimizations are disabled of course).&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9319894</link><pubDate>Thu, 15 Jan 2009 05:42:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319894</guid><dc:creator>SealedSun</dc:creator><description>&lt;p&gt;What I don't like about automatic properties is the lack of the readonly modifier. I really am a fan of immutable objects and would like the compiler to enforce the write-once semantics.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9319914</link><pubDate>Thu, 15 Jan 2009 05:59:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319914</guid><dc:creator>Sebastien Lorion</dc:creator><description>&lt;p&gt;Another reason for using &amp;quot;explicit&amp;quot; properties is to change the backing store (say a dictionary, the ViewState, etc.). In that case, no change is required also.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9319935</link><pubDate>Thu, 15 Jan 2009 06:11:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319935</guid><dc:creator>Renaud Bédard</dc:creator><description>&lt;p&gt;One scenario I often encounter (and I read on programming blogs that I'm not the only one) is that I need an event raised when a property value is changed, whether it be from the exterior or interior. Now it's impossible to do this with implicit properties, but I sure wish there would be an &amp;quot;eventful&amp;quot; modifier on them. I find implicit properties a lot cleaner and more often than not that event is the only thing I add.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9320234</link><pubDate>Thu, 15 Jan 2009 10:53:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9320234</guid><dc:creator>Petar Repac</dc:creator><description>&lt;p&gt;I think that the issue here is not tightly bound to automatic property. It answers the question &amp;quot;from within my class should I use private fields directly or go through the property&amp;quot;. &lt;/p&gt;
&lt;p&gt;Automatic properties force us to go the second way. And it sometimes it's not the right way to go. &lt;/p&gt;
&lt;p&gt;Very good post. Thanks Eric.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9320266</link><pubDate>Thu, 15 Jan 2009 11:14:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9320266</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;I too miss the the readonly ability, However the only way the semantics of this would correspond with normal readonly fields is for the set to only be legit *within* the constructor. As such object construction syntax would not be permitted (since they happen post construction). Given this this doesn't help much for the effort.&lt;/p&gt;
&lt;p&gt;What I really want is immutable declarations on classes/structs...&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9320293</link><pubDate>Thu, 15 Jan 2009 11:33:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9320293</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;&amp;quot;And it sometimes it's not the right way to go.&amp;quot;&lt;/p&gt;
&lt;p&gt;Why? can you come up with an example of why you wouldn't want to?&lt;/p&gt;
&lt;p&gt;There is only one I know of (which is fixed in 3.5 SP1 32bit) which is that if the property is a non primitive struct the get/set will not be inlined. I would think for most that's a boundary case and the only reason to ever change it is if you profiling suggests it is required.&lt;/p&gt;
</description></item><item><title>Periodic Digest of Interesting Stuff</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9320453</link><pubDate>Thu, 15 Jan 2009 12:47:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9320453</guid><dc:creator>Steve Strong's Blog</dc:creator><description>&lt;p&gt;Periodic Digest of Interesting Stuff&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9320463</link><pubDate>Thu, 15 Jan 2009 12:56:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9320463</guid><dc:creator>Petar Repac</dc:creator><description>&lt;p&gt;Matt, I was talking about using the &amp;quot;property access&amp;quot; within the class as not the right way to go. The example has been given by Eric in the post. It's when the semantics of accessing the internal object's state is different when we access it from the outside as opposed when we assess it from the inside.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9320495</link><pubDate>Thu, 15 Jan 2009 13:18:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9320495</guid><dc:creator>[ICR]</dc:creator><description>&lt;p&gt;&amp;quot;I'm not sure the savings in keystrokes is worth the stack push&amp;quot;&lt;/p&gt;
&lt;p&gt;If you run it in Release mode without a debugger attatched when you start the program (you can attatch one after) it will inline trivial properties.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9320742</link><pubDate>Thu, 15 Jan 2009 15:57:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9320742</guid><dc:creator>Stephen</dc:creator><description>&lt;p&gt;At first I thought automatic properties were a neat idea, in reality I find I hardly ever use them.. the only time I can usually rationalize their use is when I have 'business objects', but then I tend to find that having property change notifications is useful (or required), and then I might as well throw the automatic properties away, it doesn't really cause any problems.. I just think that automatic properties tend to be more useful when you are conceptualizing in code.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9321444</link><pubDate>Thu, 15 Jan 2009 22:48:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9321444</guid><dc:creator>Joe</dc:creator><description>&lt;p&gt;Am I the only one that has wanted an access modifier that was the exact opposite of &amp;quot;private&amp;quot;, that allowed you to define methods that could only be used from external classes? Probably. :) &lt;/p&gt;
&lt;p&gt;But that came up when dealing with this case. The class should have only used the private backing field, and never the public property&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9322524</link><pubDate>Fri, 16 Jan 2009 03:45:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9322524</guid><dc:creator>Kevin Westhead</dc:creator><description>&lt;p&gt;I agree with the comments about auto-implemented properties needing a better way to express read-only properties. I blogged about why I don't like the &amp;quot;private set&amp;quot; approach to simulating a read-only property.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9331016</link><pubDate>Fri, 16 Jan 2009 23:39:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9331016</guid><dc:creator>Stephen</dc:creator><description>&lt;p&gt;Yea one of the things I didn't like about private set was the fact that you don't get told the property is read-only.. you get told you don't have access to it.. erugh.&lt;/p&gt;
</description></item><item><title>re: Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx#9653349</link><pubDate>Fri, 29 May 2009 18:13:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9653349</guid><dc:creator>TheCPUWizard</dc:creator><description>&lt;p&gt;I *NEVER* access the backing field outside of the propery. EVER.&lt;/p&gt;
&lt;p&gt;If I need different semantics, then I use different properties:&lt;/p&gt;
&lt;p&gt;Money Balance { get; set; }&lt;/p&gt;
&lt;p&gt;Money SecureBalance { get { if (...) throw; return Balance }&lt;/p&gt;
</description></item></channel></rss>