<?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</title>
	<atom:link href="http://www.richardbushnell.net/tag/c-mixins-ruby/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 simple Mix-in with C# 3.0</title>
		<link>http://www.richardbushnell.net/2007/12/25/a-simple-mix-in-with-c-sharp-3_0/</link>
		<comments>http://www.richardbushnell.net/2007/12/25/a-simple-mix-in-with-c-sharp-3_0/#comments</comments>
		<pubDate>Tue, 25 Dec 2007 18:50:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# mixins ruby]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/25/a-simple-mix-in-with-c-30/</guid>
		<description><![CDATA[Heard of mix-ins? They&#8217;re an alternative to multiple inheritance, made popular recently by Ruby. Basically, you can use them to &#8220;mix in&#8221; methods from an interface with their implementations into a class. In Ruby you can do this by including a module in a class. In C#, you do it by implementing an interface and [...]]]></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%2F25%2Fa-simple-mix-in-with-c-sharp-3_0%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>Heard of mix-ins? They&#8217;re an alternative to multiple inheritance, made popular recently by Ruby. </p>
<p>Basically, you can use them to &#8220;mix in&#8221; methods from an interface <strong>with their implementations</strong> into a class.</p>
<p>In Ruby you can do this by including a module in a class. In C#, you do it by implementing an interface and defining an extension method for the interface.</p>
<p>Here&#8217;s a simple example.</p>
<p>First, define the interface. In this case, it won&#8217;t have any special features, so the interface is empty. We&#8217;ll call it IDebug, as it is going to let us call a method to get details of the object it is implemented on.</p>
<pre class="code"><span style="color: blue">public interface </span><span style="color: #2b91af">IDebug
</span>{
}</pre>
<p>After the interface is set, define a static class with an extension method for the interface. We&#8217;ll just define one method here, called &#8220;GetTypeInfo&#8221;.</p>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">DebugExtensions
</span>{
  <span style="color: blue">public static string </span>GetTypeInfo(<span style="color: blue">this </span><span style="color: #2b91af">IDebug </span>debug)
  {
    <span style="color: blue">return </span><span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">"{0} ({1}): {2}"</span>
	, debug.GetType().Name
        , debug.GetHashCode().ToString()
        , debug.ToString());
  }
}</pre>
<p>The method returns the name of the <em>class </em>(not the interface) where the interface is implemented, plus a few extra bits of information.</p>
<p>Now implement a couple of classes which implement the interface. </p>
<p><span style="color: blue">class </span><span style="color: #2b91af">MyClass </span>: <span style="color: #2b91af">IDebug<br /></span>{<br />&nbsp; <span style="color: blue">public override string </span>ToString()<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; <span style="color: blue">return </span><span style="color: #a31515">&#8220;I am an instance of MyClass&#8221;</span>;<br />&nbsp; }<br />}</p>
<p><span style="color: blue">class </span><span style="color: #2b91af">MyOtherClass </span>: <span style="color: #2b91af">IDebug<br /></span>{<br />&nbsp; <span style="color: blue">public override string </span>ToString()<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; <span style="color: blue">return </span><span style="color: #a31515">&#8220;I am an instance of MyOtherClass&#8221;</span>;<br />&nbsp; }<br />}</p>
<p>Now, magically, the method &#8220;GetTypeInfo&#8221; is included with the class as an extension method.</p>
<p>In the method you call this from, you then need add a &#8220;using&#8221; declaration for the namespace of the extension class.</p>
<p>After you&#8217;ve done that you can call the method from the mix-in.</p>
<pre class="code"><span style="color: blue">var </span>myObj = <span style="color: blue">new </span><span style="color: #2b91af">MyClass</span>();
<span style="color: blue">var </span>myObj2 = <span style="color: blue">new </span><span style="color: #2b91af">MyOtherClass</span>();
<span style="color: #2b91af">Console</span>.WriteLine(myObj.GetTypeInfo());
<span style="color: #2b91af">Console</span>.WriteLine(myObj2.GetTypeInfo());</pre>
<p><a href="http://11011.net/software/vspaste"></a>The output of this is:</p>
<p><font face="Courier New" size="2">MyClass (7995840): I am an instance of MyClass<br />MyOtherClass (56251872): I am an instance of MyOtherClass</font></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%2F25%2Fa-simple-mix-in-with-c-sharp-3_0%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/25/a-simple-mix-in-with-c-sharp-3_0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

