<?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# mixins Ruby extension methods multiple inheritance</title>
	<atom:link href="http://www.richardbushnell.net/tag/c-mixins-ruby-extension-methods-multiple-inheritance/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>A Mixin for IComparable&lt;T&gt;</title>
		<link>http://www.richardbushnell.net/2007/12/27/a-mixin-for-icomparablet/</link>
		<comments>http://www.richardbushnell.net/2007/12/27/a-mixin-for-icomparablet/#comments</comments>
		<pubDate>Thu, 27 Dec 2007 08:03:15 +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# mixins Ruby extension methods multiple inheritance]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/27/a-mixin-for-icomparablet/</guid>
		<description><![CDATA[Following on from my other posts on C# Mixins, here&#8217;s a short one to demonstrate the benefits of Mixins using IComparable&#60;T&#62;. I don&#8217;t know about you, but I can never remember how the CompareTo method of IComparable&#60;T&#62; works. If I remember correctly, it gives back -1 if the value of the compared object is less [...]]]></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%2F27%2Fa-mixin-for-icomparablet%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>Following on from my other posts on C# Mixins, here&#8217;s a short one to demonstrate the benefits of Mixins using IComparable&lt;T&gt;.</p>
<p>I don&#8217;t know about you, but I can never remember how the CompareTo method of IComparable&lt;T&gt; works. If I remember correctly, it gives back -1 if the value of the compared object is less than the value of the called object, and +1 if the compared object is greater than the value of the called object.</p>
<p>No, wait! That&#8217;s the wrong way round! See what I mean? </p>
<p>The CompareTo method is defined like this:</p>
<pre class="code"><span style="color: blue">int </span>CompareTo(T other)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>According to the MSDN Library, the value it returns is:</p>
<table cellspacing="0" cellpadding="2" width="409" border="0">
<tbody>
<tr>
<td valign="top" width="149"><strong>Value</strong></td>
<td valign="top" width="258"><strong>Meaning</strong></td>
</tr>
<tr>
<td valign="top" width="150">Less than zero</td>
<td valign="top" width="258">This object is less than the <em>other</em> parameter.</td>
</tr>
<tr>
<td valign="top" width="151">Zero</td>
<td valign="top" width="258">This object is equal to <em>other</em>.</td>
</tr>
<tr>
<td valign="top" width="151">Greater than zero</td>
<td valign="top" width="258">This object is greater than <em>other</em>.</td>
</tr>
</tbody>
</table>
<p>Now I don&#8217;t know about you, but I always have trouble with that. Mix-ins to the rescue!</p>
<p>What I really need is to define my own methods on the IComparable&lt;T&gt; interface. Something like LessThan, MoreThan and ValueEquals. That would be much more valuable. I <em>could</em> define those methods in a superclass, and have my new classes all inherit from that superclass. But that would bind me to a certain structure, reduce the coherency of my classes, and make me feel bad. But if I implement IComparable&lt;T&gt; with its sole method, I can use a Mixin to take advantage of that method and add the new functionality I need, without affecting the structure of my code.</p>
<p>Here&#8217;s an example using the Temperature class shamelessly lifted from the MSDN Library.</p>
<p>The class Temperature is defined as:</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">Temperature </span>: <span style="color: #2b91af">IComparable</span>&lt;<span style="color: #2b91af">Temperature</span>&gt;
{
  <span style="color: green">// Implement the CompareTo method. For the parameter type, Use
  // the type specified for the type parameter of the generic
  // IComparable interface.
  //
  </span><span style="color: blue">public int </span>CompareTo(<span style="color: #2b91af">Temperature </span>other)
  {
    <span style="color: green">// The temperature comparison depends on the comparison of the
    // the underlying Double values. Because the CompareTo method is
    // strongly typed, it is not necessary to test for the correct
    // object type.
    </span><span style="color: blue">return </span>m_value.CompareTo(other.m_value);
  }

  <span style="color: green">// The underlying temperature value.
  </span><span style="color: blue">protected double </span>m_value = 0.0;

  <span style="color: blue">public </span>Temperature(<span style="color: blue">double </span>degreesKelvin)
  {
    <span style="color: blue">this</span>.Kelvin = degreesKelvin;
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Note that Temperature implements <span style="color: #2b91af">IComparable</span>&lt;<span style="color: #2b91af">Temperature</span>&gt;. </p>
<p>So now if you define a static extension class for IComparable&lt;T&gt;, you should also be able to use it with IComparable&lt;Temperature&gt;.</p>
<p>Here are the extension methods I&#8217;m going to add &#8211; LessThan, MoreThan, and ValueEquals:</p>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">IComparableExtensions
</span>{
  <span style="color: blue">public static bool </span>LessThan&lt;T&gt;(<span style="color: blue">this </span><span style="color: #2b91af">IComparable</span>&lt;T&gt; comparable, T other)
  {
    <span style="color: blue">return </span>comparable.CompareTo(other) &lt; 0;
  }

  <span style="color: blue">public static bool </span>MoreThan&lt;T&gt;(<span style="color: blue">this </span><span style="color: #2b91af">IComparable</span>&lt;T&gt; comparable, T other)
  {
    <span style="color: blue">return </span>comparable.CompareTo(other) &gt; 0;
  }

  <span style="color: blue">public static bool </span>ValueEquals&lt;T&gt;(<span style="color: blue">this </span><span style="color: #2b91af">IComparable</span>&lt;T&gt; comparable, T other)
  {
    <span style="color: blue">return </span>comparable.CompareTo(other) == 0;
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>My program now references the IComparableExtensions class by importing its namespace. I then write:</p>
<pre class="code"><span style="color: blue">var </span>t1 = <span style="color: blue">new </span><span style="color: #2b91af">Temperature</span>(273);
<span style="color: blue">var </span>t2 = <span style="color: blue">new </span><span style="color: #2b91af">Temperature</span>(100);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>All I had to do was implement IComparable&lt;T&gt; with its one CompareTo method (which was trivial), and I automatically get the extension methods LessThan, MoreThan and ValueEquals mixed-in to my Temperature class. </p>
<pre class="code"><span style="color: blue">if </span>(t1.LessThan(t2))
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"t1 is less than t2"</span>);
<span style="color: blue">else if </span>(t1.MoreThan(t2))
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"t1 is more than t2"</span>);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And this doesn&#8217;t just work with IComparable&lt;Temperature&gt;, it works with anything which implements IComparable&lt;T&gt;. Classes implementing IComparable&lt;int&gt; would also get access to the new methods, for example.</p>
<p>Here&#8217;s the output of the code:</p>
<p><font face="Courier New">t1 is more than t2</font></p>
<p>&#8230; as you would expect.</p>
<p>And voila! That&#8217;s why mix-ins can be so useful.</p>
<p>I&#8217;ve intentionally done this example using a well-known interface, to make it easier to understand. But imagine what you could do by inheriting from a particular class of your own, defining your own interface, and then adding in more functionality using a mixin. You&#8217;ve nearly got multiple inheritance.</p>
<p>More to come on this&#8230;</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%2F27%2Fa-mixin-for-icomparablet%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/27/a-mixin-for-icomparablet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

