<?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>Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx</link><description>My blog reader burped recently, and gave forth a post (and reply ) than Rico wrote last September, but since I didn't comment on it then, I'll comment on it now. And, yes, I've written about this in the past... Basically, this revolves around the question</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1574630</link><pubDate>Thu, 01 Feb 2007 21:09:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1574630</guid><dc:creator>Peter Ritchie</dc:creator><description>&lt;p&gt;In principle, I agree. &amp;nbsp;If I haven't worked with dozens of clients working with code from hundreds of programmers where a public field invariably ends up being used externally (because it can). &amp;nbsp;This, of course, leads to great discomfort when the class is refactored and that field changes type.&lt;/p&gt;
&lt;p&gt;There's so many things to take into account when simply creating a class--most of which are ignored to simply &amp;quot;get it out&amp;quot;. &amp;nbsp;in .NET alone: internal or not, public or protected, sealed or not (which affects whether protected should be used), etc. &amp;nbsp;...often leading to simply accepting the defaults...&lt;/p&gt;
&lt;p&gt;While I agree to your comments in principle, you're not suggesting design of classes &amp;quot;intended&amp;quot; to be used only internally or within the same assembly (regardless of what you tell the compiler--by possibly not including &amp;quot;internal&amp;quot;) should be given the same amount of thought as an intentionally public-facing class. &amp;nbsp;...Which I'm afraid this is what some will take away, or will use in their &amp;quot;properties are bad&amp;quot; arguments (yes, there are still some out there).&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1574981</link><pubDate>Thu, 01 Feb 2007 22:55:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1574981</guid><dc:creator>Kevin Dente</dc:creator><description>&lt;p&gt;I'd love to see the ability to declare a trivial property and have the compiler generate the backing field and getters and setters. For example:&lt;/p&gt;
&lt;p&gt;public property string Name;&lt;/p&gt;
&lt;p&gt;That way I get the conciseness of fields, but can substitute in a more complex implementation if needed without breaking compatibility with existing clients. This is a lot like the event keyword. Syntactic sugar, but tasty nonetheless. :)&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1576902</link><pubDate>Fri, 02 Feb 2007 00:54:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1576902</guid><dc:creator>ericgu</dc:creator><description>&lt;p&gt;Kevin,&lt;/p&gt;
&lt;p&gt;Back after 1.0 shipped, we spent a fair amount of design team time trying to come up with something like that. We tried a bunch of different syntactical options, but didn't come up with one that worked well, and IIRC we decided that there were other more important things to discuss. I don't know what the team thinks about that now - at that point we were trying to tread lightly in terms of adding things, and Linq negates that approach a bit...&lt;/p&gt;
&lt;p&gt;Peter,&lt;/p&gt;
&lt;p&gt;I do think that classes with different clients have different design goals. And I do think that external classes require more work, simply because it's so much more painful to refactor them later if you don't get it right up front. &lt;/p&gt;
&lt;p&gt;I hadn't thought of internal classes becoming accidentally external. I'm not sure it changes my opinion, but it does provide some food for thought.&lt;/p&gt;
&lt;p&gt;Eric. &lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1579164</link><pubDate>Fri, 02 Feb 2007 05:12:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1579164</guid><dc:creator>Kevin Dente</dc:creator><description>&lt;p&gt;Hmm. What's wrong with&lt;/p&gt;
&lt;p&gt;public property string Name;&lt;/p&gt;
&lt;p&gt;:P&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1579175</link><pubDate>Fri, 02 Feb 2007 05:14:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1579175</guid><dc:creator>dhananjay123@hotmail.com</dc:creator><description>&lt;p&gt;In principal I agree with you. But exposing member field bothers me, now suddenly you have to change your naming convention for that member field, i.e. “memberField” has to change as “MemberField”, I believe it change readers perception about code when she see the code :)&lt;/p&gt;
&lt;p&gt;Dhananjay&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1580919</link><pubDate>Fri, 02 Feb 2007 11:12:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1580919</guid><dc:creator>Robin Debreuil</dc:creator><description>&lt;p&gt;I agree with this one too - and I have went back and forth and I'm sure I will continue to do so. One question is, should those public fields be capitalized? It used to make refactoring easier if you later add a property. Then again with tools now it is pretty easy either way. Still it is something I debate with myself every time (usually I use lowercase).&lt;/p&gt;
&lt;p&gt;Also, I'm not sure why they couldn't have used &amp;quot;public property string Name;&amp;quot; -- essentially the same thing is done with events. Also it could be a nice flag to the final jit code generator that the method call can be dropped for direct variable access in most cases (where now they probably have to find a usage pattern). &lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1580983</link><pubDate>Fri, 02 Feb 2007 11:26:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1580983</guid><dc:creator>Michal Talaga</dc:creator><description>&lt;p&gt;One place I see using public fields do not hurt but instead helps a lot, are the DTO (Data Transfer Objects). Making DTO use public fields no only saves you about 75% of the code, but also ensures that there will be no logic in there - which is a good thing for a DTO :-)&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1581346</link><pubDate>Fri, 02 Feb 2007 12:27:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1581346</guid><dc:creator>shuggy</dc:creator><description>&lt;p&gt;a property which could automagically generate FooChanaged events at the same time would be nice :)&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1582013</link><pubDate>Fri, 02 Feb 2007 14:50:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1582013</guid><dc:creator>Gavin Greig</dc:creator><description>&lt;p&gt;Kevin/Robin:&lt;/p&gt;
&lt;p&gt;The syntax you're suggesting for automatically generating the backing code for trivial properties is exactly what C++/CLI does, and it's one of the few areas where C++/CLI has better usability than C#.&lt;/p&gt;
&lt;p&gt;A mild annoyance is that the name of the generated field is not known, so you can't access the field directly even from within the host class, but I can't think of any reason why this would be an issue; it just doesn't chime with my preconception of where the boundaries &amp;quot;ought&amp;quot; to lie.&lt;/p&gt;
</description></item><item><title>also incompatible at source-code level</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1582397</link><pubDate>Fri, 02 Feb 2007 16:11:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1582397</guid><dc:creator>Kristof Verbiest</dc:creator><description>&lt;p&gt;Changing a public field in a property is not only binary incompatible but also at the source-code level! A field can be passed as an out- or ref- parameter while a property cannot.&lt;/p&gt;
&lt;p&gt;So even if you ship the clients of your class together with your class, just recompiling your code may not solve the problem.&lt;/p&gt;
&lt;p&gt;Therefore: always use properties!&lt;/p&gt;
&lt;p&gt;I've written something about this here: &lt;a rel="nofollow" target="_new" href="http://kristofverbiest.blogspot.com/2007_02_01_kristofverbiest_archive.html"&gt;http://kristofverbiest.blogspot.com/2007_02_01_kristofverbiest_archive.html&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1582463</link><pubDate>Fri, 02 Feb 2007 16:40:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1582463</guid><dc:creator>Dani</dc:creator><description>&lt;p&gt;I cannot really agree with your point. It may be true for the moment, that you can compile the whole thing together, but it doesn't have to be like that forever. Software is changing during the time, we all know that. So it might be possible, that in future, your class will be used inside a component for an external application (or whatever)... it's not a big deal to generate properties for fields and there's no real disadvantage... so&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1583784</link><pubDate>Fri, 02 Feb 2007 19:56:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1583784</guid><dc:creator>Steve Cooper</dc:creator><description>&lt;p&gt;This suggestion is probably an awful headache to implement, but it's more a musing on language design...&lt;/p&gt;
&lt;p&gt;In almost all ways, the trivial property and the public field are functionally identical. The only real difference is that one has compact syntax;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public string Foo;&lt;/p&gt;
&lt;p&gt;and one has obscenely verbose syntax;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;private string foo;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public string Foo &lt;/p&gt;
&lt;p&gt; &amp;nbsp;{ &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;get &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;return foo; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;} &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;set&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{ &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.foo = value; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;My question is, then, why not get rid of public fields at the C# level and just use the regular field syntax to generate the trivial property?&lt;/p&gt;
&lt;p&gt;The benefits are;&lt;/p&gt;
&lt;p&gt;- programmers don't need to worry about the field/property problem.&lt;/p&gt;
&lt;p&gt;- programmers write significantly less code; 12 lines becomes 1.&lt;/p&gt;
&lt;p&gt;- versioning becomes much easier, because you just can't switch from field to property.&lt;/p&gt;
&lt;p&gt;You lose the ability to write fields, but who cares? It feels to me (though I'm sure I'm missing something) that the programmer shouldn't have to make the distinction. Why can't we leave it to the compiler? &lt;/p&gt;
</description></item><item><title>More on properties vs fields</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1583820</link><pubDate>Fri, 02 Feb 2007 20:10:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1583820</guid><dc:creator>Eric Gunnerson's C# Compendium</dc:creator><description>&lt;p&gt;Some nice comments on what I wrote . First, a non-controversial question. Robin asked whether you would&lt;/p&gt;
</description></item><item><title>to: steve</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1584368</link><pubDate>Fri, 02 Feb 2007 22:07:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1584368</guid><dc:creator>zproxy</dc:creator><description>&lt;p&gt;Well, if there would be always generated/ automatic properties - that would be a very large overhead. :)&lt;/p&gt;
&lt;p&gt;Or maybe change syntax as such, that each native field should have a keyword &amp;quot;field&amp;quot; and without one it would be automatic property? But as the things are evolving with c#3 i do not see that happening.&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1586639</link><pubDate>Sat, 03 Feb 2007 05:07:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1586639</guid><dc:creator>rstrahl</dc:creator><description>&lt;p&gt;The way the language works is fine I'd say - having public field and the property mechanism gives you the most flexibility and the JIT will give you inlining for performance so you don't pay for trivial property implementations. This ensures we can do the right thing.&lt;/p&gt;
&lt;p&gt;But what would be cool if Visual Studio could help with Intellisense and the debugger display. An option to filter the property/field view would be handy. Public private (for this pointers). For example, most times I wish we could hide fields from the intellisense view. Or only show properties, only methods or only public etc.&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1591531</link><pubDate>Sat, 03 Feb 2007 20:42:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1591531</guid><dc:creator>Steven</dc:creator><description>&lt;p&gt;C# 3.0 is getting the automatic properties that Kevin proposes. The syntax is as follows:&lt;/p&gt;
&lt;p&gt;public string Name { get; set; }&lt;/p&gt;
&lt;p&gt;Look here:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://aspiring-technology.com/blogs"&gt;http://aspiring-technology.com/blogs&lt;/a&gt; /troym/archive/2006/11/19/53.aspx&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1597458</link><pubDate>Sun, 04 Feb 2007 16:46:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1597458</guid><dc:creator>Thomas Eyde</dc:creator><description>&lt;p&gt;I am not too fond of the proposed syntax for the trivial property; it looks too much like an abstract property. &lt;/p&gt;
&lt;p&gt;And why the { get; set}? If we leave out one of them, we have a useless property; one we can read, but not update. Or write to, but not read.&lt;/p&gt;
&lt;p&gt;No. I prefer public property string Name;&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1597765</link><pubDate>Sun, 04 Feb 2007 17:34:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1597765</guid><dc:creator>Steven</dc:creator><description>&lt;p&gt;Thomas,&lt;/p&gt;
&lt;p&gt;I think the { get, set } is actually useful. Look at this:&lt;/p&gt;
&lt;p&gt;public string Name { get; protected set; }&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;public string Name { virtual get; private set; }&lt;/p&gt;
&lt;p&gt;Of course 'string Name { get; } would we pretty useless :-)&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1605794</link><pubDate>Mon, 05 Feb 2007 21:58:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1605794</guid><dc:creator>Kevin Dente</dc:creator><description>&lt;p&gt;I'm with Thomas on the syntax - both for simplicity and for the nice symmetry with events. &lt;/p&gt;
</description></item><item><title>properties vs. fields... again</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1629885</link><pubDate>Fri, 09 Feb 2007 00:29:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1629885</guid><dc:creator>jaybaz [MS] WebLog</dc:creator><description>&lt;p&gt;Eric Gunnerson just posted Properties vs public fields redux... It's no secret that I agree with Eric&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1639779</link><pubDate>Sat, 10 Feb 2007 05:41:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1639779</guid><dc:creator>damien morton</dc:creator><description>&lt;p&gt;seems like public fields should also be marked internal then?&lt;/p&gt;
</description></item><item><title>Properties vs Public Fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1692273</link><pubDate>Sat, 17 Feb 2007 04:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1692273</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1733734</link><pubDate>Wed, 21 Feb 2007 11:27:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1733734</guid><dc:creator>Fabien Bezagu</dc:creator><description>&lt;p&gt;Of course, I'd prefer not to use trivial properties but I found one reason : reflection. &lt;/p&gt;
&lt;p&gt;In most of the framework databound controls, reflection is used to find the property matching the datamember. &lt;/p&gt;
&lt;p&gt;For that reason, I came back to trivial properties in my domain classes...&lt;/p&gt;
</description></item><item><title>re: Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#1770127</link><pubDate>Tue, 27 Feb 2007 21:33:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1770127</guid><dc:creator>Kent-Remi Gabrielsen</dc:creator><description>&lt;p&gt;I just spent a couple of hours on many different blogs/forums trying to find out when to use fields and when to use properties. And, of course, without getting *The Answer*. But, Fabien's point here is right on the spot! That's exactly why I started to read about this topic(again...), and that's a very important reason to use properties and not fields. Thanks Fabien, now I am able to bind my dataclass to a gridview with two lines of code :)&lt;/p&gt;
</description></item><item><title>Why must property?</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#2747755</link><pubDate>Sun, 20 May 2007 11:50:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2747755</guid><dc:creator>The Pragmatic Programmer</dc:creator><description>&lt;p&gt;Why so many people like property much than public field even though inside the property there is no other&lt;/p&gt;
</description></item><item><title>Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#4451511</link><pubDate>Sat, 18 Aug 2007 23:06:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4451511</guid><dc:creator>Mighell's Blog</dc:creator><description>&lt;p&gt;Properties vs public fields redux...&lt;/p&gt;
</description></item><item><title>Properties vs public fields redux...</title><link>http://blogs.msdn.com/ericgu/archive/2007/02/01/properties-vs-public-fields-redux.aspx#4493486</link><pubDate>Tue, 21 Aug 2007 15:07:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4493486</guid><dc:creator>Mighell's Mobile Blog</dc:creator><description>&lt;p&gt;Di una cosa simile, ne discutevo tempo fa con un paio di amici. L&amp;amp;#39;argomento citato in questo post&lt;/p&gt;
</description></item></channel></rss>