<?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; .net</title>
	<atom:link href="http://porg.es/blog/tag/net/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Sun, 06 May 2012 22:13:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Objects can be collected while their instance methods are still executing</title>
		<link>http://porg.es/blog/objects-can-be-collected-while-their-instance-methods-are-still-executing</link>
		<comments>http://porg.es/blog/objects-can-be-collected-while-their-instance-methods-are-still-executing#comments</comments>
		<pubDate>Wed, 02 May 2012 23:42:56 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=720</guid>
		<description><![CDATA[In Peter Ritchie&#8217;s post Dispose Pattern and “Set large fields to null”, he states the following (my highlighting): At face value, setting a field to null means that the referenced object is now unrooted from the class that owns the field and, if that was the last root of that reference, the Garbage Collector (GC) [...]]]></description>
			<content:encoded><![CDATA[<p>In Peter Ritchie&#8217;s post <a href="http://msmvps.com/blogs/peterritchie/archive/2012/04/26/dispose-pattern-and-set-large-fields-to-null.aspx"><i>Dispose Pattern and “Set large fields to null”</i></a>, he states the following (my highlighting):</p>
<blockquote><p>At face value, setting a field to null means that the referenced object is now unrooted from the class that owns the field and, if that was the last root of that reference, the Garbage Collector (GC) is now free to release the memory used by the object that was referenced by that field.  Although advanced, this seems all very academic because <span style="background:yellow">the amount of time between unrooting the reference and the return from <code>Dispose</code> (and thus the unrooting of the parent object) would seem like a very short amount of time</span>.  Even if the amount of time between these two actions is small, setting a single field to null (i.e. a single assignment) seems like such a minor bit of code to provide no adverse affects.  The prevalent opinion seems to be that the GC “handles” this case and does what is best for you without setting the field to null.</p></blockquote>
<p>In fact, this &#8220;short amount of time&#8221; can be so short as to be negative! A class that nothing else refers to is unrooted as soon as it no longer refers to itself, so objects in .NET can be collected while their instance methods are still executing. I first learned about this from <a href="http://blogs.msdn.com/b/cbrumme/archive/2003/04/19/51365.aspx">Chris Brumme&#8217;s blog</a>. Here is an example that shows this behaviour:</p>
<p><script src="https://gist.github.com/2581933.js"> </script></p>
<p>The output is:</p>
<pre>start method
collected
end method</pre>
<p>Once the method <code>Go()</code> has no references to <code>this</code>, it is eligible for collection. (What this means is that setting fields to <code>null</code> will actually <em>extend</em> the lifetime of the parent object ever-so-slightly.)</p>
<p>However, the code that a <code>using</code> is transformed into will hold onto a reference to the object while its <code>Dispose()</code> method is called, so you won&#8217;t get the same behaviour with the following code:</p>
<p><script src="https://gist.github.com/2581936.js"> </script></p>
<p>The output is:</p>
<pre>start method
end method
collected</pre>
<p>This is because the <code>using</code> statement is transformed to something like this:</p>
<p><script src="https://gist.github.com/2582061.js"> </script></p>
<p>I <em>think</em> (but cannot confirm) that the reference being held is that of <code>Slow</code> in the outer scope. We can restore the previous behaviour if the <code>using</code> transform was written as this instead:</p>
<p><script src="https://gist.github.com/2582077.js"> </script> </p>
<p>Either way, I wouldn&#8217;t bother setting fields to <code>null</code> in the <code>Dispose()</code> method. (For one thing, these fields can no longer be declared <code>readonly</code>.) And most of the time, the GC is smarter than you are.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/objects-can-be-collected-while-their-instance-methods-are-still-executing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Casting in .NET via object mutation</title>
		<link>http://porg.es/blog/casting-in-dot-net-via-object-mutation</link>
		<comments>http://porg.es/blog/casting-in-dot-net-via-object-mutation#comments</comments>
		<pubDate>Fri, 13 May 2011 10:37:03 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[horrid]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=624</guid>
		<description><![CDATA[In this post, we will see how to make the following code fail: object it = new SomeStruct &#123; Item = 1 &#125;; &#160; Floatsy&#40;it&#41;; &#160; Console.WriteLine&#40;&#40;&#40;SomeStruct&#41;it&#41;.Item&#41;; At runtime, it will throw an InvalidCastException! Here&#8217;s how. In .NET, each object has also associated with it a value which determines the type of the object. In [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we will see how to make the following code fail:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	<span style="color: #6666cc; font-weight: bold;">object</span> it <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SomeStruct <span style="color: #008000;">&#123;</span> Item <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	Floatsy<span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>SomeStruct<span style="color: #008000;">&#41;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>At runtime, it will throw an <code>InvalidCastException</code>!</p>
<p><span id="more-624"></span></p>
<p>Here&#8217;s how. In .NET, each object has also associated with it a value which determines the type of the object. In memory, this is stored before the object&#8217;s data, like so (I got this information from the MSDN article <a href="http://msdn.microsoft.com/en-us/magazine/cc163791.aspx">Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects</a>):</p>
<p><center><a href="http://porg.es/blog/wp-content/uploads/2011/05/layout2.png"><img src="http://porg.es/blog/wp-content/uploads/2011/05/layout2.png" alt="" title="layout" width="191" height="176" class="aligncenter size-full wp-image-634" /></a></center></p>
<p>Now, if we can write to that, we can set the type of the object to whatever we want!</p>
<p>There&#8217;s one small problem &mdash; in .NET we can&#8217;t take the address of a managed object (which we need in order to write to the object in memory). There are various reasons for this, one of them being that the garbage collector likes to be able to move objects around. Being able to take arbitrary pointers of objects would mean that the pointers could become invalidated.</p>
<p>What we <em>can</em> do is to pin a struct, which allows us to retrieve the address of it (this facility exists so that users can pass pinned managed structs into unmanaged code as pointers). Here&#8217;s how to pin a struct:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    var handle <span style="color: #008000;">=</span> GCHandle<span style="color: #008000;">.</span><span style="color: #0000FF;">Alloc</span><span style="color: #008000;">&#40;</span>o, GCHandleType<span style="color: #008000;">.</span><span style="color: #0000FF;">Pinned</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var addr <span style="color: #008000;">=</span> handle<span style="color: #008000;">.</span><span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>We can then write to the type handle like so (both the sync block and type handle are 32 bits):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008000;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>addr<span style="color: #008000;">&#41;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=</span> someValue<span style="color: #008000;">;</span></pre></div></div>

<p>Another problem is that all of this is only true for values on the heap. As far as I can tell, the static type of the variable is what .NET uses to identify values on the stack. So in order for this to work, we must use a boxed copy of a struct.</p>
<hr/>
<p>Finally, here&#8217;s some demo code, showing runtime changing of types for both a primitive and custom struct:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">unsafe</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">struct</span> SomeStruct <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Item<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">object</span> notAFloat <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">object</span> me <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SomeStruct <span style="color: #008000;">&#123;</span>Item <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0} ({1})&quot;</span>, notAFloat, notAFloat<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Floatify<span style="color: #008000;">&#40;</span>notAFloat<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0} ({1})&quot;</span>, notAFloat, notAFloat<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>me<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Floatify<span style="color: #008000;">&#40;</span>me<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>me<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Floatify<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T o<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var handle <span style="color: #008000;">=</span> GCHandle<span style="color: #008000;">.</span><span style="color: #0000FF;">Alloc</span><span style="color: #008000;">&#40;</span>o, GCHandleType<span style="color: #008000;">.</span><span style="color: #0000FF;">Pinned</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var addr <span style="color: #008000;">=</span> handle<span style="color: #008000;">.</span><span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">object</span> f <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #FF0000;">1.0</span><span style="color: #008000;">;</span>
        var handle2 <span style="color: #008000;">=</span> GCHandle<span style="color: #008000;">.</span><span style="color: #0000FF;">Alloc</span><span style="color: #008000;">&#40;</span>f, GCHandleType<span style="color: #008000;">.</span><span style="color: #0000FF;">Pinned</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var addr2 <span style="color: #008000;">=</span> handle2<span style="color: #008000;">.</span><span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// copy type handle of a float to the object</span>
        <span style="color: #008000;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>addr<span style="color: #008000;">&#41;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>addr2<span style="color: #008000;">&#41;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        handle2<span style="color: #008000;">.</span><span style="color: #0000FF;">Free</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        handle<span style="color: #008000;">.</span><span style="color: #0000FF;">Free</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The output is:</p>
<p><center>
<pre>1 (System.Int32)
1.401298E-45 (System.Single)

Program+SomeStruct
1.401298E-45</pre>
<p></center></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/casting-in-dot-net-via-object-mutation/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[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; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">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: #008000;">&#40;</span>first,
        node <span style="color: #008000;">=&gt;</span> node<span style="color: #008000;">.</span><span style="color: #0000FF;">next</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">?</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">:</span> Tuple<span style="color: #008000;">.</span><span style="color: #0000FF;">Of</span><span style="color: #008000;">&#40;</span>node<span style="color: #008000;">.</span><span style="color: #0000FF;">next</span>, node<span style="color: #008000;">.</span><span style="color: #0000FF;">datum</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNullable</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#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; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">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: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Tdata current<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Tstate initialState<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Tstate state<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">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; font-weight: bold;">public</span> Stream<span style="color: #008000;">&#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: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        moveNext <span style="color: #008000;">=</span> calcNextValue<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">initialState</span> <span style="color: #008000;">=</span> initialState<span style="color: #008000;">;</span>
        Reset<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#region IEnumerator&lt;Tdata&gt; Members</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Tdata Current
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> current<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region IDisposable Members</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Dispose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        GC<span style="color: #008000;">.</span><span style="color: #0000FF;">SuppressFinalize</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> disposing<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>disposing<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var disposeCurrent <span style="color: #008000;">=</span> current <span style="color: #0600FF; font-weight: bold;">as</span> IDisposable<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>disposeCurrent <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                disposeCurrent<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var disposeState <span style="color: #008000;">=</span> state <span style="color: #0600FF; font-weight: bold;">as</span> IDisposable<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>disposeState <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                disposeState<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#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: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>IDisposable<span style="color: #008000;">&#41;</span>initialState<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region IEnumerator Members</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">object</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">IEnumerator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span>
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> current<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveNext<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var result <span style="color: #008000;">=</span> moveNext<span style="color: #008000;">&#40;</span>state<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>result<span style="color: #008000;">.</span><span style="color: #0000FF;">HasValue</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            current <span style="color: #008000;">=</span> result<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Right</span><span style="color: #008000;">;</span>
            state <span style="color: #008000;">=</span> result<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Reset<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        state <span style="color: #008000;">=</span> initialState<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#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[C#]]></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; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Equals<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> objA, <span style="color: #6666cc; font-weight: bold;">object</span> objB<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>objA <span style="color: #008000;">==</span> objB<span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>objA <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>objB <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> objA<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>objB<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#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; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>other <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_first <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>_first <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span>
    <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_first <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> other<span style="color: #008000;">.</span>_first<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>_first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_second <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>_second <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span>
    <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_second <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> other<span style="color: #008000;">.</span>_second<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>_second<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#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; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>other <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_first, _first<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_second, _second<span style="color: #008000;">&#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>
		<item>
		<title>A simple BigNum library for .NET</title>
		<link>http://porg.es/blog/a-simple-bignum-library-for-dot-net</link>
		<comments>http://porg.es/blog/a-simple-bignum-library-for-dot-net#comments</comments>
		<pubDate>Sat, 13 Oct 2007 08:05:51 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://porg.es/blog/a-simple-bignum-library-for-net</guid>
		<description><![CDATA[Update Due to minor demand, the code is also available: BigNum source. Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At [...]]]></description>
			<content:encoded><![CDATA[<h4>Update</h4>
<p>Due to minor demand, the code is also available: <a href='http://porg.es/blog/wp-content/uploads/2008/04/bignum.zip'>BigNum source</a>.</p>
<p>Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At the moment every time you add, subtract, etc, a completely new BigInt is returned. To make it faster you’d want to just update the number in-place.</p>
<h4>Original content&#8230;</h4>
<p>I’ve created a simple wrapper for <a href="http://www.gmplib.org">GMP</a>. At the moment only BigInts are implemented, but I expect to have BigRationals and BigFloats coming along soon enough. (<i>Edit: this never happened.</i>)</p>
<h4>Usage</h4>
<p>I think this is very close to as-you’d-expect. The only minor thing that might come as a surprise is that BigInts aren’t value types; since they need destructors, they can’t be. The rest is fairly straightforward:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    BigInt n<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">999</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        n <span style="color: #008000;">=</span> BigInt<span style="color: #008000;">.</span><span style="color: #0000FF;">Factorial</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This code executes with sub 5-second times, which I’m pretty pleased with. I’m not sure what kind of performance decrease you get through using GMP under managed code.</p>
<h4>Caveats</h4>
<ul>
<li>Consider this alpha software <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></li>
<li>The included GMP DLL is compiled with none of the magical assembly they provide. If you want to, you can compile it yourself with all this enabled, and you should be able to just use it as a drop-in replacement.</li>
</ul>
<h4>Download</h4>
<p>Available here: <a href='http://porg.es/blog/wp-content/uploads/2007/10/bignum.zip' title='BigNum library'>BigNum library</a>. Documentation <a href="http://porg.es/Help/Index.html">is available</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/a-simple-bignum-library-for-dot-net/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

