<?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# 3.0 extension methods ruby syntax ranges</title>
	<atom:link href="http://www.richardbushnell.net/tag/c-3-0-extension-methods-ruby-syntax-ranges/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>Fun with C# Extension Methods: Easy Ranges</title>
		<link>http://www.richardbushnell.net/2007/12/31/fun-with-c-sharp-extension-methods-easy-ranges/</link>
		<comments>http://www.richardbushnell.net/2007/12/31/fun-with-c-sharp-extension-methods-easy-ranges/#comments</comments>
		<pubDate>Sun, 30 Dec 2007 22:00:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# 3.0 extension methods ruby syntax ranges]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/31/fun-with-c-sharp-extension-methods-easy-ranges/</guid>
		<description><![CDATA[I&#8217;m not a real Ruby on Rails developer, but I&#8217;ve tried to learn it, just to broaden my perspective. Coming from a C# background, I&#8217;m impressed by how easy it is to read Ruby code. In fact, it is usually so compact and self-descriptive, you can understand it just by reading the code. Imagine not [...]]]></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%2F31%2Ffun-with-c-sharp-extension-methods-easy-ranges%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><p>I&#8217;m not a real Ruby on Rails developer, but I&#8217;ve tried to learn it, just to broaden my perspective. Coming from a C# background, I&#8217;m impressed by how easy it is to read Ruby code. In fact, it is usually so compact and self-descriptive, you can understand it just by reading the code. Imagine not having to write comments because your code is so clear! That&#8217;s what you can do with Ruby.</p>
<p>And now, using extension methods, it&#8217;s almost as easy to write C# code which is just as self-descriptive as Ruby code. I&#8217;m going to try to demonstrate that in this post.</p>
<h2></h2>
<p>Before we start, if you want to create your own extension methods, you need to define a static class to put them in:</p>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">Extensions</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I generally make my extension methods public, because I intend them to be usable for all my projects. I&#8217;m putting them all in a common project which I reference in most of my work.</p>
<h2></h2>
<h2>Easy Ranges</h2>
<p>.Net 3.5 includes a new extension method for IEnumerable&lt;int&gt; called Range. Its first parameter is the starting integer, and its second is the number of integers to count. So to get a range from 1 to 10, you write:</p>
<pre class="code"><span style="color: #2b91af">Enumerable</span>.Range(1, 11)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>It might be just me, but personally, I find that syntax a bit counter-intuitive. If I want the range 100 to 200, I&#8217;d have to write:</p>
<pre class="code"><span style="color: #2b91af">Enumerable</span>.Range(100, 101);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Could I read that later, and easily understand what was being done? Not without a comment or two. When I read that code, it&#8217;s not obvious at all what range is being used.</p>
<p>But, let&#8217;s not complain. It&#8217;s easy to implement our own interpretation and extend an integer with a new method. Let&#8217;s make it more Ruby-like.</p>
<p>In Ruby you declare a range from 1 to 10 like this: [1..10]. A range of 100 to 200 looks like this: [100..200]. That&#8217;s nice, but pretty much impossible for us to define for C#. I think it would be just as good though if I could write something like this in my programs:</p>
<p><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; range = 1.To(3);</p>
<p>For me, that compares quite nicely to the Ruby syntax. Plus, I get intellisense for the method on an integer, so it works well.</p>
<p>To implement that, I just have to define a new extension method like so:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; To(<span style="color: blue">this int </span>first, <span style="color: blue">int </span>last)
{
  <span style="color: blue">return </span><span style="color: #2b91af">Enumerable</span>.Range(first, last - first + 1);
}</pre>
<p>Using this new method and the new C# LINQ syntax, I could select all even numbers from 1000 to 2000 like this:</p>
<pre class="code"><span style="color: blue">var </span>odds = <span style="color: blue">from </span>i <span style="color: blue">in </span>1000.To(2000)
           <span style="color: blue">where </span>i % 2 == 0
           <span style="color: blue">select </span>i;

<span style="color: blue">foreach </span>(<span style="color: blue">var </span>i <span style="color: blue">in </span>odds)
  <span style="color: #2b91af">Console</span>.WriteLine(i);</pre>
<p>This gives 501 items, as you&#8217;d expect.</p>
<h2>What&#8217;s next?</h2>
<p>Ruby has a nice little syntax for defining a quick loop. Rails uses it a lot. I&#8217;m going to demonstrate how to create the same syntax in C# in my next post.</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%2F31%2Ffun-with-c-sharp-extension-methods-easy-ranges%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/31/fun-with-c-sharp-extension-methods-easy-ranges/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

