<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.Net Smoothie &#187; C# automatic properties refactoring</title>
	<atom:link href="http://www.richardbushnell.net/tag/c-automatic-properties-refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 11:42:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Refactoring C# Series: Use Automatic Property</title>
		<link>http://www.richardbushnell.net/2007/12/28/refactoring-c-sharp-series-use-automatic-property/</link>
		<comments>http://www.richardbushnell.net/2007/12/28/refactoring-c-sharp-series-use-automatic-property/#comments</comments>
		<pubDate>Thu, 27 Dec 2007 22:00:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# automatic properties refactoring]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/28/refactoring-c-sharp-series-use-automatic-property/</guid>
		<description><![CDATA[Name Use Automatic Property Summary You have a property in a class which just wraps a field of the same type, and simply returns or sets that field. private string _field1; public string Field1 { get { return _field1; } set { _field1 = value; } } Becomes: public string Field1 { get; set; } [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F28%2Frefactoring-c-sharp-series-use-automatic-property%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><h2>Name</h2>
<p>Use Automatic Property</p>
<h2>Summary</h2>
<p>You have a property in a class which just wraps a field of the same type, and simply returns or sets that field.</p>
<pre class="code"><span style="color: blue">private string </span>_field1;
<span style="color: blue">public string </span>Field1
{
  <span style="color: blue">get </span>{ <span style="color: blue">return </span>_field1; }
  <span style="color: blue">set </span>{ _field1 = <span style="color: blue">value</span>; }
}</pre>
<p>Becomes:</p>
<pre class="code"><span style="color: blue">public string </span>Field1 { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<h2>C# Version</h2>
<p>3.0</p>
<h2>Motivation</h2>
<p>Encapsulation is quite possibly <em>the</em> key principle of object-oriented design. It is common practice in C# to encapsulate fields by wrapping them in a property. </p>
<p>When a class has many properties, much of the class is taken up by the same coding pattern for a property:</p>
<pre class="code"><span style="color: blue">private string </span>_field1;
<span style="color: blue">public string </span>Field1
{
  <span style="color: blue">get </span>{ <span style="color: blue">return </span>_field1; }
  <span style="color: blue">set </span>{ _field1 = <span style="color: blue">value</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>In C# 3.0 this block of code can be removed by using automatic properties. The compiler will do the same thing that you would have done before if you just define the property name and the getter and setter.</p>
<pre class="code"><span style="color: blue">public string </span>Field1 { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Reducing the property code like this makes it much easier to understand the code later, as only the necessary details are defined. Even if you use a terse syntax formatting, as in the previous example, you are saving 5 lines of code per property. The way I usually format my code with extra linefeeds, I save 9 lines for each one. When there are a lot of properties in a class, that is a large amount of code which can be removed.</p>
<p>Removing code isn&#8217;t just good for making it easier to understand; it makes it easier to test too. You wouldn&#8217;t have to test properties to see if they are simply setting and getting the correct values with this syntax, as the compiler is doing the work for you. So if you have unit tests, you might be able to remove lots of property testing code.</p>
<p>Of course, if the property does not just simply set or get a field value, you must use the previous C# 1.0 syntax for properties. In addition, you cannot specify get or set alone, but you can include scope identifiers to make a set private, for example:</p>
<pre class="code"><span style="color: green">// The following are not valid:
// Automatically implemented properties must define both get and set accessors
</span><span style="color: blue">public string </span>GetOnly { <span style="color: blue">get</span>; } <span style="color: green">// Not valid
</span><span style="color: blue">public string </span>SetOnly { <span style="color: blue">set</span>; } <span style="color: green">// Not valid either

// But this is okay
</span><span style="color: blue">public string </span>GetWithPrivateSet { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2><a href="http://11011.net/software/vspaste"></a>Mechanics</h2>
<ul>
<li>Remove the body of the property, replacing the code between the get and set blocks with a semicolon &#8220;;&#8221; .
<li>Compile.
<li>If there is a compiler warning that the field is no longer being used, you can now simply remove the field which the property was wrapping.
<li>If there is no compiler warning, other methods or properties are using the field directly. You can remove the field and recompile anyway, which will give you the location in the code where the field is being used. Replace each reference with a reference to the property instead.
<li>Compile. </li>
</ul>
<p>Repeat for each property.</p>
<p>If the field was a protected field, not private, then you might have subclasses which access the field. They will have to be changed to access the property instead of directly accessing the field.</p>
<p><strong>Tip</strong>: For new code, you can simply use the code snippet &#8220;prop&#8221; in Visual Studio 2008 to get an automatic property.</p>
<h2>Examples</h2>
<p>I&#8217;ll just do an example with one property, even though it looks a lot better with a longer class.</p>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Account
</span>{
  <span style="color: blue">private long </span>_id;
  <span style="color: blue">public long </span>ID
  {
    <span style="color: blue">get </span>{ <span style="color: blue">return </span>_id; }
    <span style="color: blue">set </span>{ _id = <span style="color: blue">value</span>; }
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>First, remove the body of the property getter and setter (and apply a bit of reformatting too, if you like):</p>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Account
</span>{
  <span style="color: blue">private long </span>_id;
  <span style="color: blue">public long </span>ID { <span style="color: blue">get </span>; <span style="color: blue">set </span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Compile, to make sure the code is happy, then remove the field:</p>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Account
</span>{
  <span style="color: blue">public long </span>ID { <span style="color: blue">get </span>; <span style="color: blue">set </span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F28%2Frefactoring-c-sharp-series-use-automatic-property%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/28/refactoring-c-sharp-series-use-automatic-property/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

