<?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>out &#62;&#62; m_Conscientia; &#187; C#</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Sat, 27 Aug 2011 19:16:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>fatal error C1859</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2011/08/fatal-error-c1859/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2011/08/fatal-error-c1859/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 22:14:24 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[precompiled header]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=1647</guid>
		<description><![CDATA[Once, a friend of mine set the welcome message on his Nokia 5110 to &#8216;fatal error&#8217;. This totally baffled the salesmen in Carphone Warehouse. I was equally baffled when I returned to an old C++ project and tried to compile it in a freshly installed Visual Studio 2008 SP1. I was greeted with a series [...]]]></description>
			<content:encoded><![CDATA[<p>Once, a friend of mine set the welcome message on his <a href="http://en.wikipedia.org/wiki/Nokia_5110">Nokia 5110</a> to &#8216;fatal error&#8217;. This totally baffled the salesmen in Carphone Warehouse.</p>
<p>I was equally baffled when I returned to an old C++ project and tried to compile it in a freshly installed Visual Studio 2008 SP1. I was greeted with a series of these errors:</p>
<blockquote><p>
fatal error C1859: &#8216;Debug\blah.pch&#8217; unexpected precompiled header error, simply rerunning the compiler might fix this problem
</p></blockquote>
<p>Not surprisingly, simply rerunning the compiler did not fix the problem. In fact, this is caused by address space layout randomization on Windows7/Server2008, as noted <a href="http://blogs.msdn.com/b/vcblog/archive/2009/11/12/visual-c-precompiled-header-errors-on-windows-7.aspx">here</a>.</p>
<p>The hotfix is available here: <a href="http://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=25785">Fix for Visual C++ 2008 SP1 compiler error C1859</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2011/08/fatal-error-c1859/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extension method to compute standard deviation of IEnumerable&lt;TimeSpan&gt;</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2011/06/extension-method-to-compute-standard-deviation-of-ienumerabletimespan/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2011/06/extension-method-to-compute-standard-deviation-of-ienumerabletimespan/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 18:24:27 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[extension method]]></category>
		<category><![CDATA[ienumerable]]></category>
		<category><![CDATA[TimeSpan]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=1591</guid>
		<description><![CDATA[Recently, I have been writing some quick and dirty performance tests. I repeat each test scenario a few times and take an average of the execution time. In order to verify that I have a reasonably stable measurement, I wanted to also display the coefficient of variation, which is defined as the ratio of the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I have been writing some <a href="http://blogs.msdn.com/b/ricom/archive/2010/04/26/a-few-words-about-micro-benchmarks.aspx">quick and dirty performance tests</a>. I repeat each test scenario a few times and take an average of the execution time. In order to verify that I have a reasonably stable measurement, I wanted to also display the <a href="http://en.wikipedia.org/wiki/Coefficient_of_variation">coefficient of variation</a>, which is defined as the ratio of the <a href="http://en.wikipedia.org/wiki/Standard_deviation">standard deviation</a> to the mean.</p>
<p>Given a set of TimeSpan values, I want to get the standard deviation as a TimeSpan. I implemented this based on the extension method I found <a href="http://stackoverflow.com/questions/2253874/linq-equivalent-for-standard-deviation">here</a> on StackOverflow. I implemented both sample (with <a href="http://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel&#8217;s correction</a>) and population standard deviation.</p>

<div class="wp_codebox"><table><tr id="p15913"><td class="code" id="p1591code3"><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;">class</span> StandardDeviationExtensions
<span style="color: #008000;">&#123;</span>
    <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;">double</span> SampleStandardDeviation<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span> values<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> count <span style="color: #008000;">=</span> values<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><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>count <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> average <span style="color: #008000;">=</span> values<span style="color: #008000;">.</span><span style="color: #0000FF;">Average</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> sumOfSquaredDifferences <span style="color: #008000;">=</span> values<span style="color: #008000;">.</span><span style="color: #0000FF;">Sum</span><span style="color: #008000;">&#40;</span>v <span style="color: #008000;">=&gt;</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>v <span style="color: #008000;">-</span> average, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>sumOfSquaredDifferences <span style="color: #008000;">/</span> <span style="color: #008000;">&#40;</span>count <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</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;">return</span> <span style="color: #FF0000;">0.0</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: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> PopulationStandardDeviation<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span> values<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>values<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> average <span style="color: #008000;">=</span> values<span style="color: #008000;">.</span><span style="color: #0000FF;">Average</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>values<span style="color: #008000;">.</span><span style="color: #0000FF;">Average</span><span style="color: #008000;">&#40;</span>v <span style="color: #008000;">=&gt;</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>v <span style="color: #008000;">-</span> average, <span style="color: #FF0000;">2</span><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>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">0.0</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: #0600FF; font-weight: bold;">static</span> TimeSpan SampleStandardDeviation<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TimeSpan<span style="color: #008000;">&gt;</span> values<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #008000;">&#40;</span>
            <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">long</span><span style="color: #008000;">&#41;</span>values
                <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>v <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>v<span style="color: #008000;">.</span><span style="color: #0000FF;">Ticks</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">SampleStandardDeviation</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>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> TimeSpan PopulationStandardDeviation<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TimeSpan<span style="color: #008000;">&gt;</span> values<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #008000;">&#40;</span>
            <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">long</span><span style="color: #008000;">&#41;</span>values
                <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>v <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>v<span style="color: #008000;">.</span><span style="color: #0000FF;">Ticks</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">PopulationStandardDeviation</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>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And here are the tests:</p>

<div class="wp_codebox"><table><tr id="p15914"><td class="code" id="p1591code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestClass<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> StandardDeviationTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> EmptySet <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> SingleValue <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">1.0</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> SameValues <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">5.0</span>, <span style="color: #FF0000;">5.0</span>, <span style="color: #FF0000;">5.0</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ScenarioValues <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">2.0</span>, <span style="color: #FF0000;">4.0</span>, <span style="color: #FF0000;">4.0</span>, <span style="color: #FF0000;">4.0</span>, <span style="color: #FF0000;">5.0</span>, <span style="color: #FF0000;">5.0</span>, <span style="color: #FF0000;">7.0</span>, <span style="color: #FF0000;">9.0</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">double</span> ScenarioValuesSampleDeviation <span style="color: #008000;">=</span> <span style="color: #FF0000;">2.13809</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">double</span> ScenarioValuesPopulationDeviation <span style="color: #008000;">=</span> <span style="color: #FF0000;">2.0</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SampleStandardDeviation_EmptySet_HasZeroDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.0</span>, EmptySet<span style="color: #008000;">.</span><span style="color: #0000FF;">SampleStandardDeviation</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>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SampleStandardDeviation_SingleValue_HasZeroDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.0</span>, SingleValue<span style="color: #008000;">.</span><span style="color: #0000FF;">SampleStandardDeviation</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>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SampleStandardDeviation_SetOfSameValues_HasZeroDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.0</span>, SameValues<span style="color: #008000;">.</span><span style="color: #0000FF;">SampleStandardDeviation</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>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SampleStandardDeviation_ScenarioValues_HaveExpectedDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>ScenarioValuesSampleDeviation, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span>ScenarioValues<span style="color: #008000;">.</span><span style="color: #0000FF;">SampleStandardDeviation</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SampleStandardDeviation_TimeSpanScenarioValues_HaveExpectedDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var timeSpanValues <span style="color: #008000;">=</span> ScenarioValues<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>seconds <span style="color: #008000;">=&gt;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>seconds<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var result <span style="color: #008000;">=</span> timeSpanValues<span style="color: #008000;">.</span><span style="color: #0000FF;">SampleStandardDeviation</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TotalSeconds</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>ScenarioValuesSampleDeviation, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span>result, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> PopulationStandardDeviation_EmptySet_HasZeroDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.0</span>, EmptySet<span style="color: #008000;">.</span><span style="color: #0000FF;">PopulationStandardDeviation</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>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> PopulationStandardDeviation_SingleValue_HasZeroDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.0</span>, SingleValue<span style="color: #008000;">.</span><span style="color: #0000FF;">PopulationStandardDeviation</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>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> PopulationStandardDeviation_SetOfSameValues_HasZeroDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.0</span>, SameValues<span style="color: #008000;">.</span><span style="color: #0000FF;">PopulationStandardDeviation</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>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> PopulationStandardDeviation_ScenarioValues_HaveExpectedDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>ScenarioValuesPopulationDeviation, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span>ScenarioValues<span style="color: #008000;">.</span><span style="color: #0000FF;">PopulationStandardDeviation</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> PopulationStandardDeviation_TimeSpanScenarioValues_HaveExpectedDeviation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var timeSpanValues <span style="color: #008000;">=</span> ScenarioValues<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>seconds <span style="color: #008000;">=&gt;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>seconds<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var result <span style="color: #008000;">=</span> timeSpanValues<span style="color: #008000;">.</span><span style="color: #0000FF;">PopulationStandardDeviation</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TotalSeconds</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>ScenarioValuesPopulationDeviation, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span>result, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</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></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2011/06/extension-method-to-compute-standard-deviation-of-ienumerabletimespan/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing HTML tables into System.Data.DataTable</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2010/05/parsing-html-tables-into-system-data-datatable/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2010/05/parsing-html-tables-into-system-data-datatable/#comments</comments>
		<pubDate>Thu, 06 May 2010 20:50:08 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DataSet]]></category>
		<category><![CDATA[DataTable]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[RegEx]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=877</guid>
		<description><![CDATA[What follows is a quick and dirty class I made to parse HTML tables into DataTables. As usual, it is the result of internet search/run/bug fix/refactor. In use, it looks little like this: WebClient client = new WebClient&#40;&#41;; string html = client.DownloadString&#40;@&#34;http://www.table.co.uk&#34;&#41;; DataSet dataSet = HtmlTableParser.Parse&#40;html&#41;; Here is the implementation. It&#8217;s not optimised for runtime [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a quick and dirty class I made to parse HTML tables into <a href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx">DataTable</a>s. As usual, it is the result of internet search/run/bug fix/refactor.</p>
<p>In use, it looks little like this:</p>

<div class="wp_codebox"><table><tr id="p87710"><td class="code" id="p877code10"><pre class="csharp" style="font-family:monospace;">WebClient client <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> html <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;http://www.table.co.uk&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DataSet dataSet <span style="color: #008000;">=</span> HtmlTableParser<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Here is the implementation. It&#8217;s not optimised for runtime performance, but it works.</p>

<div class="wp_codebox"><table><tr id="p87711"><td class="code" id="p877code11"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// HtmlTableParser parses the contents of an html string into a System.Data DataSet or DataTable.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> HtmlTableParser
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> RegexOptions ExpressionOptions <span style="color: #008000;">=</span> RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Singleline</span> <span style="color: #008000;">|</span> RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Multiline</span> <span style="color: #008000;">|</span> RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreCase</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> CommentPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;!--(.*?)--&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> TablePattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;table[^&gt;]*&gt;(.*?)&lt;/table&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> HeaderPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;th[^&gt;]*&gt;(.*?)&lt;/th&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> RowPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;tr[^&gt;]*&gt;(.*?)&lt;/tr&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> CellPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;td[^&gt;]*&gt;(.*?)&lt;/td&gt;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Given an HTML string containing n table tables, parse them into a DataSet containing n DataTables.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;html&quot;&gt;An HTML string containing n HTML tables&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A DataSet containing a DataTable for each HTML table in the input HTML&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataSet ParseDataSet<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> html<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        DataSet dataSet <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataSet<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        MatchCollection tableMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            WithoutComments<span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span>,
            TablePattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Match tableMatch <span style="color: #0600FF; font-weight: bold;">in</span> tableMatches<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            dataSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Tables</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>ParseTable<span style="color: #008000;">&#40;</span>tableMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</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;">return</span> dataSet<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Given an HTML string containing a single table, parse that table to form a DataTable.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tableHtml&quot;&gt;An HTML string containing a single HTML table&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A DataTable which matches the input HTML table&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable ParseTable<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tableHtml<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> tableHtmlWithoutComments <span style="color: #008000;">=</span> WithoutComments<span style="color: #008000;">&#40;</span>tableHtml<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        DataTable dataTable <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        MatchCollection rowMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            tableHtmlWithoutComments,
            RowPattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        dataTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddRange</span><span style="color: #008000;">&#40;</span>tableHtmlWithoutComments<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;th&quot;</span><span style="color: #008000;">&#41;</span>
                                       <span style="color: #008000;">?</span> ParseColumns<span style="color: #008000;">&#40;</span>tableHtml<span style="color: #008000;">&#41;</span>
                                       <span style="color: #008000;">:</span> GenerateColumns<span style="color: #008000;">&#40;</span>rowMatches<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        ParseRows<span style="color: #008000;">&#40;</span>rowMatches, dataTable<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> dataTable<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Strip comments from an HTML stirng</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;html&quot;&gt;An HTML string potentially containing comments&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The input HTML string with comments removed&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> WithoutComments<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> html<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span>html, CommentPattern, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span>, ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Add a row to the input DataTable for each row match in the input MatchCollection</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;rowMatches&quot;&gt;A collection of all the rows to add to the DataTable&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;dataTable&quot;&gt;The DataTable to which we add rows&lt;/param&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> ParseRows<span style="color: #008000;">&#40;</span>MatchCollection rowMatches, DataTable dataTable<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Match rowMatch <span style="color: #0600FF; font-weight: bold;">in</span> rowMatches<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// if the row contains header tags don't use it - it is a header not a row</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>rowMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;th&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                DataRow dataRow <span style="color: #008000;">=</span> dataTable<span style="color: #008000;">.</span><span style="color: #0000FF;">NewRow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                MatchCollection cellMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
                    rowMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span>,
                    CellPattern,
                    ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> columnIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> columnIndex <span style="color: #008000;">&lt;</span> cellMatches<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> columnIndex<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    dataRow<span style="color: #008000;">&#91;</span>columnIndex<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> cellMatches<span style="color: #008000;">&#91;</span>columnIndex<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Groups</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                dataTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>dataRow<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; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Given a string containing an HTML table, parse the header cells to create a set of DataColumns</span>
    <span style="color: #008080; font-style: italic;">/// which define the columns in a DataTable.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tableHtml&quot;&gt;An HTML string containing a single HTML table&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A set of DataColumns based on the HTML table header cells&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataColumn<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ParseColumns<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tableHtml<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        MatchCollection headerMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            tableHtml,
            HeaderPattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> Match headerMatch <span style="color: #0600FF; font-weight: bold;">in</span> headerMatches
                <span style="color: #0600FF; font-weight: bold;">select</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataColumn<span style="color: #008000;">&#40;</span>headerMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Groups</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><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: #0000FF;">ToArray</span><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; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// For tables which do not specify header cells we must generate DataColumns based on the number</span>
    <span style="color: #008080; font-style: italic;">/// of cells in a row (we assume all rows have the same number of cells).</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;rowMatches&quot;&gt;A collection of all the rows in the HTML table we wish to generate columns for&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A set of DataColumns based on the number of celss in the first row of the input HTML table&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataColumn<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GenerateColumns<span style="color: #008000;">&#40;</span>MatchCollection rowMatches<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> columnCount <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            rowMatches<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
            CellPattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> index <span style="color: #0600FF; font-weight: bold;">in</span> Enumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, columnCount<span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">select</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataColumn<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Column &quot;</span> <span style="color: #008000;">+</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>index<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: #0000FF;">ToArray</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></td></tr></table></div>

<p>As always, here are the tests. They yield 100% coverage but I still need to add some asserts on the column names.</p>

<div class="wp_codebox"><table><tr id="p87712"><td class="code" id="p877code12"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Tests for the HtmlTableParser class</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008000;">&#91;</span>TestClass<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ParserTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> TestContext testContextInstance<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Verify that HtmlTableParser can parse an HTML file containing a single table. The</span>
    <span style="color: #008080; font-style: italic;">/// test file includes a commented out table which should be ignored. Note some tags use</span>
    <span style="color: #008080; font-style: italic;">/// attributes (we test we can parse tags with and without attributes).</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span>DeploymentItem<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;data\singleTable.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestParseSingleTable<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;">string</span> html <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;singleTable.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        DataTable table <span style="color: #008000;">=</span> HtmlTableParser<span style="color: #008000;">.</span><span style="color: #0000FF;">ParseTable</span><span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        AssertTable<span style="color: #008000;">&#40;</span>GetExpectedData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, table<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Verify that HtmlTableParser can parse an HTML file containing multiple tables. The</span>
    <span style="color: #008080; font-style: italic;">/// test file includes a commented out table which should be ignored. The test file</span>
    <span style="color: #008080; font-style: italic;">/// contains tables both with and without headers.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span>DeploymentItem<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;data\multipleTables.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestParseMultipleTables<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;">string</span> html <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;multipleTables.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        DataSet dataSet <span style="color: #008000;">=</span> HtmlTableParser<span style="color: #008000;">.</span><span style="color: #0000FF;">ParseDataSet</span><span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>, dataSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Tables</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        var expected <span style="color: #008000;">=</span> GetExpectedData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>DataTable table <span style="color: #0600FF; font-weight: bold;">in</span> dataSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Tables</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            AssertTable<span style="color: #008000;">&#40;</span>expected, table<span style="color: #008000;">&#41;</span><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;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GetExpectedData<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> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#123;</span>
            <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;row 1, cell 1&quot;</span>, <span style="color: #666666;">&quot;row 1, cell 2&quot;</span> <span style="color: #008000;">&#125;</span>,
            <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;row 2, cell 1&quot;</span>, <span style="color: #666666;">&quot;row 2, cell 2&quot;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> AssertTable<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><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> expected, DataTable table<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>expected<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, table<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span>, <span style="color: #666666;">&quot;Table did not contain the expected number of rows&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> expected<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> expected<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> actualElement <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>table<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> expectedElement <span style="color: #008000;">=</span> expected<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>expectedElement, actualElement, <span style="color: #666666;">&quot;Table did not contain the expected element&quot;</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>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>These are the test files, which are just some basic HMTL.</p>

<div class="wp_codebox"><table><tr id="p87713"><td class="code" id="p877code13"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
      &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;all&quot; /&gt;
      &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
      &lt;!-- This table is commented out, so it shouldn't be parsed.
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Commented Heading 1&lt;/th&gt;
                  &lt;th&gt;Commented Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt;
      --&gt;
&nbsp;
      &lt;!-- The parser should ignore the border attributes --&gt;
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Heading 1&lt;/th&gt;
                  &lt;th&gt;Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                  &lt;td border=&quot;1&quot;&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p87714"><td class="code" id="p877code14"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
      &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;all&quot; /&gt;
      &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
      &lt;!-- The parser should ignore the border attributes --&gt;
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
      &lt;!-- This table is commented out, so it shouldn't be parsed.
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Commented Heading 1&lt;/th&gt;
                  &lt;th&gt;Commented Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt;
      --&gt;
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Heading 1&lt;/th&gt;
                  &lt;th&gt;Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
      &lt;table&gt;
            &lt;tr&gt;
                  &lt;td&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td border=&quot;1&quot;&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2010/05/parsing-html-tables-into-system-data-datatable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SqueezeServer WinForms Client</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2010/02/squeezeserver-winforms-client/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2010/02/squeezeserver-winforms-client/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 00:05:22 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Hi-Fi]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[squeezebox]]></category>
		<category><![CDATA[squeezecenter]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=761</guid>
		<description><![CDATA[A couple of days ago I finally got fed up with the SqueezeCenter web interface. It&#8217;s not that it&#8217;s bad, it&#8217;s just that I open a lot of tabs in my browser, then can&#8217;t find the SqueezeCenter page when I need it. I thought it would be nice to be able to control SqueezeCenter from [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago I finally got fed up with the SqueezeCenter web interface. It&#8217;s not that it&#8217;s bad, it&#8217;s just that I open a lot of tabs in my browser, then can&#8217;t find the SqueezeCenter page when I need it.</p>
<div id="attachment_789" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.hypercomplex.co.uk/wp-content/uploads/2010/02/player.jpg"><img src="http://blog.hypercomplex.co.uk/wp-content/uploads/2010/02/player-300x182.jpg" alt="" title="WinForms" width="300" height="182" class="size-medium wp-image-789" /></a><p class="wp-caption-text">A simple winforms Squeeze client.</p></div>
<p>I thought it would be nice to be able to control SqueezeCenter from something sitting in the taskbar, so over the last couple of days I made a simple .NET client. I&#8217;ve got 90% of what I need running, so as a proof of concept it has served its purpose.</p>
<p>Most of my effort was spent on data access and marshalling threads into the UI. Once I finish under the hood I might make a WPF UI layer, which would make it much more presentable.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2010/02/squeezeserver-winforms-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Should Dispose throw exceptions?</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 23:47:51 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IDisposable]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=560</guid>
		<description><![CDATA[The Framework Design Guidelines suggest that you should avoid throwing an exception from Dispose. This page has a better quote: Avoid throwing an exception from within Dispose except under critical situations where the containing process has been corrupted (leaks, inconsistent shared state, etc.). I can think of two good reasons to avoid throwing (depending on [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://my.safaribooksonline.com/9780321545671">Framework Design Guidelines</a> suggest that you should <em>avoid</em> throwing an exception from Dispose. <a href="http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae">This</a> page has a better quote: </p>
<p><em>Avoid throwing an exception from within Dispose except under critical situations where the containing process has been corrupted (leaks, inconsistent shared state, etc.).</em></p>
<p>I can think of two good reasons to avoid throwing (depending on your use case they may or may not represent the desired behaviour):</p>
<ul>
<li> If there is an unhandled exception in the finaliser thread, under .NET 2.0 and above, the <a href="http://msdn.microsoft.com/en-us/library/ms228965.aspx">process terminates</a>. Yikes. As MSDN <a href="http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx">states</a>, IDisposable classes should call Dispose from the finaliser to be sure Dispose is always called. </li>
<li> Exception masking: If dispose is triggered by a using block after an exception is thrown inside the block, then Dispose throws, you will get the exception from dispose, not the original.</li>
</ul>
<p>Rogue Dispose methods pop up in the .NET framework (e.g. in WCF), and in other <a href="http://blog.cornetdesign.com/2009/06/exceptions-in-constructors-how-reflection-helped-workaround-an-oracle-bug/">libraries</a>. If you have to contend with some dangerous disposal, Marc Gravell has a safe <a href="http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html">IDisposable wrapper</a> template/extension method pattern. This could easily be modified to do some logging rather than silently swallowing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HtmlWriter</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/08/htmlwriter/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/08/htmlwriter/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 22:50:37 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HtmlWriter]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=357</guid>
		<description><![CDATA[Today I needed to generate an HTML file. Having used XmlWriter, I wanted to follow a similar approach. In the beginning, I didn&#8217;t find HtmlTextWriter (largely because I searched for HtmlWriter on MSDN), so I wrote the class below. It uses a stack ensure you open and close matching tags and to properly indent the [...]]]></description>
			<content:encoded><![CDATA[<p>Today I needed to generate an HTML file. Having used <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx">XmlWriter</a>, I wanted to follow a similar approach. In the beginning, I didn&#8217;t find <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx">HtmlTextWriter</a> (largely because I searched for HtmlWriter on MSDN), so I wrote the class below. It uses a stack ensure you open and close matching tags and to properly indent the output. I used a <a href="http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx">TextWriter</a>, so I could plug in a <a href="http://msdn.microsoft.com/en-us/library/system.io.stringwriter.aspx">StringWriter</a> to unit test. It&#8217;s easy to instantiate and write directly to a file with new HtmlWriter(new <a href="http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx">StreamWriter</a>(path)).</p>
<p>I later tried to replace my HtmlWriter with System.Web.UI.HtmlTextWriter, but because it insists on doing some weird things (I was unable to fully control the linebreaks in the output, despite my best attempts), all my unit tests broke. If you don&#8217;t want to generate HTML which is also readable by a human then HtmlTextWriter is what you need, otherwise I offer an alternative.</p>

<div class="wp_codebox"><table><tr id="p35717"><td class="code" id="p357code17"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// A writer that provides a fast, non-cached, forward-only means of generating HTML streams.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> HtmlWriter <span style="color: #008000;">:</span> IDisposable
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> HtmlWriter<span style="color: #008000;">&#40;</span>TextWriter writer<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Writer</span> <span style="color: #008000;">=</span> writer<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span> <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Stack<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ~HtmlWriter<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;">false</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;">private</span> TextWriter Writer
    <span style="color: #008000;">&#123;</span> 
        get<span style="color: #008000;">;</span> 
        set<span style="color: #008000;">;</span> 
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> Stack<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> TagStack
    <span style="color: #008000;">&#123;</span>
        get<span style="color: #008000;">;</span>
        set<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;">string</span> MakeAnchor<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> href, <span style="color: #6666cc; font-weight: bold;">string</span> text<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;a href=<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;{1}&lt;/a&gt;&quot;</span>, href, text<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;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> MakeAttribute<span style="color: #008000;">&#40;</span>Attribute attribute<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}=<span style="color: #008080; font-weight: bold;">\&quot;</span>{1}<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, attribute<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, attribute<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</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;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> MakeAttributeSet<span style="color: #008000;">&#40;</span>Collection<span style="color: #008000;">&lt;</span>Attribute<span style="color: #008000;">&gt;</span> attributes<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        StringBuilder attributeBuilder <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Attribute attribute <span style="color: #0600FF; font-weight: bold;">in</span> attributes<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            attributeBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> MakeAttribute<span style="color: #008000;">&#40;</span>attribute<span style="color: #008000;">&#41;</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;">return</span> attributeBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><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: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteLine<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> line<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// indent according to the tag stack depth</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            line <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\t</span>'</span>, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> line<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Writer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>line<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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> OpenTag<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tag<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0}&gt;&quot;</span>, tag<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span>tag<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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> OpenTag<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tag, Attribute attribute<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0} {1}&gt;&quot;</span>, tag, MakeAttribute<span style="color: #008000;">&#40;</span>attribute<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: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span>tag<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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> OpenTag<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tag, Collection<span style="color: #008000;">&lt;</span>Attribute<span style="color: #008000;">&gt;</span> attributes<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0}{1}&gt;&quot;</span>, tag, MakeAttributeSet<span style="color: #008000;">&#40;</span>attributes<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: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span>tag<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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CloseTag<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tag<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><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InvalidOperationException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Attempt to close HTML '&quot;</span> <span style="color: #008000;">+</span> tag <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' when there are no open tags.&quot;</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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Peek</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> tag<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InvalidOperationException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Attempt to close HTML '&quot;</span> <span style="color: #008000;">+</span> tag <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' when '&quot;</span> <span style="color: #008000;">+</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Peek</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' is currently open.&quot;</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TagStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/{0}&gt;&quot;</span>, tag<span style="color: #008000;">&#41;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Tag<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tag, <span style="color: #6666cc; font-weight: bold;">string</span> content<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0}&gt;{1}&lt;/{0}&gt;&quot;</span>, tag, content<span style="color: #008000;">&#41;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Tag<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tag, Attribute attribute, <span style="color: #6666cc; font-weight: bold;">string</span> content<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0} {1}&gt;{2}&lt;/{0}&gt;&quot;</span>, tag, MakeAttribute<span style="color: #008000;">&#40;</span>attribute<span style="color: #008000;">&#41;</span>, content<span style="color: #008000;">&#41;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Tag<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tag, Collection<span style="color: #008000;">&lt;</span>Attribute<span style="color: #008000;">&gt;</span> attributes, <span style="color: #6666cc; font-weight: bold;">string</span> content<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0} {1}&gt;{2}&lt;/{0}&gt;&quot;</span>, tag, MakeAttributeSet<span style="color: #008000;">&#40;</span>attributes<span style="color: #008000;">&#41;</span>, content<span style="color: #008000;">&#41;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteStartDocument<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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;!DOCTYPE html PUBLIC <span style="color: #008080; font-weight: bold;">\&quot;</span>-//W3C//DTD XHTML 1.1 Strict//EN<span style="color: #008080; font-weight: bold;">\&quot;</span> <span style="color: #008080; font-weight: bold;">\&quot;</span>http://www.w3.org/TR/xhtml1/DTD/xhtml11-strict.dtd<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&quot;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteMeta<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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;meta http-equiv=<span style="color: #008080; font-weight: bold;">\&quot;</span>Content-Type<span style="color: #008080; font-weight: bold;">\&quot;</span> content=<span style="color: #008080; font-weight: bold;">\&quot;</span>text/html; charset=iso-8859-1<span style="color: #008080; font-weight: bold;">\&quot;</span> /&gt;&quot;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> RefStylesheet<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> stylesheet<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> tag <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;link rel=<span style="color: #008080; font-weight: bold;">\&quot;</span>stylesheet<span style="color: #008080; font-weight: bold;">\&quot;</span> href=<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> type=<span style="color: #008080; font-weight: bold;">\&quot;</span>text/css<span style="color: #008080; font-weight: bold;">\&quot;</span> media=<span style="color: #008080; font-weight: bold;">\&quot;</span>screen<span style="color: #008080; font-weight: bold;">\&quot;</span> /&gt;&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>tag, stylesheet<span style="color: #008000;">&#41;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> RefJavascript<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> javascript<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> tag <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;script src=<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> type=<span style="color: #008080; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&lt;/script&gt;&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>tag, javascript<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</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: #0600FF; font-weight: bold;">virtual</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Writer</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>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And here is the attribute struct, just for completeness.</p>

<div class="wp_codebox"><table><tr id="p35718"><td class="code" id="p357code18"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Html tag attribute as name value pair</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">struct</span> Attribute
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> Attribute<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> name, <span style="color: #6666cc; font-weight: bold;">string</span> value<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Name <span style="color: #008000;">=</span> name<span style="color: #008000;">;</span>
        Value <span style="color: #008000;">=</span> value<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;">string</span> Name
    <span style="color: #008000;">&#123;</span>
        get<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> set<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;">string</span> Value
    <span style="color: #008000;">&#123;</span>
        get<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/08/htmlwriter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Type System and Conversions</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/csharp-type-system/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/csharp-type-system/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 22:15:29 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=183</guid>
		<description><![CDATA[This is really a catch all post for the things I missed when reviewing the fundamentals. Skeet describes the C# type system as static, explicit and safe. Widening and narrowing conversions A narrowing conversion is a conversion which loses precision, for example converting a double to an int is a narrowing conversion. In C#, you [...]]]></description>
			<content:encoded><![CDATA[<p>This is really a catch all post for the things I missed when reviewing the fundamentals. <a href="http://skeetoverflow.com/">Skeet</a> describes the C# type system as static, explicit and safe.</p>
<p><strong>Widening and narrowing conversions</strong></p>
<p>A narrowing conversion is a conversion which loses precision, for example converting a double to an int is a narrowing conversion. In C#, you can usually only perform narrowing conversions explicitly. Widening conversions, which preserve precision, may be performed implicitly.</p>
<p><strong>Implementing conversion: Overview</strong></p>
<p>.NET 2.0 introduced the TryParse family of methods, which in addition to the ToString and Parse methods, may be used to convert types to and from strings. Obviously it is possible to cast between types, but only between types that define <a href="http://msdn.microsoft.com/en-us/library/09479473%28VS.80%29.aspx">conversion operators</a>. In summary, you can implement conversion using these methods:</p>
<ul>
<li>Conversion operators (operator overloading for for implicit/explicit casts)</li>
<li>Override object ToString and Parse methods</li>
<li>Implement IConvertible</li>
<li>Implement a TypeConverter. This is not in scope for the foundation course.</li>
</ul>
<p>There&#8217;s nothing mysterious about conversion operators or overriding object methods, just remember to use implicit for widening conversions and explicit for narrowing conversions. Here&#8217;s a zero utility example:</p>

<div class="wp_codebox"><table><tr id="p18321"><td class="code" id="p183code21"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">struct</span> MyConvertible
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> value<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> MyConvertible<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        value <span style="color: #008000;">=</span> i<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: #0600FF; font-weight: bold;">explicit</span> <span style="color: #0600FF; font-weight: bold;">operator</span> MyConvertible<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">long</span> l<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MyConvertible<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;">&#41;</span>l<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;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">implicit</span> <span style="color: #0600FF; font-weight: bold;">operator</span> <span style="color: #6666cc; font-weight: bold;">long</span><span style="color: #008000;">&#40;</span>MyConvertible c<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">value</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: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToString<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: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</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></td></tr></table></div>

<p><strong>Implementing <a href="http://msdn.microsoft.com/en-us/library/system.iconvertible.aspx">IConvertible</a></strong></p>
<p>Clearly, it would not be possible to implement every method in IConvertible. It&#8217;s best to just add the IConvertible interface to your type, then use Visual Studio generate method stubs. Either implement methods, or throw an <a href="http://msdn.microsoft.com/en-us/library/system.invalidcastexception.aspx">InvalidCastException</a> if there is no meaningful conversion. Personally I would prefer Visual Studio to give me this by default instead of NotImplementedException.</p>
<p><strong>Co-variance and contra-variance</strong></p>
<p>This is really a diversion from the .NET foundation course, but it is worth understanding properly. For an in depth view, please refer to Eric Lippert&#8217;s variance series, part <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.aspx">1</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx">2</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/19/covariance-and-contravariance-in-c-part-three-member-group-conversion-variance.aspx">3</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/22/covariance-and-contravariance-in-c-part-four-real-delegate-variance.aspx">4</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/24/covariance-and-contravariance-in-c-part-five-higher-order-functions-hurt-my-brain.aspx">5</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx">6</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/29/covariance-and-contravariance-in-c-part-seven-why-do-we-need-a-syntax-at-all.aspx">7</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/31/covariance-and-contravariance-in-c-part-eight-syntax-options.aspx">8</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/11/02/covariance-and-contravariance-in-c-part-nine-breaking-changes.aspx">9</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2007/11/09/covariance-and-contravariance-in-c-part-ten-dealing-with-ambiguity.aspx">10</a>, <a href="http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx">11 to infinity</a>. Eric uses the terms bigger and smaller to refer to what I think of as generalised and specialised types. </p>
<p>For example, in a hierarchy of shapes, rooted at Shape, you could have two branches, one consisting of Polygon and Triangle, the other consisting of Ellipse and Circle. Clearly, a triangle is a specialised version of Shape, and Shape is a generalised version of Triangle. Circle is neither a specialisation or generalisation of Triangle. </p>
<p>To quote <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29">Wikipedia</a> a type conversion operator is:</p>
<ul>
<li>covariant if it preserves the ordering, ≤, of types, which orders types from more specific to more generic;</li>
<li>contravariant if it reverses this ordering, which orders types from more generic to more specific;</li>
<li>invariant if neither of the above apply.</li>
</ul>
<p>That&#8217;s a bit wordy. To convince myself that I understand what it means in the context of C# I tried to think of an example of each. In C#, let&#8217;s say we do something like Shape s = new Triangle(). This assignment is a <em>covariant</em> operation: you are assigning a specialised reference to a more general reference.</p>
<p>If you tried to write something like this: List&lt;Shape&gt; shapes = new List&lt;Triangle&gt;() the C# compiler will complain. This is because generics are <em>invariant</em>, the types List&lt;Shape&gt; and List&lt;Triangle&gt; are <a href="http://msdn.microsoft.com/en-gb/library/aa479859.aspx#fundamentals_topic12">not related</a>.</p>
<p>Contravariance is tricker to present in a byte size example, consider this fragment:</p>

<div class="wp_codebox"><table><tr id="p18322"><td class="code" id="p183code22"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Contravariant
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #6666cc; font-weight: bold;">void</span> MyDelegate<span style="color: #008000;">&#40;</span>Triangle value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">void</span> Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        MyDelegate shapeDelegate <span style="color: #008000;">=</span> ShapeMethod<span style="color: #008000;">;</span>
        MyDelegate triangleDelegate <span style="color: #008000;">=</span> TriangleMethod<span style="color: #008000;">;</span>
&nbsp;
        Triangle t <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Triangle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        shapeDelegate<span style="color: #008000;">&#40;</span>t<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;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> ShapeMethod<span style="color: #008000;">&#40;</span>Shape value<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</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> TriangleMethod<span style="color: #008000;">&#40;</span>Triangle value<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The delegate parameter type is <em>contravariant</em>. At first this might look a little strange. But inside ShapeMethod we only care about shapes, so invoking shapeDelegate with a Triangle is fine, because it is just treated as a Shape.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/csharp-type-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delegates and Events</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/delegates-and-events/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/delegates-and-events/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 18:16:58 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=167</guid>
		<description><![CDATA[Delegates A delegate is a reference type that encapsulates a method, and may be used rather like a function pointer in C/C++. Unlike function pointers, delegates are object-oriented and type-safe. The term delegate is used to describe both delegate types and instances. When you declare a delegate type, you define the method signature for instances [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Delegates</strong></p>
<p>A <a href="http://msdn.microsoft.com/en-us/library/900fyy8e%28VS.71%29.aspx">delegate</a> is a reference type that encapsulates a method, and may be used rather like a function pointer in C/C++. Unlike function pointers, delegates are object-oriented and type-safe. The term delegate is used to describe both delegate <em>types</em> and <em>instances</em>.</p>
<p>When you declare a delegate type, you define the method signature for instances of the type. Delegate instances always refer to methods. The following somewhat contrived example illustrates type and instance declaration, and delegate invocation.</p>

<div class="wp_codebox"><table><tr id="p16724"><td class="code" id="p167code24"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #6666cc; font-weight: bold;">void</span> DelegateType<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> DelegateHost
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> InstanceMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Instance method.&quot;</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;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> StaticMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Static method.&quot;</span><span style="color: #008000;">&#41;</span><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;">class</span> Test
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DelegateType delegateInstance<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        delegateInstance <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DelegateType<span style="color: #008000;">&#40;</span>DelegateHost<span style="color: #008000;">.</span><span style="color: #0000FF;">StaticMethod</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        delegateInstance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        DelegateHost host <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DelegateHost<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        delegateInstance <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DelegateType<span style="color: #008000;">&#40;</span>host<span style="color: #008000;">.</span><span style="color: #0000FF;">InstanceMethod</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        delegateInstance<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></td></tr></table></div>

<p>As well as having single action delegate instances, you can also create <a href="http://msdn.microsoft.com/en-us/library/ms173175.aspx">multicast delegates</a>. Delegate instances can be <a href="http://msdn.microsoft.com/en-us/library/system.delegate.combine.aspx">combined</a> and <a href="http://msdn.microsoft.com/en-us/library/system.delegate.remove.aspx">removed</a> from one another. Delegate instances are immutable.</p>
<p><strong>Events</strong></p>
<p>I originally understood <a href="http://msdn.microsoft.com/en-us/library/8627sbea.aspx">events</a> to be a multicast form of delegates. Given that you can create multicast delegates, what then is the purpose of event?</p>
<p>The event keyword is effectively a modifier for a delegate declaration. Events may be distinguished from raw delegates as follows:</p>
<ul>
<li>An event can be included in an interface declaration, whereas a delegate field cannot</li>
<li>An event can only be invoked from within the class that declared it</li>
<li>Events have a pair of customisable accessors (add and remove), in this regard they are very similar to properties</li>
<li>Within the .NET framework, an event delegate signature must be of the form void Event(object sender, EventArgs e)
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/delegates-and-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET Framework Fundamentals</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-fundamentals/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-fundamentals/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 18:50:56 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[boxing]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=120</guid>
		<description><![CDATA[Some time ago I began studying for the .NET Framework 2.0 Application Development Foundation exam. In order to cement the aquired knowledge I have decided to post my notes as I recap, starting at the absolute beginning. Types For some time I didn&#8217;t fully appreciate the difference between reference and value types. The following table [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I began studying for the .NET Framework 2.0 Application Development Foundation exam. In order to cement the aquired knowledge I have decided to post my notes as I recap, starting at the absolute beginning.</p>
<p><strong>Types</strong></p>
<p>For some time I didn&#8217;t fully appreciate the difference between reference and value types. The following table summarises the important characteristics:</p>
<table border="0" cellspacing="1" cellpadding="1" width="100%">
<tbody>
<tr>
<th scope="col"><a href="http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx">Value Types</a></th>
<th scope="col"><a href="http://msdn.microsoft.com/en-us/library/490f96s2.aspx">Reference Types</a></th>
</tr>
<tr>
<td>Actually contain data. Assignment copies data.</td>
<td>Store a reference to the real data, assignment copies the reference.</td>
</tr>
<tr>
<td>Local value types reside on the stack.*</td>
<td>Reference resides on the stack, the actual data on the heap.</td>
</tr>
<tr>
<td>Derived (implicitly) from System.ValueType</td>
<td>Derived from System.Object, or a descendant</td>
</tr>
<tr>
<td>User value types are structs or enums. Unlike in C, you can define methods on structs.</td>
<td>May be a class, interface, delegate or an array (including arrays of value types).</td>
</tr>
<tr>
<td>Implicitly sealed</td>
<td></td>
</tr>
</tbody>
</table>
<p>* Local as in declared within a method. When allocating a class with a value type member, this member will be allocated on the heap.</p>
<p><strong>Boxing and Unboxing</strong></p>
<p>A boxing operation converts a value type to a reference type; unboxing converts a reference type to a value type. For example, converting an int to an object (boxing) causes a new int to be created on the heap, the object reference then refers to this copy. Clearly, this incurs some overhead.</p>
<p><strong>Generics</strong></p>
<p>Generics are paramaterised types and methods, and are superficially <a href="http://msdn.microsoft.com/en-us/library/c6cyy67b.aspx">similar to C++ templates</a>. The most common use of generics in the .NET framework are the collection classes introduced in .NET 2.0.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/d5x73970.aspx ">Constraints</a> may be specified to restrict use of generic types and methods: </p>
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>where T : struct</td>
<td>any value type, excluding Nullable types</td>
</tr>
<tr>
<td>where T : class</td>
<td>any reference type (class, interface, delegate or array)</td>
</tr>
<tr>
<td>where T : new</td>
<td>T must have a public parameterless constructor</td>
</tr>
<tr>
<td>where T : &lt;base&gt;</td>
<td>T must derive from base, which can be either a class or interface. Multiple interfaces may be specified, and they may also be generic.</td>
</tr>
</table>
<p><strong>Attributes</strong></p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.attribute.aspx ">Attributes</a> are programmatic annotations (metadata) that may be queried by reflection. An item decorated with an attribute can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute. Attributes derive from System.Attribute.</p>
<p>The TypeForwardedTo attribute allows you to move a type from one assembly to another without having to recompile the application which uses the original assembly. This is succinctly illustrated <a href="http://blogs.msdn.com/thottams/archive/2006/11/17/typeforwarding-typeforwardedto-attribute-in-runtime-compilerservices.aspx">here</a>.</p>
<p><strong>Partial Classes</strong></p>
<p>Partial Classes facilitate splitting class definitions across files. I find this is useful when using code generation – you can re-generate an entire class file from a model without overwriting hand written code.</p>
<p><strong>Exceptions</strong></p>
<p>The training material recommends that you define your own exceptions which derive from System.ApplicationException. It is therefore possible to differentiate between exceptions thrown from application code and the .NET framework.</p>
<p>In reality, it is best practice to simply inherit from Exception, <a href="http://stackoverflow.com/questions/52753/derive-from-exception-or-applicationexception-in-net">as ApplicationException didn’t add any value</a>.</p>
<p>Either way, having distinct exception classes allows you to respond to different exceptions differently. Since it is possible to catch a derived exception by catching the base type, catch blocks should be ordered from most to least specific.</p>
<p>The finally block should be used to dispose of non memory resources, e.g. closing files or a SqlConnection.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-fundamentals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When dependency injection meets IDisposable</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:50:58 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[IDisposable]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=500</guid>
		<description><![CDATA[What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with a class you need to instantiate inline, such as an IDisposable class? Consider this: public class NiceClass &#123; private DependentClass injectedDependency; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with a class you need to instantiate inline, such as an IDisposable class? Consider this:</p>

<div class="wp_codebox"><table><tr id="p50028"><td class="code" id="p500code28"><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> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>DependentClass injectedDependency<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<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> CoupledMethod<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;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>We can see that injectedDependency is not coupled, but what about the local instance of Face in CoupledMethod? To decouple it use a delegate to return the IFace instance instead of calling new directly. If the coupling had been to a concrete type (Face), we could refactor to an abstract type (IFace) or wrapper first.</p>

<div class="wp_codebox"><table><tr id="p50029"><td class="code" id="p500code29"><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> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Func<span style="color: #008000;">&lt;</span>DependentClass, IFace<span style="color: #008000;">&gt;</span> faceFactoryMethod<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>
        DependentClass injectedDependency, 
        Func<span style="color: #008000;">&lt;</span>DependentClass, IFace<span style="color: #008000;">&gt;</span> faceFactoryMethod<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">faceFactoryMethod</span> <span style="color: #008000;">=</span> faceFactoryMethod<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> CoupledMethod<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;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">faceFactoryMethod</span><span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This is an additional three lines of code, which arguably makes the control flow harder to read statically. It does, however, improve testability. I think of this as equivalent to the classic tradeoff between <a href="http://www.threeriversinstitute.org/AbstractVsConcreteParameters.html">abstract and concrete parameters</a>.</p>
<p>Instantiating NiceClass is not too arduous either with a lambda expression (although it is still a little verbose).</p>

<div class="wp_codebox"><table><tr id="p50030"><td class="code" id="p500code30"><pre class="csharp" style="font-family:monospace;">NiceClass nice <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> NiceClass<span style="color: #008000;">&#40;</span>dependency, d <span style="color: #008000;">=&gt;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

