<?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>Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx</link><description>(Writers note: I apologize for this C#-related post. I'll return to posting trivial and useless information, reviews, and links soon) A post on one of our internal groups came up asking whether a class's implementation should use properties on that class,</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471418</link><pubDate>Mon, 19 Sep 2005 22:18:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471418</guid><dc:creator>Haacked</dc:creator><description>What is this crap?  I subscribed to your blog when you were a PM on the C# team, but when your blog changed towards trivial and useless information as well as excrutiating detail on your bike rides, I thought, &amp;quot;Now THIS is a blog worth reading.  Eric has finally brought his A game!&amp;quot;&lt;br&gt;&lt;br&gt;But then you soil it by returning this insightful comment on &amp;quot;Premature Generalization&amp;quot; in C#.  What do you expect to accomplish with this extremely helpful and useful post?  Ewww.</description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471420</link><pubDate>Mon, 19 Sep 2005 22:18:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471420</guid><dc:creator>tzagotta</dc:creator><description>I'm finding more and more that I am using properties to set initial values in constructors (either from constructor parameters or otherwise).  The reason I am doing this is that I have side-effect code in the set accessor that I want to run when the value is assigned in the constructor.  So I would say these sorts of cases would be best handled by having properties.&lt;br&gt;&lt;br&gt;I'm not sure why using properties would make life more difficult for a maintenance programmer.  If you consider the possibility that in some cases you might want to use properties, and other cases, you might want to directly access fields, then when programming or maintaining a class, you have to think about whether to directly use the field or accessor each time.  Maybe always using an accessor is easier in that case?  What do you think?&lt;br&gt;&lt;br&gt;Also, with the awesome refactoring tools built into VS2005, encapsulating a field into a property is such a painless operation, that I can't really see the practical value of always doing that up-front, except for cases like the one I described above.</description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471429</link><pubDate>Mon, 19 Sep 2005 22:25:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471429</guid><dc:creator>ericgu</dc:creator><description>Haacked,&lt;br&gt;&lt;br&gt;You had me going there...</description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471433</link><pubDate>Mon, 19 Sep 2005 22:27:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471433</guid><dc:creator>ericgu</dc:creator><description>Tzagotta,&lt;br&gt;&lt;br&gt;Perhaps I can explain a bit more clearly.&lt;br&gt;&lt;br&gt;When you're doing maintenance, you spend a fair amount of time reading through code, trying to discover or (if it's your own code) remember how something works. In doing so, you need to keep a lot of possibilities in mind - does it do x? does it do y?&lt;br&gt;&lt;br&gt;To the extent that you can constrain the things that you need to learn to understand a class, you make it easier for that class to understand. &lt;br&gt;&lt;br&gt;That's the argument, though you don't have to agree with it. </description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471452</link><pubDate>Mon, 19 Sep 2005 23:05:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471452</guid><dc:creator>RichB</dc:creator><description>Deja-vu?&lt;br&gt;&lt;br&gt;I'm sure I've read a blog post from you 18 months ago which talked about using public fields rather than go to the hassle of creating properties with private storage.</description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471503</link><pubDate>Tue, 20 Sep 2005 00:52:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471503</guid><dc:creator>Darren Oakey</dc:creator><description>I have a dislike for unexpected side-effects - so have a personal rule that any object which can be changed post-creation must also raise an event to say that that change has occurred - the corollory being - don't create objects that are changed post-creation!&lt;br&gt;&lt;br&gt;But, for brevity and maintainability I've started using public fields instead of properties - the magic is the readonly field:&lt;br&gt;&lt;br&gt;public class Employee&lt;br&gt;    public readonly string FirstName;&lt;br&gt;    public readonly string LastName;&lt;br&gt;    public readonly Date Birthdate;&lt;br&gt;    public Employee( string name, string lastName, date birthdate)&lt;br&gt;    { &lt;br&gt;       Ensure.NotMissing( &amp;quot;firstName&amp;quot;, firstName );&lt;br&gt;       Ensure.NotMissing( &amp;quot;lastName&amp;quot;, lastName );&lt;br&gt;       Ensure.NotMissing( &amp;quot;birthdate&amp;quot;, birthdate );&lt;br&gt;       FirstName = firstName;&lt;br&gt;       LastName = lastName;&lt;br&gt;       Birthdate = birthdate&lt;br&gt;    }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;Not too much can go wrong with that - and I find it very quick to make and easy to read.&lt;br&gt;</description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471701</link><pubDate>Tue, 20 Sep 2005 12:30:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471701</guid><dc:creator>barrkel</dc:creator><description>The biggest elephant you folks are missing is binary compatibility. If you have a library that exposes something as a field, you can't change that later to be a property.&lt;br&gt;&lt;br&gt;If there's any chance that an already deployed version of the library exists in the wild along with clients that reference the field, changing it to a property is going to break those clients, especially if the library is in the GAC (which can still be desirable, for updating reasons).&lt;br&gt;</description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471729</link><pubDate>Tue, 20 Sep 2005 15:29:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471729</guid><dc:creator>robburke</dc:creator><description>I'd agree with the post and add that, in the event that a subsequent refactor from a Field to a Property is necessary, it's highly unlikely to introduce breaking changes, unless you're doing reflection tricks and iterating over Fields or something like that.  So I tend to err on the side of not making everything a Property if the getters and setters are trivial and the thing is public to begin with.&lt;br&gt;&lt;br&gt;That being said... there are times I wish I could make public Fields behave more like public Properties.  I wish I could easily ask the PropertyGrid to display my Fields as Properties, for instance...</description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471730</link><pubDate>Tue, 20 Sep 2005 15:30:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471730</guid><dc:creator>robburke</dc:creator><description>barrkel - oops... you're right of course.  If it's in the wild you're out of luck. </description></item><item><title>re: Accessing private fields through properties...</title><link>http://blogs.msdn.com/ericgu/archive/2005/09/19/471327.aspx#471782</link><pubDate>Tue, 20 Sep 2005 17:47:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:471782</guid><dc:creator>ericgu</dc:creator><description>Barrkel,&lt;br&gt;&lt;br&gt;I did say that there's an exception if you're building a library that you need to version separately. I think both of those are important - there are many libraries that always ship as a unit with their clients and/or for which a recompile is not an issue.&lt;br&gt;&lt;br&gt;Eric</description></item></channel></rss>