<?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>Think In Geek &#187; c#</title>
	<atom:link href="http://thinkingeek.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkingeek.com</link>
	<description>In geek we trust</description>
	<lastBuildDate>Sat, 19 Jun 2010 22:00:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C#, the &#8220;and&#8221; operators and lazy evaluation</title>
		<link>http://thinkingeek.com/2009/10/12/c-the-and-operators-and-lazy-evaluation/</link>
		<comments>http://thinkingeek.com/2009/10/12/c-the-and-operators-and-lazy-evaluation/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 15:00:11 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=121</guid>
		<description><![CDATA[Today at work we found a bug. My workmate, not used to C#, usually uses the &#38; operator to compare boolean values. However, in C#, the &#38; operator does not use lazy evaluation. One curious thing about C# is that it can use two different operators to calculate an and expression: the &#38; operator and [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work we found a bug. My workmate, not used to C#, usually uses the <i>&amp;</i> operator to compare boolean values. However, in C#, the <i>&amp;</i> operator does not use <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Lazy_evaluation?referer=');">lazy evaluation</a>.</p>
<p>One curious thing about C# is that it can use two different operators to calculate an <i>and</i> expression: the <a href="http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/sbf85k1c.aspx?referer=');">&amp;</a> operator and the <a href="http://msdn.microsoft.com/en-us/library/2a723cdk.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/2a723cdk.aspx?referer=');">&amp;&amp;</a> operator. The difference between both is that the first one (<i>&amp;</i>) can be used both with integer types and boolean types. When used with integer types it will perform a bitwise comparison between the two, and when used with boolean values it will use the logical <i>and</i> operation between the two boolean values, <b>evaluating all the parts of the expression</b>. This means that using a code like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>someObject <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;</span> someObject.<span style="color: #0000FF;">SomeProperty</span> <span style="color: #008000;">==</span> someValue<span style="color: #000000;">&#41;</span></pre></div></div>

<p>will throw a runtime error if <i>someObject</i> is null, because it will try to obtain the <i>SomeProperty</i> value.</p>
<p>However, the <i>&amp;&amp;</i> operator is only available to boolean expressions, and it uses lazy evaluation, this is, if the first condition evaluated is false, it will calculate false without evaluating the rest of the expression, because an <i>and</i> is only true if all the expressions are true.</p>
<p>Conclusion, be sure to always use <i>&#038;amp&amp;</i> when evaluating boolean values if you want to avoid run time surprises <img src='http://thinkingeek.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2009%2F10%2F12%2Fc-the-and-operators-and-lazy-evaluation%2F&amp;linkname=C%23%2C%20the%20%26%238220%3Band%26%238221%3B%20operators%20and%20lazy%20evaluation" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2009_2F10_2F12_2Fc-the-and-operators-and-lazy-evaluation_2F_amp_linkname=C_23_2C_20the_20_26_238220_3Band_26_238221_3B_20operators_20and_20lazy_20evaluation&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2009/10/12/c-the-and-operators-and-lazy-evaluation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating and testing a Linked List based Queue in C# using Nunit</title>
		<link>http://thinkingeek.com/2009/07/09/creating-and-testing-a-linked-list-based-queue-in-c-using-nunit/</link>
		<comments>http://thinkingeek.com/2009/07/09/creating-and-testing-a-linked-list-based-queue-in-c-using-nunit/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 16:23:22 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=108</guid>
		<description><![CDATA[The Queue&#60;T&#62; class (and Stack&#60;T&#62; too) of the .NET Framework from Microsoft is implemented using an array. While this is a perfectly good approach, I think that a Linked List based implementation could be desired in some situations (specifically when the size of the queue is not fixed). Since the implementation alone would be rather [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/7977ey2c.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/7977ey2c.aspx?referer=');">Queue&lt;T&gt;</a> class (and <a href="http://msdn.microsoft.com/en-us/library/3278tedw.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/3278tedw.aspx?referer=');">Stack&lt;T&gt;</a> too) of the .NET Framework from Microsoft is implemented using an array. While this is a perfectly good approach, I think that a <a href="http://en.wikipedia.org/wiki/Linked_list" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Linked_list?referer=');">Linked List</a> based implementation could be desired in some situations (specifically when the size of the queue is not fixed).</p>
<p>Since the implementation alone would be rather simple for a post, I&#8217;ll show you how to implement <a href="http://en.wikipedia.org/wiki/Unit_testing" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Unit_testing?referer=');">Unit Testing</a> with the class using <a href="http://www.nunit.org/" onclick="pageTracker._trackPageview('/outgoing/www.nunit.org/?referer=');">Nunit</a>. Although this is a rather simple class to test I think it will show the basic concepts behind unit testing.<br />
<span id="more-108"></span><br />
First of all let&#8217;s begin with the interface of the class. We will call it <em>LinkedListQueue&lt;T&gt;</em>. We basically need the typical operations of a queue.</p>
<p>The interface (without the implementation) is going to be something similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LinkedListQueue
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080;">#region LinkedListQueue Members</span>
&nbsp;
    <span style="color: #0600FF;">public</span> T Dequeue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">NotImplementedException</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Enqueue<span style="color: #000000;">&#40;</span>T item<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">NotImplementedException</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> T Peek<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">NotImplementedException</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Clear<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">NotImplementedException</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Count
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">NotImplementedException</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The thing about unit testing is to first write the different tests so we know <strong>what we want to test</strong> and then we have to write the code so the tests are passed. Following this convention, now is the time to write some tests using Nunit. To do so, we have to create a new project of type <em>Class Library</em> in Visual Studio (we could have the tests on the same code but it&#8217;s better to have it separately so we don&#8217;t bloat the real production code). For more information about how to install and set up Nunit I suggest you visit <a href="http://www.nunit.org/" onclick="pageTracker._trackPageview('/outgoing/www.nunit.org/?referer=');">the site</a> for more information. I called the project <em>LinkedListQueueTest</em></p>
<p>Once we have the code, we can start writing tests in a special class which will have this modifier:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LinkedListQueueTest
<span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span></pre></div></div>

<p>Inside this class we have to write one method for each test we want to perform. It&#8217;s usually a good practice to have one test per method on the class you&#8217;re testing. In our case, we will begin testing the <em>Enqueue</em> method. To mark a method as a test, we have to add a modifier to it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dequeue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span></pre></div></div>

<p>And in the body of the method, we have to perform the test we need. Usually the tests have a structure: creating an object on which to test, executing the method, and confirm that the result is the same as we expected. Let&#8217;s do a simple code for our test:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dequeue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    LinkedListQueue queue <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinkedListQueue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//We enqueue some items to test</span>
    queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//For each number enqueued we check that is removed properly and in the</span>
    <span style="color: #008080; font-style: italic;">//queue order (FIFO)</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">Throws</span><span style="color: #000000;">&#40;</span>
        <span style="color: #FF0000;">delegate</span>
        <span style="color: #000000;">&#123;</span>
            queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I suggest you read the Nunit documentation to see the different assertions you have available.</p>
<p>Now that we wrote our first test, we should start thinking and writing the rest of the tests. To simplify the post, here&#8217;s a full class showing some tests that you may have wanted to perform:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">NUnit.Framework</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ThinkInGeek.<span style="color: #0000FF;">Collections</span>.<span style="color: #0000FF;">Generic</span>.<span style="color: #0000FF;">Testing</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LinkedListQueueTest
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dequeue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            LinkedListQueue queue <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinkedListQueue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//We enqueue some items to test</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//For each number enqueued we check that is removed properly and in the</span>
            <span style="color: #008080; font-style: italic;">//queue order (FIFO)</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">Throws</span><span style="color: #000000;">&#40;</span>
                <span style="color: #FF0000;">delegate</span>
                <span style="color: #000000;">&#123;</span>
                    queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Enqueue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            LinkedListQueue queue <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinkedListQueue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//We enqueue some items to test and check that the items are inserted correctly</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Peek<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            LinkedListQueue queue <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinkedListQueue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//We enqueue some items to test</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//We check that after a peeking we have the correct value but the item</span>
            <span style="color: #008080; font-style: italic;">//is not deleted</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">Throws</span><span style="color: #000000;">&#40;</span>
                <span style="color: #FF0000;">delegate</span>
                <span style="color: #000000;">&#123;</span>
                    queue.<span style="color: #0000FF;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Clear<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            LinkedListQueue queue <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinkedListQueue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            queue.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>queue.<span style="color: #0000FF;">Count</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To see how our test behave, we have to open the resulting <em>LinkedListQueueTest.dll</em> in the Nunit tester program. As we have written no real code on the queue class, it&#8217;s obvious that our tests will fail. So let&#8217;s get our hands dirty and start programming our queue.</p>
<p>I will not go into the details because a linked list based queue is really easy to implement, so here&#8217;s the final code of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ThinkInGeek.<span style="color: #0000FF;">Collections</span>.<span style="color: #0000FF;">Generic</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LinkedListQueue
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080;">#region Node inner class</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Node
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">public</span> K Element <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">public</span> Node NextElement <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Private members</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> m_count <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> Node m_head <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> Node m_tail <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #0600FF;">public</span> T Dequeue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>m_head <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #008080; font-style: italic;">//We have to return the item on the head and update the new head</span>
            T currentItem <span style="color: #008000;">=</span> m_head.<span style="color: #0000FF;">Element</span><span style="color: #008000;">;</span>
            m_head <span style="color: #008000;">=</span> m_head.<span style="color: #0000FF;">NextElement</span><span style="color: #008000;">;</span>
            m_count<span style="color: #008000;">--;</span>
            <span style="color: #0600FF;">return</span> currentItem<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Enqueue<span style="color: #000000;">&#40;</span>T item<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//We need to add the item and update the tail</span>
            Node newNode <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Node<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            newNode.<span style="color: #0000FF;">Element</span> <span style="color: #008000;">=</span> item<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>m_tail <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                m_tail.<span style="color: #0000FF;">NextElement</span> <span style="color: #008000;">=</span> newNode<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            m_tail <span style="color: #008000;">=</span> newNode<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>m_head <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                m_head <span style="color: #008000;">=</span> newNode<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            m_count<span style="color: #008000;">++;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> T Peek<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>m_head <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> m_head.<span style="color: #0000FF;">Element</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Clear<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            m_head <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            m_tail <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            m_count <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Count
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> m_count<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The advantage of unit testing is that now the testing is going to be done automatically. The only thing we have to do is open the testing dll file into Nunit and see how&#8217;s the outcome. As we&#8217;re really good programmers, using the code I showed you, all the tests will pass <img src='http://thinkingeek.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="attachment_115" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2009/07/unittesting.png"  rel="lightbox[roadtrip]"><img class="size-medium wp-image-115" title="UnitTesting" src="http://thinkingeek.com/wp-content/uploads/2009/07/unittesting-300x227.png" alt="Unit Testing results window" width="300" height="227" /></a><p class="wp-caption-text">Unit Testing results window</p></div>
<p>This is a really simple example of how things should be done with unit testing using C# and Nunit, but I hope it can get you started and you can grasp the philosophy of it. For more information I really recommend reading the book <a href="http://www.amazon.com/Pragmatic-Unit-Testing-NUnit-2nd/dp/0977616673/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1247156448&amp;sr=8-1" onclick="pageTracker._trackPageview('/outgoing/www.amazon.com/Pragmatic-Unit-Testing-NUnit-2nd/dp/0977616673/ref=sr_1_1?ie=UTF8_amp_s=books_amp_qid=1247156448_amp_sr=8-1&amp;referer=');">Pragmatic Unit Testing in C# with NUnit</a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2009%2F07%2F09%2Fcreating-and-testing-a-linked-list-based-queue-in-c-using-nunit%2F&amp;linkname=Creating%20and%20testing%20a%20Linked%20List%20based%20Queue%20in%20C%23%20using%20Nunit" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2009_2F07_2F09_2Fcreating-and-testing-a-linked-list-based-queue-in-c-using-nunit_2F_amp_linkname=Creating_20and_20testing_20a_20Linked_20List_20based_20Queue_20in_20C_23_20using_20Nunit&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2009/07/09/creating-and-testing-a-linked-list-based-queue-in-c-using-nunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using IronPython to extend your .NET applications</title>
		<link>http://thinkingeek.com/2009/03/11/using-ironpython-to-extend-your-net-applications/</link>
		<comments>http://thinkingeek.com/2009/03/11/using-ironpython-to-extend-your-net-applications/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 15:44:32 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[ironpython]]></category>
		<category><![CDATA[ironruby]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=93</guid>
		<description><![CDATA[One of the interesting new things on the .NET platform is the recent addition of Python and Ruby to the CLR. Both versions for .NET are called IronPython and IronRuby respectively, and they provide some new and good things to the platform. Python and Ruby lovers will see now that they can use all the [...]]]></description>
			<content:encoded><![CDATA[<p>
One of the interesting new things on the .NET platform is the recent addition of <a href="http://www.python.org/" onclick="pageTracker._trackPageview('/outgoing/www.python.org/?referer=');">Python</a> and <a href="http://ruby-lang.org/" onclick="pageTracker._trackPageview('/outgoing/ruby-lang.org/?referer=');">Ruby</a> to the CLR. Both versions for .NET are called <a href="http://www.codeplex.com/IronPython" onclick="pageTracker._trackPageview('/outgoing/www.codeplex.com/IronPython?referer=');">IronPython</a> and <a href="http://www.ironruby.net/" onclick="pageTracker._trackPageview('/outgoing/www.ironruby.net/?referer=');">IronRuby</a> respectively, and they provide some new and good things to the platform.
</p>
<p>
Python and Ruby lovers will see now that they can use all the library and features of the .NET platform programming in their favorite scripting language. Since both of them are object oriented, you can now write fully fledged apps using either of them.
</p>
<p>
However, there&#8217;s another interesting application for IronPython and IronRuby: adding scripting support for your existing .NET applications. This can be a very useful and powerful way to extend your applications and give the user freedom to program their own mini programs, scripts or whatever in your applications. It could be good for defining rules, assigning and calculating values, etc.
</p>
<p>
I&#8217;ll provide a simple class you can use to add scripting to your application. I&#8217;ll use IronPython in this example.
</p>
<p>
First of all, you have to download IronPython and install it, and add the references to the assemblies on your project references.
</p>
<p>
The usual way to proceed in those cases is to provide the user of some local variables you give them access to, execute the script, and then recover the values of those or new variables. To do so, You can use a class similar to this one:
</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">IronPython.Hosting</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.Scripting.Hosting</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.Scripting</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Scripting
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">class</span> PythonEngine
	<span style="color: #000000;">&#123;</span>
        ScriptEngine m_engine<span style="color: #008000;">;</span>
        ExceptionOperations m_exceptionOperations<span style="color: #008000;">;</span>
        SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> m_inputVariables<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> m_script<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> PythonEngine<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            m_engine <span style="color: #008000;">=</span> Python.<span style="color: #0000FF;">CreateEngine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            m_exceptionOperations <span style="color: #008000;">=</span> m_engine.<span style="color: #0000FF;">GetService</span><span style="color: #008000;">&lt;</span>ExceptionOperations<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> ScriptVariables
        <span style="color: #000000;">&#123;</span>
            set <span style="color: #000000;">&#123;</span> m_inputVariables <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">string</span> Script
        <span style="color: #000000;">&#123;</span>
            set <span style="color: #000000;">&#123;</span> m_script <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> ExceptionOperations ExceptionOperations
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> m_exceptionOperations<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">internal</span> SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> Execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//Create structures</span>
            SourceCodeKind sc <span style="color: #008000;">=</span> SourceCodeKind.<span style="color: #0000FF;">Statements</span><span style="color: #008000;">;</span>
            ScriptSource source <span style="color: #008000;">=</span> m_engine.<span style="color: #0000FF;">CreateScriptSourceFromString</span><span style="color: #000000;">&#40;</span>m_script, sc<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ScriptScope scope <span style="color: #008000;">=</span> m_engine.<span style="color: #0000FF;">CreateScope</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//Fill input variables</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>KeyValuePair<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> variable <span style="color: #0600FF;">in</span> m_inputVariables<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                scope.<span style="color: #0000FF;">SetVariable</span><span style="color: #000000;">&#40;</span>variable.<span style="color: #0000FF;">Key</span>, variable.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> outputVariables <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SortedDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//Execute the script</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                source.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span>scope<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">//Recover variables</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> variable <span style="color: #0600FF;">in</span> scope.<span style="color: #0000FF;">GetVariableNames</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    outputVariables.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>variable, scope.<span style="color: #0000FF;">GetVariable</span><span style="color: #000000;">&#40;</span>variable<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> error <span style="color: #008000;">=</span> m_exceptionOperations.<span style="color: #0000FF;">FormatException</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">//Do something with the pretty printed error</span>
                throw<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> outputVariables<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</p>
<p>
Usage of this class is pretty simple. You have to provide the object the script you want to execute and the input variables the script will have available as local variables. Once this is done, you have to call the Execute method, and this method will either return the output variables of the execution of the resulting script, or throw an exception.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2009%2F03%2F11%2Fusing-ironpython-to-extend-your-net-applications%2F&amp;linkname=Using%20IronPython%20to%20extend%20your%20.NET%20applications" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2009_2F03_2F11_2Fusing-ironpython-to-extend-your-net-applications_2F_amp_linkname=Using_20IronPython_20to_20extend_20your_20.NET_20applications&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2009/03/11/using-ironpython-to-extend-your-net-applications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Full view of ComboBox drop-down list components in C# 3.0</title>
		<link>http://thinkingeek.com/2008/11/18/full-view-of-combobox-drop-down-list-components-in-c-30/</link>
		<comments>http://thinkingeek.com/2008/11/18/full-view-of-combobox-drop-down-list-components-in-c-30/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 12:43:02 +0000</pubDate>
		<dc:creator>alopez</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=88</guid>
		<description><![CDATA[By default in C# 3.0 ComboBox controls don’t provide support for showing drop-down list items if they exceed the width of their parent ComboBox, like this one: This is annoying because users cannot read properly the information. To solve that problem, all we have to do is derive the ComboBox class and override the DropDown [...]]]></description>
			<content:encoded><![CDATA[<p>By default in C# 3.0 ComboBox controls don’t provide support for showing drop-down list items if they exceed the width of their parent ComboBox, like this one:
</p>
<div id="attachment_89" class="wp-caption aligncenter" style="width: 246px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/image001.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/image001.png" alt="Cropped ComboBox" title="Cropped ComboBox" width="236" height="123" class="size-medium wp-image-89" /></a><p class="wp-caption-text">Cropped ComboBox</p></div>
<p>
This is annoying because users cannot read properly the information. To solve that problem, all we have to do is derive the ComboBox class and override the DropDown event as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ComboBoxEx <span style="color: #008000;">:</span> ComboBox
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> ComboBoxEx<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		DropDown <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">&#40;</span>event_DropDown<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">void</span> event_DropDown<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">try</span>
		<span style="color: #000000;">&#123;</span>
			ComboBox comboBox <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ComboBox<span style="color: #000000;">&#41;</span>sender<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Catch the combo firing this event</span>
			<span style="color: #FF0000;">int</span> width <span style="color: #008000;">=</span> comboBox.<span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Current width for ComboBox</span>
			Graphics g <span style="color: #008000;">=</span> comboBox.<span style="color: #0000FF;">CreateGraphics</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Get graphics for ComboBox</span>
			Font font <span style="color: #008000;">=</span> comboBox.<span style="color: #0000FF;">Font</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Doesn't change original font</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">//checks if a scrollbar will be displayed.</span>
			<span style="color: #FF0000;">int</span> vertScrollBarWidth<span style="color: #008000;">;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>comboBox.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> comboBox.<span style="color: #0000FF;">MaxDropDownItems</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
				<span style="color: #008080; font-style: italic;">//If yes, then get its width to adjust the size of the drop down list.</span>
				vertScrollBarWidth <span style="color: #008000;">=</span> SystemInformation.<span style="color: #0000FF;">VerticalScrollBarWidth</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0600FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-style: italic;">//Otherwise set to 0</span>
				vertScrollBarWidth <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #008080; font-style: italic;">//Loop through list items and check size of each items.</span>
			<span style="color: #008080; font-style: italic;">//set the width of the drop down list to the width of the largest item.</span>
			<span style="color: #FF0000;">int</span> newWidth<span style="color: #008000;">;</span>
			<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> s <span style="color: #0600FF;">in</span> comboBox.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>s <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					newWidth <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>g.<span style="color: #0000FF;">MeasureString</span><span style="color: #000000;">&#40;</span>s.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, font<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Width</span> <span style="color: #008000;">+</span> vertScrollBarWidth<span style="color: #008000;">;</span>
					<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>width <span style="color: #008000;">&lt;</span> newWidth<span style="color: #000000;">&#41;</span>
						width <span style="color: #008000;">=</span> newWidth<span style="color: #008000;">;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #008080; font-style: italic;">// Finally, adjust the new width</span>
			comboBox.<span style="color: #0000FF;">DropDownWidth</span> <span style="color: #008000;">=</span> width<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span>  <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>   
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The following picture shows the results of using the above control instead of the default one:
</p>
<div id="attachment_90" class="wp-caption aligncenter" style="width: 246px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/image003.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/image003.png" alt="Non Cropped ComboBox" title="Non Cropped ComboBox" width="236" height="123" class="size-medium wp-image-90" /></a><p class="wp-caption-text">Non Cropped ComboBox</p></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2008%2F11%2F18%2Ffull-view-of-combobox-drop-down-list-components-in-c-30%2F&amp;linkname=Full%20view%20of%20ComboBox%20drop-down%20list%20components%20in%20C%23%203.0" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2008_2F11_2F18_2Ffull-view-of-combobox-drop-down-list-components-in-c-30_2F_amp_linkname=Full_20view_20of_20ComboBox_20drop-down_20list_20components_20in_20C_23_203.0&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2008/11/18/full-view-of-combobox-drop-down-list-components-in-c-30/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# and the StringBuilder class</title>
		<link>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/</link>
		<comments>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 17:07:37 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=67</guid>
		<description><![CDATA[This morning I was working on a project at work. It&#8217;s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: &#8220;are you [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I was working on a project at work. It&#8217;s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: &#8220;<em>are you using a StringBuilder to use those strings?</em>&#8220;. And I replied with this answer: &#8220;<em>no, I am not</em>&#8220;. This kind of stupid dialog came over because last week we were discussing about using StringBuilders instead of the default String class operators to append strings each other in Java. It seemed using the StringBuilder class resulted in an overall performance gain. It was then when I asked: &#8220;<em>don&#8217;t tell me this happens with C#, too?</em>&#8220;. And he answered: &#8220;<em>yes, it does!</em>&#8220;.</p>
<p>So, what&#8217;s the matter with StringBuilders in C#?</p>
<p><span id="more-67"></span></p>
<p>It seems the same thing happens with Strings, C# and Java. Here is a copy paste from the <a href="http://msdn2.microsoft.com/en-us/library/2839d5h5%28vs.80%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/2839d5h5_28vs.80_29.aspx?referer=');">MSDN web page</a> about the StringBuilder usage:</p>
<blockquote><p>
The <strong>String</strong> object is immutable. Every time you use one of the methods in the <strong>System.String</strong> class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new <strong>String</strong> object can be costly. The <a href="http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder%28VS.80%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/system.text.stringbuilder_28VS.80_29.aspx?referer=');">System.Text.StringBuilder</a> class can be used when you want to modify a string without creating a new object. For example, using the <strong>StringBuilder</strong> class can boost performance when concatenating many strings together in a loop.
</p></blockquote>
<p>
So I decided to run some tests to see how much it was worth to change my code to use the StringBuilder class, because I use the string &#8216;+&#8217; operator a lot in my program, and the results are simply amazing. Here you can find a chart comparing the times it took to concatenate a given number of strings using both methods. See for yourself and then&#8230; use StringBuilders from now on!
</p>
<div id="attachment_68" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/string.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/string-300x180.png" alt="String usage" title="String usage" width="300" height="180" class="size-medium wp-image-68" /></a><p class="wp-caption-text">String usage</p></div>
<div id="attachment_69" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/stringbuilder.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/stringbuilder-300x180.png" alt="StringBuilder usage" title="StringBuilder usage" width="300" height="180" class="size-medium wp-image-69" /></a><p class="wp-caption-text">StringBuilder usage</p></div>
<p>
Here&#8217;s the source code i used if you want to try it for yourself:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Adapdev.Diagnostics</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> SBuilder
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">class</span> Program
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            HiPerfTimer timer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HiPerfTimer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">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> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                StringBuilder sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                timer.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">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> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&lt;</span>strong<span style="color: #008000;">&gt;</span> i<span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>j<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                timer.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">double</span> timeStringBuilder <span style="color: #008000;">=</span> timer.<span style="color: #0000FF;">Duration</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #FF0000;">string</span> s <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
                timer.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">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> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&lt;/</span>strong<span style="color: #008000;">&gt;</span> i<span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    s <span style="color: #008000;">+=</span> j<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                timer.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">double</span> timeString <span style="color: #008000;">=</span> timer.<span style="color: #0000FF;">Duration</span><span style="color: #008000;">;</span>
&nbsp;
                StringBuilder line <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1000</span> <span style="color: #008000;">*</span> i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>timeStringBuilder<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>timeString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>line.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
You can download the external timer classes from <a href="http://www.codeproject.com/csharp/highperformancetimercshar.asp" onclick="pageTracker._trackPageview('/outgoing/www.codeproject.com/csharp/highperformancetimercshar.asp?referer=');">The Code Project</a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2008%2F07%2F16%2Fc-and-the-stringbuilder-class%2F&amp;linkname=C%23%20and%20the%20StringBuilder%20class" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2008_2F07_2F16_2Fc-and-the-stringbuilder-class_2F_amp_linkname=C_23_20and_20the_20StringBuilder_20class&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Double Check Design Pattern</title>
		<link>http://thinkingeek.com/2007/07/29/the-double-check-design-pattern/</link>
		<comments>http://thinkingeek.com/2007/07/29/the-double-check-design-pattern/#comments</comments>
		<pubDate>Sun, 29 Jul 2007 17:21:37 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=74</guid>
		<description><![CDATA[One of the deficiencies of actual programming languages, specially those ones still widely used that are old, such as C or C++, is that they were designed having in mind the sequential programming paradigm. This usually means that those languages don&#8217;t have standard ways to work with multithreading features, and you usually have to rely [...]]]></description>
			<content:encoded><![CDATA[<p>One of the deficiencies of actual programming languages, specially those ones still widely used that are old, such as C or C++, is that they were designed having in mind the sequential programming paradigm. This usually means that those languages don&#8217;t have standard ways to work with multithreading features, and you usually have to rely on third party libraries to develop thread safe software.</p>
<p>Today I&#8217;ll be discussing a design pattern called <em>Double Check</em>, which can be widely used to manage the resource access, elimination and initialization in a safe thread way.</p>
<p><span id="more-74"></span></p>
<p>Consider another common design pattern: the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Singleton_pattern?referer=');">Singleton Pattern</a>. A singleton is a special class which is allowed to have one and only one instance in a program. This pattern can be used in a variety of ways, one of the most usual one is to store global program information, which, as its name states, is global and unique to the application, so having more than one instance of that class would have the burden to mantain them synchronized, or even more nasty consequences.</p>
<p>There are different ways to implement a singleton class. I will focus on C#, but the example shown here could be easily extended to other languages such as C++ or Java. The trick on the pattern consists on declaring the class constructor private. This way we can be sure no one will be creating new instances of that class at will. After having done this, we have to provide a public method, which usually has the name getInstance, that will be the one actually creating this item and returning it to the caller.</p>
<p>The code would look similar to this one:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> Singleton
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Singleton instance <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Singleton Instance
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Another way to create this singleton class would be doing the instantiation of the private variable on the class itself and not on the <em>getInstance</em> method, but this second way gives us a <em>lazy initialization</em>, so the variable is actually instantiated only when it&#8217;s needed, and never before (who knows, maybe we will never need it in some circumstances).</p>
<p>This implementation is perfect&#8230; as long as this method isn&#8217;t called twice (or more) at the same time. Consider what would happen if two threads requested an instance of the singleton class: both threads would check for the instance being null, and both threads would create the instance. That would sure lead to an unknown behaviour, and more than one instance of the class would be in the program, which is exactly what we wanted to avoid.</p>
<p>The first solution that comes in mind is to lock the method so multiple threads must wait to enter the method and get the singleton instance. It would look like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> Singleton
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Singleton instance <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> sempahore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Singleton Instance
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span>semaphore<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
This way we ensure that if n threads try to access the routine at the same time, only one will be initializing the singleton instance, since when the others gain the access to the method again, that instance won&#8217;t be null anymore. However, even if this solution is valid and works, it has a serious performance problem. Consider the situation where the instance has been already created. Then more than one thread access the routine again to get that instance. What will happen is that those threads will have to block and wait for other threads to finish, even if the instance is not null, so in any case will be created again. Conclusion: we are blocking threads that don&#8217;t need to be blocked. &#8220;<em>Ok, you may say, we will put the lock inside the if clause, and we&#8217;ll have the issue resolved</em>&#8220;. Let&#8217;s take a look at this solution:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> Singleton
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Singleton instance <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> sempahore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Singleton Instance
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span>semaphore<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
You don&#8217;t have to be a genious to see that this is not working, either. It will work after the instance has been created, because the threads won&#8217;t be entering the if clause, and they won&#8217;t be blocked. But if we are in the same situation as the described in the first place &#8211;this is, more than one thread entering the method before the instance has been created&#8211;, we have the same problem we had before: the singleton will be created more than once.</p>
<p>
The final solution is simple: check again for the variable being null! This is why this pattern is called <em>Double Check</em>. The final code would look like this one:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> Singleton
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Singleton instance <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> sempahore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Singleton Instance
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span>semaphore<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>instance <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
This way, we allow the threads to pass by the if clause if the instance has been already instantiated, and if it has not, the threads will get blocked, and the first one to gain access to the method will do the instantiation. Further threads will check again against the instance being null or not, so it won&#8217;t be created again.
</p>
<p>
Altough this pattern may be implemented similarly in other languages, be aware that, as I said before, a lot of them were never intended to deal with multithreading, and that compiler optimizations may give you code which is not really thread safe, or that actually is not optimized. Here you have a link discussing this issue with Java and C++: <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html" onclick="pageTracker._trackPageview('/outgoing/www.cs.umd.edu/_pugh/java/memoryModel/DoubleCheckedLocking.html?referer=');">http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html</a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2007%2F07%2F29%2Fthe-double-check-design-pattern%2F&amp;linkname=The%20Double%20Check%20Design%20Pattern" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2007_2F07_2F29_2Fthe-double-check-design-pattern_2F_amp_linkname=The_20Double_20Check_20Design_20Pattern&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2007/07/29/the-double-check-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
