<?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>porges &#187; csharp</title>
	<atom:link href="http://porg.es/blog/tag/csharp/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description>... master of none</description>
	<lastBuildDate>Sat, 12 Sep 2009 07:57:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Things I&#8217;d like to see in C#: Conditional interface implementation</title>
		<link>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation</link>
		<comments>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation#comments</comments>
		<pubDate>Sat, 20 Sep 2008 02:54:29 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[idea]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=171</guid>
		<description><![CDATA[To explain this, imagine you are designing a type Wrapper&#60;T&#62;, which is just a simple wrapper around a type T. class Wrapper&#60;T&#62; &#123; T value; public Wrapper&#40;T theObject&#41; &#123; value = theObject; &#125; // Some other methods... &#125; Now, since this is a just a wrapper around some type T, we would like to implement [...]]]></description>
			<content:encoded><![CDATA[<p>To explain this, imagine you are designing a type <code>Wrapper&lt;T&gt;</code>, which is just a simple wrapper around a type <code>T</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    T value<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> Wrapper<span style="color: #000000;">&#40;</span>T theObject<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        value <span style="color: #008000;">=</span> theObject<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">// Some other methods...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now, since this is a just a wrapper around some type <code>T</code>, we would like to implement some simple interfaces around this object. For example, if <code>T</code> is comparable, we would like the wrapper class to implement <code>IComparable&lt;Wrapper&lt;T&gt;&gt;</code>. However this is not possible with C#. In Haskell we would have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Disposable a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Disposable <span style="color: green;">&#40;</span>Wrapper a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span> <span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>This says &#8220;if <code>a</code> is a type that is an instance of <code>Disposable</code>, then the type <code>Wrapper a</code> is also an instance of <code>Disposable</code>”. In C#, I&#8217;d like to see something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">:</span> IComparable<span style="color: #008000;">&lt;</span>Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span>
        when T <span style="color: #008000;">:</span> IComparable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008080; font-style: italic;">//this is the syntax extension</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And then in any methods that the interface specifies (in this case <code>int Compare(Wrapper&lt;T&gt; other)</code>), it is assumed that for any object of type <code>T</code> you have access to all the methods that <code>T</code> has when it implements <code>IComparable&lt;T&gt;</code>.</p>
<p>At the moment the best you can do is to <em>always</em> implement <code>IComparable&lt;Wrapper&lt;T&gt;&gt;</code> and just throw a runtime exception when <code>T</code> doesn&#8217;t implement <code>IComparable&lt;T&gt;</code>, which isn&#8217;t very nice.</p>
<p>A simple idea, but it would add great power to C#.</p>
<p>After some (very quick) research I haven&#8217;t found anything that suggests anyone else has attempted to get this into C#, but there is <a href="http://homepages.cwi.nl/~ralf/JavaGI/paper.pdf">JavaGI</a> (see section 2.4 for the equivalent of what this post talks about) where the authors have extended Java to deal with what they term ‘generalized interfaces’—a concept which alters Java (rather radically) to make interface implementations similar to Haskell&#8217;s typeclasses.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing IEnumerable easily</title>
		<link>http://porg.es/blog/implementing-ienumerable-easily</link>
		<comments>http://porg.es/blog/implementing-ienumerable-easily#comments</comments>
		<pubDate>Fri, 04 Apr 2008 09:43:02 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[IEnumerable]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=125</guid>
		<description><![CDATA[Say that you’re implementing a linked list, and you want an enumerator: public IEnumerator&#60;T&#62; GetEnumerator&#40;&#41; &#123; return new Stream&#60;T,Node&#62;&#40;first, node =&#62; node.next == null ? null : Tuple.Of&#40;node.next, node.datum&#41;.AsNullable&#40;&#41;&#41;; &#125; This uses the following utility class to implement the enumerator in one line (along with some code for Tuples and an extension method for structs): [...]]]></description>
			<content:encoded><![CDATA[<p>Say that you’re implementing a linked list, and you want an enumerator:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Stream<span style="color: #008000;">&lt;</span>T,Node<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>first,
        node <span style="color: #008000;">=&gt;</span> node.<span style="color: #0000FF;">next</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">?</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">:</span> Tuple.<span style="color: #0000FF;">Of</span><span style="color: #000000;">&#40;</span>node.<span style="color: #0000FF;">next</span>, node.<span style="color: #0000FF;">datum</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AsNullable</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This uses the following utility class to implement the enumerator in one line (along with some code for Tuples and an extension method for structs):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Stream<span style="color: #008000;">&lt;</span>Tdata, Tstate<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IEnumerator<span style="color: #008000;">&lt;</span>Tdata<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> Tdata current<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> Tstate initialState<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> Tstate state<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> Func<span style="color: #008000;">&lt;</span>Tstate, Pair<span style="color: #008000;">&lt;</span>Tstate, Tdata<span style="color: #008000;">&gt;?&gt;</span> moveNext<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Stream<span style="color: #000000;">&#40;</span>Tstate initialState, Func<span style="color: #008000;">&lt;</span>Tstate, Pair<span style="color: #008000;">&lt;</span>Tstate, Tdata<span style="color: #008000;">&gt;?&gt;</span> calcNextValue<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        moveNext <span style="color: #008000;">=</span> calcNextValue<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">initialState</span> <span style="color: #008000;">=</span> initialState<span style="color: #008000;">;</span>
        Reset<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#region IEnumerator&lt;Tdata&gt; Members</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Tdata Current
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> current<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region IDisposable Members</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        GC.<span style="color: #0000FF;">SuppressFinalize</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> disposing<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposing<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var disposeCurrent <span style="color: #008000;">=</span> current <span style="color: #0600FF;">as</span> IDisposable<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposeCurrent <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                disposeCurrent.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var disposeState <span style="color: #008000;">=</span> state <span style="color: #0600FF;">as</span> IDisposable<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposeState <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                disposeState.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">//safe; have checked already above.</span>
                <span style="color: #008080; font-style: italic;">//type of state == type of initialstate</span>
                <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IDisposable<span style="color: #000000;">&#41;</span>initialState<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region IEnumerator Members</span>
&nbsp;
    <span style="color: #FF0000;">object</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span>.<span style="color: #0000FF;">IEnumerator</span>.<span style="color: #0000FF;">Current</span>
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> current<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> MoveNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var result <span style="color: #008000;">=</span> moveNext<span style="color: #000000;">&#40;</span>state<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>result.<span style="color: #0000FF;">HasValue</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            current <span style="color: #008000;">=</span> result.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Right</span><span style="color: #008000;">;</span>
            state <span style="color: #008000;">=</span> result.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Left</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">else</span>
            <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Reset<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        state <span style="color: #008000;">=</span> initialState<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/implementing-ienumerable-easily/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>object.Equals handles null values correctly</title>
		<link>http://porg.es/blog/objectequals-handles-null-values-correctly</link>
		<comments>http://porg.es/blog/objectequals-handles-null-values-correctly#comments</comments>
		<pubDate>Thu, 27 Mar 2008 20:58:12 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://porg.es/blog/objectequals-handles-null-values-correctly</guid>
		<description><![CDATA[Here’s the source code, as disassembled by Reflector: public static bool Equals&#40;object objA, object objB&#41; &#123; return &#40;&#40;objA == objB&#41; &#124;&#124; &#40;&#40;&#40;objA != null&#41; &#38;&#38; &#40;objB != null&#41;&#41; &#38;&#38; objA.Equals&#40;objB&#41;&#41;&#41;; &#125; It seems that not even Microsoft knows this! I spotted this code, from ASP.NET’s MVC implementation, on Scott Hanselman’s blog: return &#40;other != null&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Here’s the source code, as disassembled by Reflector:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> Equals<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> objA, <span style="color: #FF0000;">object</span> objB<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>objA <span style="color: #008000;">==</span> objB<span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>objA <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span>objB <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> objA.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>objB<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It seems that not even Microsoft knows this! I spotted this code, from ASP.NET’s MVC implementation, on <a href="http://www.hanselman.com/blog/TheWeeklySourceCode21ASPNETMVCPreview2SourceCode.aspx">Scott Hanselman’s blog</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>other <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>other._first <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span>_first <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span>
    <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>other._first <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> other._first.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>_first<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>other._second <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span>_second <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span>
    <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>other._second <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> other._second.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>_second<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This can be rewritten as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>other <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #FF0000;">object</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>other._first, _first<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #FF0000;">object</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>other._second, _second<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Much nicer!</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/objectequals-handles-null-values-correctly/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
