<?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>Coffee, Code and Chocolate</title>
	<atom:link href="http://blog.ganeshzone.net/blog/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ganeshzone.net/blog</link>
	<description>throw new Exception(&#34;#fail&#34;);</description>
	<lastBuildDate>Mon, 08 Mar 2010 19:36:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Blogging: Length v/s Popularity</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/03/blogging-length-vs-popularity/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/03/blogging-length-vs-popularity/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 19:36:07 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Blogging]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=1010</guid>
		<description><![CDATA[<p style="text-align: justify;">I have always believed that when it comes to blogging, brevity is soul and its very important to convey your idea in as less words as possible. Of course with technical blogs its an altogether different ball game with most of the visitors coming through search engines and looking for specific topics, where [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I have always believed that when it comes to blogging, brevity is soul and its very important to convey your idea in as less words as possible. Of course with technical blogs its an altogether different ball game with most of the visitors coming through search engines and looking for specific topics, where you can be afford to be verbose. But with a mixed audience and a variety of blog subjects, it is extremely important to keep it as short and simple as possible to ensure maximum reach.</p>
<p style="text-align: justify;">To prove this point, I set out to find if there is a relation between the length of each post and the comments it receives. Though, the number of comments is not the best yardstick to measure the popularity of the post, it certainly is the most concrete one. The best place to try this out seemed to be my organization&#8217;s internal blogosphere, which had a lot of sample data (3500+ posts). Since its impossible to manually collect data for that many posts, I wrote a program to write it to a text file from where any correlation could be identified. Its a mixture of retrieving the data through HTML and RSS and parsing it. Though it should be compatible with any WordPress version, the comment counting function might need some tweaking to make it work on later versions. Once the code finishes executing, you would have a text file with the name of each post, the length and the number of comments. The data in this file can be imported into Excel for further manipulation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td 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.Xml</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TestRSS
<span style="color: #000000;">&#123;</span>
    StreamWriter _swObj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Results.txt&quot;</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> _feedURL <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://blog.ganeshzone.net&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008000;">new</span>  TestRSS<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">RunTests</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> RunTests<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</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;">1</span><span style="color: #008000;">;</span> <span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//This is an infinite loop only to be broken when there are no more posts</span>
        <span style="color: #000000;">&#123;</span>
            XmlDocument _xdoc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//a New XmlDocument object</span>
            <span style="color: #008080; font-style: italic;">//Lets load the XML from the url</span>
            _xdoc.<span style="color: #0000FF;">LoadXml</span><span style="color: #000000;">&#40;</span>GetXmlData<span style="color: #000000;">&#40;</span>_feedURL <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>i<span style="color: #008000;">&gt;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">?</span><span style="color: #666666;">&quot;?paged=&quot;</span> <span style="color: #008000;">+</span> i<span style="color: #008000;">+</span><span style="color: #666666;">&quot;&amp;&quot;</span><span style="color: #008000;">:</span><span style="color: #666666;">&quot;?&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #666666;">&quot;feed=rss2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            XmlNodeList _xList <span style="color: #008000;">=</span> _xdoc.<span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;item&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_xList.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                break<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//This means there are no more blog posts</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>XmlNode _tempNode <span style="color: #0600FF;">in</span> _xList<span style="color: #000000;">&#41;</span>
                <span style="color: #008080; font-style: italic;">//This method writes the name of post, length and the number of comments delimited by a colon</span>
                <span style="color: #008080; font-style: italic;">//So we need to remove the colon in case of any being present/</span>
                <span style="color: #008080; font-style: italic;">//Example Data: - ASP.NET Evolution WebForms v/s MVC: 6060:0</span>
                WriteToFile<span style="color: #000000;">&#40;</span>_tempNode.<span style="color: #0000FF;">FirstChild</span>.<span style="color: #0000FF;">InnerText</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;:&quot;</span>, <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;: &quot;</span> <span style="color: #008000;">+</span> _tempNode.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;description&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NextSibling</span>.<span style="color: #0000FF;">InnerText</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;:&quot;</span> <span style="color: #008000;">+</span> GetComments<span style="color: #000000;">&#40;</span>_tempNode.<span style="color: #0000FF;">FirstChild</span>.<span style="color: #0000FF;">NextSibling</span>.<span style="color: #0000FF;">InnerText</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>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetXmlData<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> _url<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//An ordinary retrieval of data from the url using the HttpWebRequest</span>
        <span style="color: #008080; font-style: italic;">//This is a workaround for sites with badly formed RSS feeds due to script tags.</span>
        <span style="color: #008080; font-style: italic;">//Once we get the data, we can load it into an XmlDocument class</span>
        HttpWebRequest _blogReq <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebRequest<span style="color: #000000;">&#41;</span>WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>_url<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        HttpWebResponse _blogResp <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>_blogReq.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        StreamReader _respStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>_blogResp.<span style="color: #0000FF;">GetResponseStream</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: #008080; font-style: italic;">//Replace Script tags</span>
        <span style="color: #0600FF;">return</span> Regex.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span>_respStream.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #666666;">@&quot;&lt;script[^&gt;]*?&gt;[\s\S]*?&lt;\/script&gt;&quot;</span>, <span style="color: #666666;">&quot;&quot;</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> GetComments<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> _url<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//Retrieving the Comments from the HTML and not the RSS feed. I wasnt able</span>
        <span style="color: #008080; font-style: italic;">//to find a workaround for 10 comment limit in the RSS feed in wordpress 2.3,</span>
        <span style="color: #008080; font-style: italic;">//Hence retriving the HTML and matching the comment divs. Keep in mind this wont work</span>
        <span style="color: #008080; font-style: italic;">//for higher Wordpress versions. Just take the comment tag and substitute accordingly</span>
        Match _match <span style="color: #008000;">=</span> Regex.<span style="color: #0000FF;">Match</span><span style="color: #000000;">&#40;</span>GetXmlData<span style="color: #000000;">&#40;</span>_url<span style="color: #000000;">&#41;</span>, <span style="color: #666666;">&quot;&lt;div class=<span style="color: #008080; font-weight: bold;">\&quot;</span>mycomment<span style="color: #008080; font-weight: bold;">\&quot;</span>[^&gt;]*?&gt;[<span style="color: #008080; font-weight: bold;">\\</span>s<span style="color: #008080; font-weight: bold;">\\</span>S]*?&lt;<span style="color: #008080; font-weight: bold;">\\</span>/div&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> _counter<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">;</span> _match.<span style="color: #0000FF;">Success</span><span style="color: #008000;">;</span> _counter<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            _match <span style="color: #008000;">=</span> _match.<span style="color: #0000FF;">NextMatch</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> _counter<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> WriteToFile<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> _tobeWritten<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//Just write it to the file</span>
        _swObj.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>_tobeWritten<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _swObj.<span style="color: #0000FF;">Flush</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></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/03/blogging-length-vs-popularity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lifting the .NET Hood &#8211; Part 1 &#8211; Generics</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/03/lifting-the-net-hood-part-1-generics/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/03/lifting-the-net-hood-part-1-generics/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 21:33:39 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=966</guid>
		<description><![CDATA[<p style="text-align: justify;">I have started this series of posts dedicated to exploring the internals of the .NET framework and runtime. To get started on probing of internals of .NET, we need a debugging extension called SOS (Sons of Strike). Visual Studio comes packaged with this assembly (SOS.dll). The first posts is about one of the [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I have started this series of posts dedicated to exploring the internals of the .NET framework and runtime. To get started on probing of internals of .NET, we need a debugging extension called SOS (Sons of Strike). Visual Studio comes packaged with this assembly (SOS.dll). The first posts is about one of the most important features of .NET 2.0 over 1.1 &#8211; Generics or &#8220;Parametric Polymorphism&#8221;. Wikipedia defines Generics as <em>&#8220;a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters&#8221;</em>.</p>
<p style="text-align: justify;">First, we need to set up Visual studio  to use the Sons of Strike. A few simple steps mentioned below are needed to accomplish this:-</p>
<ul>
<li>Enable Unmanaged Debugging in the Project Properties</li>
</ul>
<p><a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/03/unmanaged.jpg"><img class="aligncenter size-medium wp-image-967" title="unmanaged" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/03/unmanaged-300x183.jpg" alt="" width="300" height="183" /></a></p>
<ul>
<li style="text-align: justify;">Set the symbol server to  <strong>*C:\localcache*http://msdl.microsoft.com/download/symbols</strong>. The symbol server can be set by going to Tools -&gt; Options-&gt; Debugging. This step allows Visual studio to download the symbols needed for debugging from the Microsoft server.</li>
</ul>
<ul>
<li style="text-align: justify;">Set a breakpoint in the code and press F5 to start the program in debug mode. Once the application hits the breakpoint, load the SOS.dll using the command <strong>.load SOS </strong>in the Immediate Window.</li>
</ul>
<p><a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/03/loadDLL.jpg"><img class="aligncenter size-full wp-image-971" title="loadDLL" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/03/loadDLL.jpg" alt="" width="525" height="71" /></a></p>
<p style="text-align: justify;">Now we are ready to use the debugging extension. And SOS is also compatible with debugging tools like WinDbg in case you are comfortable with using that. Click <a href="http://geekswithblogs.net/.netonmymind/archive/2006/03/14/72262.aspx" target="_blank">here</a> for a list of SOS commands.</p>
<p style="text-align: justify;">Now, lets see the code that we would use for seeing how generics work. It consists of a Generic Type Customer which contains a list of the generic type and a public integer property.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Employee
    <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> EmployeeID<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> age<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Name<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #FF0000;">class</span> Customer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> CustomerID<span style="color: #008000;">;</span>
        List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> _innerList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> ListItems <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _innerList<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddItems<span style="color: #000000;">&#40;</span>T _addObject<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _innerList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_addObject<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">The main method to call this code is below. We create the Customer class in three types &#8211; a string, an integer and a custom Employee type. Of these the string and Employee are reference types while integer is a primitive value type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><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>
            Customer<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> _cxStringObj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Customer<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> _cxIntObj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Customer<span style="color: #008000;">&lt;</span>Employee<span style="color: #008000;">&gt;</span> _cxEmpObj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer<span style="color: #008000;">&lt;</span>Employee<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            _cxStringObj.<span style="color: #0000FF;">CustomerID</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">34</span><span style="color: #008000;">;</span>
            _cxStringObj.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Ganesh&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            _cxIntObj.<span style="color: #0000FF;">CustomerID</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">23</span><span style="color: #008000;">;</span>
            _cxIntObj.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">43</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            _cxEmpObj.<span style="color: #0000FF;">CustomerID</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">34</span><span style="color: #008000;">;</span>
            _cxEmpObj.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Employee<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Every .NET class has two main data structures it uses to maintain type identity  &#8211; the MethodTable and EEClass. Every object has a pointer to its MethodTable, and this methodtable contains a pointer to the EEClass. In .NET 1.1, the EE class was the strongest identifier of Type Identity of an object, however with .NET 2.0 and the implementation of Generics, Generic Types can share the same EEClass, and the MethodTable takes on the mantle of being the unique type identifier.</p>
<p style="text-align: justify;">There are two main ways of implementing Generics which were proposed by the researchers who were working at Microsoft (<a href="http://research.microsoft.com/en-us/um/people/akenn/generics/DesignAndImplementationOfGenerics.pdf">This</a> paper provides details of the implementation).</p>
<ul>
<li style="text-align: justify;">Expansion: In this way, whenever the runtime encounters a generic type, the code could be generated on the fly for the type being called. For e.g. in our code, when the object Customer&lt;string&gt; was encountered, it could have expanded into the string representation of the object. When the type Customer&lt;int&gt; was encountered, a similar expansion can be done for it.</li>
</ul>
<ul>
<li style="text-align: justify;">Code sharing: In this approach only a single instantiation is done for the generic type, and it is reused for all the calls.</li>
</ul>
<p style="text-align: justify;">The method chosen in the end was a mixture of both these approaches. Generic types share as much code as possible They contain the same EEClass as mentioned before, but also have distinct MethodTables. Even though they have distinct MethodTables, code is shared between compatible types (all reference types).</p>
<p style="text-align: justify;">Fire up the debugger and load the SOS.dll. To see the particular instances of the type on the heap give the Command DumpHeap -type . Once all the three objects have been created, lets see the result of this command.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">!</span>DumpHeap <span style="color: #008000;">-</span>type Customer
 Address       MT     Size
01ed35cc 002331d8       <span style="color: #FF0000;">16</span>
01ed3604 00233284       <span style="color: #FF0000;">16</span>
01ed3638 0023336c       <span style="color: #FF0000;">16</span>
total <span style="color: #FF0000;">3</span> objects
Statistics<span style="color: #008000;">:</span>
      MT    Count    TotalSize <span style="color: #FF0000;">Class</span> Name
0023336c        <span style="color: #FF0000;">1</span>           <span style="color: #FF0000;">16</span> GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span>GenericsTry.<span style="color: #0000FF;">Employee</span>, GenericsTry<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>
00233284        <span style="color: #FF0000;">1</span>           <span style="color: #FF0000;">16</span> GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Int32</span>, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>
002331d8        <span style="color: #FF0000;">1</span>           <span style="color: #FF0000;">16</span> GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #FF0000;">String</span>, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>
Total <span style="color: #FF0000;">3</span> objects</pre></div></div>

<p style="text-align: justify;">As you can see from the MT column, all the three objects have different MethodTables. Lets see the MethodTables for each of the three type instantiations.</p>
<p style="text-align: justify;">The command is DumpMT -MD . First the Employee type. Think of the MethodTable as an extension to a conventional vtable which simply contains the method pointers. The MethodTable&#8217;s vtable points to 7 slots, 4 of which are inherited from the parent Object class and three which are defined by the object itself. If you see the JIT status, there is a YES and NONE. The NONE means the method has never been called by the JIT Compiler and it points to a temporary stub. The first time the method is called, it is compiled on the fly and the temporary stub is replaced with the actual method pointer. To optimize performance, JIT defers execution and compilation till as late as possible.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">!</span>DumpMT <span style="color: #008000;">-</span>MD 0023336c
EEClass<span style="color: #008000;">:</span> 00231420
Module<span style="color: #008000;">:</span> 00232c5c
Name<span style="color: #008000;">:</span> GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span>GenericsTry.<span style="color: #0000FF;">Employee</span>, GenericsTry<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>
mdToken<span style="color: #008000;">:</span> 02000004  <span style="color: #000000;">&#40;</span>Projects\GenericsTry\GenericsTry\bin\Debug\GenericsTry.<span style="color: #0000FF;">exe</span><span style="color: #000000;">&#41;</span>
BaseSize<span style="color: #008000;">:</span> 0x10
ComponentSize<span style="color: #008000;">:</span> 0x0
Number of IFaces <span style="color: #0600FF;">in</span> IFaceMap<span style="color: #008000;">:</span> <span style="color: #FF0000;">0</span>
Slots <span style="color: #0600FF;">in</span> VTable<span style="color: #008000;">:</span> <span style="color: #FF0000;">7</span>
<span style="color: #008000;">--------------------------------------</span>
MethodDesc Table
   Entry MethodDesc      JIT Name
70766aa0   705e4a34   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
70766ac0   705e4a3c   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span><span style="color: #000000;">&#41;</span>
70766b30   705e4a6c   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
707d7550   705e4a90   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">Finalize</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
0023c058   0023313c     NONE GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">get_ListItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
0023c060   00233148     NONE GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.__Canon<span style="color: #000000;">&#41;</span>
0023c068   00233154      JIT GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>..<span style="color: #0000FF;">ctor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p style="text-align: justify;">Lets see the MethodTable for the integer type. As you can see the EEClass for the Integer and Employee types are not same, this is because they are not compatible types and hence no code sharing can take place</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">!</span>DumpMT <span style="color: #008000;">-</span>MD 00233284
EEClass<span style="color: #008000;">:</span> 002314b4
Module<span style="color: #008000;">:</span> 00232c5c
Name<span style="color: #008000;">:</span> GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Int32</span>, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>
mdToken<span style="color: #008000;">:</span> 02000004  <span style="color: #000000;">&#40;</span>\GenericsTry\GenericsTry\bin\Debug\GenericsTry.<span style="color: #0000FF;">exe</span><span style="color: #000000;">&#41;</span>
BaseSize<span style="color: #008000;">:</span> 0x10
ComponentSize<span style="color: #008000;">:</span> 0x0
Number of IFaces <span style="color: #0600FF;">in</span> IFaceMap<span style="color: #008000;">:</span> <span style="color: #FF0000;">0</span>
Slots <span style="color: #0600FF;">in</span> VTable<span style="color: #008000;">:</span> <span style="color: #FF0000;">7</span>
<span style="color: #008000;">--------------------------------------</span>
MethodDesc Table
   Entry MethodDesc      JIT Name
70766aa0   705e4a34   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
70766ac0   705e4a3c   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span><span style="color: #000000;">&#41;</span>
70766b30   705e4a6c   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
707d7550   705e4a90   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">Finalize</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
0023c080   00233254     NONE GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Int32</span>, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">get_ListItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
0023c088   00233260     NONE GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Int32</span>, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span>Int32<span style="color: #000000;">&#41;</span>
0023c090   0023326c      JIT GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Int32</span>, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>..<span style="color: #0000FF;">ctor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p style="text-align: justify;">The last one is the string type. This brings us to interesting conclusions. Compare the first MethodTable with the last one. Both are reference types and hence compatible with other. It can be observed that the string and Employee tables have the same EEClass <strong>00231420</strong>. The integer type however has the EEClass <strong>002314b4</strong>. There is no way for primitive value types to share code with each other since they differ in size. For e.g. If we created a bool type for Customer class, it would still have a different EEClass and MethodTables.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">!</span>DumpMT <span style="color: #008000;">-</span>MD 002331d8
EEClass<span style="color: #008000;">:</span> 00231420
Module<span style="color: #008000;">:</span> 00232c5c
Name<span style="color: #008000;">:</span> GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #FF0000;">String</span>, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>
mdToken<span style="color: #008000;">:</span> 02000004  <span style="color: #000000;">&#40;</span>\GenericsTry\GenericsTry\bin\Debug\GenericsTry.<span style="color: #0000FF;">exe</span><span style="color: #000000;">&#41;</span>
BaseSize<span style="color: #008000;">:</span> 0x10
ComponentSize<span style="color: #008000;">:</span> 0x0
Number of IFaces <span style="color: #0600FF;">in</span> IFaceMap<span style="color: #008000;">:</span> <span style="color: #FF0000;">0</span>
Slots <span style="color: #0600FF;">in</span> VTable<span style="color: #008000;">:</span> <span style="color: #FF0000;">7</span>
<span style="color: #008000;">--------------------------------------</span>
MethodDesc Table
   Entry MethodDesc      JIT Name
70766aa0   705e4a34   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
70766ac0   705e4a3c   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span><span style="color: #000000;">&#41;</span>
70766b30   705e4a6c   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
707d7550   705e4a90   PreJIT <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Object</span>.<span style="color: #0000FF;">Finalize</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
0023c058   0023313c     NONE GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">get_ListItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
0023c060   00233148     NONE GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.__Canon<span style="color: #000000;">&#41;</span>
0023c068   00233154      JIT GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>..<span style="color: #0000FF;">ctor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p style="text-align: justify;">One more thing of notice is the Method Descriptors for the first and third types. The AddItems method in Customer class has the same Method descriptor for both the string and Employee implementation. So even after a MethodTable is duplicated, both those entries still point to the same Method Description. Even the name has the type System.__Canon, which is dynamically replaced by the type on which the method is called. This is an illustration of the second approach of code sharing given by the researchers from Microsoft.</p>
<p style="text-align: justify;">Once the code for AddItems method is called for the first time, it is JIT Compiled and assigned a code address. The Method Descriptor holds this code address which is assembly code and can be viewed by the SOS command !u.  Till the method is Jitted, there is junk value in this field on the MethodDescriptor.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">!</span>DumpMD 00233148
Method Name<span style="color: #008000;">:</span> GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.__Canon<span style="color: #000000;">&#41;</span>
<span style="color: #FF0000;">Class</span><span style="color: #008000;">:</span> 00231420
MethodTable<span style="color: #008000;">:</span> 0023316c
mdToken<span style="color: #008000;">:</span> 06000005
Module<span style="color: #008000;">:</span> 00232c5c
IsJitted<span style="color: #008000;">:</span> yes
CodeAddr<span style="color: #008000;">:</span> 00aa0298</pre></div></div>

<p>Running the CodeAddress would give you a bunch of assembly code that is executed when the method is called.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">!</span>u 00aa0298
Normal JIT generated code
GenericsTry.<span style="color: #0000FF;">Customer</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AddItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.__Canon<span style="color: #000000;">&#41;</span>
Begin 00aa0298, size <span style="color: #FF0000;">42</span>
<span style="color: #008000;">&gt;&gt;&gt;</span> 00AA0298 <span style="color: #FF0000;">55</span>               push        ebp
00AA0299 8BEC             mov         ebp,esp
00AA029B <span style="color: #FF0000;">57</span>               push        edi
00AA029C <span style="color: #FF0000;">56</span>               push        esi
00AA029D <span style="color: #FF0000;">53</span>               push        ebx
00AA029E 83EC34           sub         esp,34h
00AA02A1 33C0             xor         eax,eax
00AA02A3 8945F0           mov         dword ptr <span style="color: #000000;">&#91;</span>ebp<span style="color: #008000;">-</span>10h<span style="color: #000000;">&#93;</span>,eax
00AA02A6 33C0             xor         eax,eax
00AA02A8 8945E4           mov         dword ptr <span style="color: #000000;">&#91;</span>ebp<span style="color: #008000;">-</span>1Ch<span style="color: #000000;">&#93;</span>,eax
00AA02AB 894DC4           mov         dword ptr <span style="color: #000000;">&#91;</span>ebp<span style="color: #008000;">-</span>3Ch<span style="color: #000000;">&#93;</span>,ecx
00AA02AE 8955C0           mov         dword ptr <span style="color: #000000;">&#91;</span>ebp<span style="color: #008000;">-</span>40h<span style="color: #000000;">&#93;</span>,edx
00AA02B1 833D142E230000   cmp         dword ptr ds<span style="color: #008000;">:</span><span style="color: #000000;">&#91;</span>00232E14h<span style="color: #000000;">&#93;</span>,<span style="color: #FF0000;">0</span>
00AA02B8 <span style="color: #FF0000;">7405</span>             je          00AA02BF
00AA02BA E8BABC3571       call        71DFBF79 <span style="color: #000000;">&#40;</span>JitHelp<span style="color: #008000;">:</span> CORINFO_HELP_DBG_IS_JUST_MY_CODE<span style="color: #000000;">&#41;</span>
00AA02BF <span style="color: #FF0000;">90</span>               nop
00AA02C0 8B45C4           mov         eax,dword ptr <span style="color: #000000;">&#91;</span>ebp<span style="color: #008000;">-</span>3Ch<span style="color: #000000;">&#93;</span>
00AA02C3 8B4804           mov         ecx,dword ptr <span style="color: #000000;">&#91;</span>eax<span style="color: #008000;">+</span><span style="color: #FF0000;">4</span><span style="color: #000000;">&#93;</span>
00AA02C6 8B55C0           mov         edx,dword ptr <span style="color: #000000;">&#91;</span>ebp<span style="color: #008000;">-</span>40h<span style="color: #000000;">&#93;</span>
00AA02C9 <span style="color: #FF0000;">3909</span>             cmp         dword ptr <span style="color: #000000;">&#91;</span>ecx<span style="color: #000000;">&#93;</span>,ecx
00AA02CB E8E0F1D16F       call        707BF4B0 <span style="color: #000000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span>.<span style="color: #0000FF;">Generic</span>.<span style="color: #0000FF;">List</span>`<span style="color: #FF0000;">1</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.__Canon, mscorlib<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.__Canon<span style="color: #000000;">&#41;</span>, mdToken<span style="color: #008000;">:</span> 060019f1<span style="color: #000000;">&#41;</span>
00AA02D0 <span style="color: #FF0000;">90</span>               nop
00AA02D1 <span style="color: #FF0000;">90</span>               nop
00AA02D2 8D65F4           lea         esp,<span style="color: #000000;">&#91;</span>ebp<span style="color: #008000;">-</span>0Ch<span style="color: #000000;">&#93;</span>
00AA02D5 5B               pop         ebx
00AA02D6 5E               pop         esi
00AA02D7 5F               pop         edi
00AA02D8 5D               pop         ebp
00AA02D9 C3               ret</pre></div></div>

<p>In this post, we have looked at how the CLR treats Generics and how it takes care of performance implications of Generics code by sharing as much code as possible without having to needlessly box and unbox the types being passed to the Generic classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/03/lifting-the-net-hood-part-1-generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chrome Extension for displaying Vancouver Olympics Medals tally</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/02/chrome-extension-for-displaying-vancouver-olympics-medals-tally/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/02/chrome-extension-for-displaying-vancouver-olympics-medals-tally/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 18:07:48 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Chrome]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=949</guid>
		<description><![CDATA[<p style="text-align: justify;">The Vancouver Olympics has been generating considerable bit of interest with everyone speculating on the country which bags the most number of golds. To make it easier to keep up to date with the medals tally, I wrote a Chrome extension which displays the top 10 countries. It can be downloaded at the chrome site here.</p>
<p [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The Vancouver Olympics has been generating considerable bit of interest with everyone speculating on the country which bags the most number of golds. To make it easier to keep up to date with the medals tally, I wrote a Chrome extension which displays the top 10 countries. It can be downloaded at the chrome site <a href="https://chrome.google.com/extensions/detail/bpohbjolihanngodhglbobaodakjocdp">here</a>.</p>
<p style="text-align: justify;">As with the Twitter Trends, I wasn&#8217;t able to find any RSS feed which gives the details of the medals. So I had to scrape the HTML page for the data and then dynamically create the table and display it. The <strong>manifest.json</strong> file gives the details of the gadget.</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{
  &quot;name&quot;: &quot;Vancouver Medals Tally&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;description&quot;: &quot;Displays latest medals tally from Vancouver 2010 olympics&quot;,
  &quot;icons&quot;: { &quot;128&quot;: &quot;icon.png&quot; },
  &quot;browser_action&quot;: {
    &quot;default_title&quot;: &quot;Vancouver Medals Tally&quot;,
    &quot;default_icon&quot;: &quot;icon.png&quot;,
    &quot;popup&quot;: &quot;Background.html&quot;
  },
  &quot;permissions&quot;: [
    &quot;tabs&quot;,
    &quot;http://www.vancouver2010.com/*&quot;
  ]
}</pre></div></div>

<p style="text-align: justify;">The stylesheet gives the basic styling of the table and the banner above it. The banner has two states, one where the gadget is loading and hence its smaller in size. After the gadget increases in size, the banner must also be made to fit. This is done in CSS. The switching is done through javascript.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">  body <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> helvetica<span style="color: #00AA00;">,</span> arial<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#title</span>
  <span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#mainDiv</span>
  <span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">min-width</span><span style="color: #00AA00;">:</span><span style="color: #933;">205px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
th <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span> <span style="color: #933;">11px</span> <span style="color: #ff0000;">&quot;Trebuchet MS&quot;</span><span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span>
	<span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#6D929B</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#C1DAD7</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#C1DAD7</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#C1DAD7</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">letter-spacing</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-transform</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">uppercase</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">6px</span> <span style="color: #933;">6px</span> <span style="color: #933;">6px</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#CAE8EA</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
  td
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#C1DAD7</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#C1DAD7</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #933;">5px</span> <span style="color: #933;">5px</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#6D929B</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
   <span style="color: #cc00cc;">#bannerDiv</span><span style="color: #6666ff;">.expanded</span>
   <span style="color: #00AA00;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">18px</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">middle</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">padding-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
   <span style="color: #00AA00;">&#125;</span>
&nbsp;
   <span style="color: #cc00cc;">#bannerDiv</span><span style="color: #6666ff;">.collapsed</span>
   <span style="color: #00AA00;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">middle</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">padding-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
       <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">15px</span><span style="color: #00AA00;">;</span>
   <span style="color: #00AA00;">&#125;</span>
&nbsp;
   <span style="color: #cc00cc;">#bannerDiv</span><span style="color: #6666ff;">.collapsed</span> img
   <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
   <span style="color: #00AA00;">&#125;</span>
&nbsp;
    <span style="color: #cc00cc;">#bannerDiv</span><span style="color: #6666ff;">.expanded</span> img
   <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">28px</span><span style="color: #00AA00;">;</span>
        <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">56px</span><span style="color: #00AA00;">;</span>
   <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: justify;">In the JavaScript, we retrieve the HTML table from the website, create a table using DOM and populate it with the data and append it to the main div. The challenge here was to parse the HTML page into the XML DOM structure to allow JavaScript manipulation. Chromer and Firefox support a class called DOMParser which can be used to construct a DOM structure from the AJAX response text. However, this was giving an error because of the script tags. After much googling, I found a solution at Stack Overflow where the user had removed the data between script tags and then parsed the document. This worked!! and once we the data could be manipulated, all that remained was creating a HTML table and appending the proper rows to the table.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Developer  : Ganesh Ranganathan
    * Date        : 02/15/2010
    * Description : This is a Google Chrome Extension created to display latest medals
                    tally for Vancouver olympic games 2010
    * Disclaimer  : I have checked that this extension doesnt violate any copyrights
                    However, if you feel otherwise please point me towards the documentation
                    and I will take corrective measures */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> req<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> _feedURL <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.vancouver2010.com/olympic-medals/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//Call the loading method after a slight delay to allow</span>
    <span style="color: #006600; font-style: italic;">//Chrome to load the extension body. Else it looks ugly with</span>
    <span style="color: #006600; font-style: italic;">//the pressed button waiting for the feed to load.</span>
    setTimeout<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;delayedCall()&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> delayedCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    _req <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//Hooking up the readystate changed event handler.</span>
    _req.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_req.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">&amp;&amp;</span> _req.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">//Come here only if all data is loaded.</span>
            <span style="color: #003366; font-weight: bold;">var</span> doc <span style="color: #339933;">=</span> _req.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">//This workaround is for parsing the HTML string into</span>
            <span style="color: #006600; font-style: italic;">//an xml Dom. The script tags are removed from the HTML</span>
            <span style="color: #006600; font-style: italic;">//since they cause a lot of parser errors</span>
            <span style="color: #003366; font-weight: bold;">var</span> _tempDiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             _tempDiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> doc.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&lt;script (.|\s)*?\/script&gt;/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">//Get medals table - There is one 1</span>
            <span style="color: #003366; font-weight: bold;">var</span> medalsTable <span style="color: #339933;">=</span> _tempDiv.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> tableRows <span style="color: #339933;">=</span> medalsTable<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sortMe'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">var</span> mainDiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mainDiv'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            mainDiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//resetting existing html to null</span>
            <span style="color: #003366; font-weight: bold;">var</span> table <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            table.<span style="color: #660066;">cssClass</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'medalsTally'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Setting CSS Class</span>
            <span style="color: #003366; font-weight: bold;">var</span> tbody <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tbody'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            table.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>LoadHeader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> tableRows.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">//Iterate through each of the table nodes </span>
                <span style="color: #006600; font-style: italic;">//and create a new table for displaying in the extension</span>
                <span style="color: #003366; font-weight: bold;">var</span> _childNodes <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span>tableRows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'c2'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nextSibling</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'td'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'40%'</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span>tableRows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'c3'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'td'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'15%'</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span>tableRows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'c4'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'td'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'15%'</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span>tableRows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'c5'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'td'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'15%'</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span>tableRows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'c6'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'td'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _childNodes<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'15%'</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">//Appending everything in the array to the row</span>
                <span style="color: #003366; font-weight: bold;">var</span> _tempRow <span style="color: #339933;">=</span> AppendAllData<span style="color: #009900;">&#40;</span>_childNodes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                tbody.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>_tempRow<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #006600; font-style: italic;">//append body to table</span>
            table.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>tbody<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">//Changing Banner size</span>
            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bannerDiv'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'expanded'</span><span style="color: #339933;">;</span>
            mainDiv.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>table<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">//Without these two the AJAX request would never get sent.</span>
    _req.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> _feedURL<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _req.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//Helper function ofr loading the header cells</span>
<span style="color: #003366; font-weight: bold;">function</span> LoadHeader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> thead <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'thead'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> headerRow <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tr'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> countryName <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'CountryName'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'th'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> gold <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Gold'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'th'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> silver <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Silver'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'th'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> bronze <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Bronze'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'th'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> total <span style="color: #339933;">=</span> createTableCell<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Total'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'th'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    headerRow.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>countryName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    headerRow.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>gold<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    headerRow.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>silver<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    headerRow.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>bronze<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    headerRow.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>total<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    thead.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>headerRow<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> thead<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> createTableCell<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span>tag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _tempCell <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span>tag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _txtNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _tempCell.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>_txtNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> _tempCell<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> AppendAllData<span style="color: #009900;">&#40;</span>_childNodes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> _tempRow <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tr'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> _childNodes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        _tempRow.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>_childNodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> _tempRow<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The HTML is just two div tags &#8211; one for the content and one for the banner. Please download the extension and let me know your valuable feedback</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/02/chrome-extension-for-displaying-vancouver-olympics-medals-tally/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing a Google Chrome extension for displaying Twitter trending topics</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/02/writing-a-google-chrome-extension-for-displaying-twitter-trending-topics/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/02/writing-a-google-chrome-extension-for-displaying-twitter-trending-topics/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 21:13:07 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Chrome]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=922</guid>
		<description><![CDATA[<p style="text-align: justify;">Google Chrome recently opened up their extensions API. Though Chrome had supported extensions quite early on in its lifecycle, the API was still a work in progress and one needed to open Chrome in the developer mode to play around. A few weeks back, Chrome announced that extensions are now enabled in normal [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Google Chrome recently opened up their extensions API. Though Chrome had supported extensions quite early on in its lifecycle, the API was still a work in progress and one needed to open Chrome in the developer mode to play around. A few weeks back, Chrome announced that extensions are now enabled in normal mode as well.</p>
<p style="text-align: justify;">So I decided to poke around the API and write a simple extension. Though it doesnt compare to FireFox yet with much of the browser still not accessible, but I am sure future versions will bring much better customization capability. The extension I wrote is very simple and just displays the latest trending topics from twitter. The source can be downloaded <a href="http://www.ganeshzone.net/Files/TrendingTopics.zip">here </a> and the packaged version <a href="http://www.ganeshzone.net/Files/TrendingTopics.crx">here</a>.</p>
<p>Lets see the anatomy of a Chrome Extension. An Extension consists of three main parts</p>
<ul>
<li>Manifest</li>
<li>UI</li>
<li>All your other files like scripts, stylesheets, images, etc</li>
</ul>
<p>The manifest is a json file which contains the metadata about your extension. Details like Title, Author etc</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{
  &quot;name&quot;: &quot;Twitter Trends&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;description&quot;: &quot;Displays Current Trending Topics&quot;,
  &quot;icons&quot;: { &quot;128&quot;: &quot;icon.png&quot; },
  &quot;browser_action&quot;: {
    &quot;default_title&quot;: &quot;Twitter Trends&quot;,
    &quot;default_icon&quot;: &quot;icon.png&quot;,
    &quot;popup&quot;: &quot;Background.html&quot;
  },
  &quot;permissions&quot;: [
    &quot;tabs&quot;,
    &quot;http://twitter.trends.free.fr/*&quot;
  ]
}</pre></div></div>

<p style="text-align: justify;">As you can see, it contains basic details like name, version, icon etc. The popup attribute tells us the page to open when the browser button is clicked. As you can see I have specified a normal HTML page here. A popup extension is nothing but a simple HTML page opened when the toolbar button is clicked. Another interesting part is the permissions section. Here we can specify what all websites the extension can make AJAX calls to retrieve data. Since it matches based on wildcards, a * denotes all possible subdirectories under the website.</p>
<p style="text-align: justify;">Now the HTML part. Since its a normal HTML page, you can include references to scripts, images, style sheets etc. The extension directory works as the root and all paths must be relative to it. Since its a simple page, I included all the scripts and styling information in a single page.</p>
<p>First the styling information. Always helps to keep this separate.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">  body <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> helvetica<span style="color: #00AA00;">,</span> arial<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#ddeef6</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#title</span>
  <span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#mainDiv</span>
  <span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">min-width</span><span style="color: #00AA00;">:</span><span style="color: #933;">175px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #6666ff;">.topicDisplayClass</span>
  <span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">min-height</span><span style="color: #00AA00;">:</span><span style="color: #933;">2.2em</span><span style="color: #00AA00;">;</span>
     <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span><span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #6666ff;">.linkClass</span>
  <span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#2276bb</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">middle</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #6666ff;">.topicDisplayClass</span><span style="color: #3333ff;"><span style="color: #00AA00;">:</span>Hover
  </span><span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#edfeff</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Next the javascript. This is the most important part of the extension. What we do here is retrieve the RSS feed for the trending topics and parse it as an xmlDocument. After iterating thorough this xml, a span element is dynamically created for each item and it is appended into our main content holder. This main function call is delayed using the setTimeout function. We do this in order to let the user see the popup and see some progress which data is being fetched. Writing this in the onload event would make the user wait for the whole process to complete before seeing the popup.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Developer  : Ganesh Ranganathan
* Date       : 02/12/2010
* Description: This is a Google Chrome Extension created to display
            latest trending topics on twitter. */</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//Global variables: the xmlHttpRequest and Feed URL</span>
<span style="color: #003366; font-weight: bold;">var</span> _req<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> _feedURL <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://twitter.trends.free.fr/feed/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//Call the loading method after a slight delay to allow</span>
    <span style="color: #006600; font-style: italic;">//Chrome to load the extension body. Else it looks ugly with</span>
    <span style="color: #006600; font-style: italic;">//the pressed button waiting for the feed to load.</span>
    setTimeout<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;delayedCall()&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> delayedCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//No browser compatibility code Yay!!!</span>
    _req <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//Hooking up the readystate changed event handler.</span>
    _req.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_req.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> _req.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">//Come here only if all data is loaded.</span>
            <span style="color: #003366; font-weight: bold;">var</span> doc <span style="color: #339933;">=</span> _req.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> parser <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> DOMParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">//Parsing the Data into the XML dom for manipulation</span>
            <span style="color: #003366; font-weight: bold;">var</span> xmlDoc <span style="color: #339933;">=</span> parser.<span style="color: #660066;">parseFromString</span><span style="color: #009900;">&#40;</span>doc<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> mainDiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mainDiv'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            mainDiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">//Getting all the items</span>
            <span style="color: #003366; font-weight: bold;">var</span> items <span style="color: #339933;">=</span> xmlDoc.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> items.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">//Iterating through all the items and getting the title</span>
                <span style="color: #006600; font-style: italic;">//and link. The description isnt a big deal in this feed.</span>
                <span style="color: #003366; font-weight: bold;">var</span> title <span style="color: #339933;">=</span> items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">//Create an anchor and a span</span>
                <span style="color: #003366; font-weight: bold;">var</span> spanNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> linkNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">//Set Link to span</span>
                linkNode.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span> link<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                linkNode.<span style="color: #660066;">innerText</span> <span style="color: #339933;">=</span> title<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">childNodes</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">;</span>
                linkNode.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'linkClass'</span><span style="color: #339933;">;</span>
                spanNode.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>linkNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                spanNode.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'topicDisplayClass'</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">childNodes</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">nodeValue</span>
                <span style="color: #006600; font-style: italic;">//Hooking the click event handler to the span</span>
                <span style="color: #006600; font-style: italic;">//It calls the chrome tabs API to open the clikced</span>
                <span style="color: #006600; font-style: italic;">//URL in a new tab</span>
                spanNode.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Function</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;clickHandler('&quot;</span><span style="color: #339933;">+</span>str<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                mainDiv.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>spanNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//Without these two the AJAX request would never get sent.</span>
    _req.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> _feedURL<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _req.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> clickHandler<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    chrome.<span style="color: #660066;">tabs</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> url<span style="color: #339933;">:</span> str <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">The markup is largely simple. Just the main content holder which contains a  progress bar which is displayed to user while we load the data. Once that is done, the progress bar is replaced with the span nodes containing the trending topics.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;span id=&quot;title&quot;&gt;Trending Topics&lt;/span&gt;
&lt;div id=&quot;mainDiv&quot;&gt;
&lt;!-- A Progress Bar to show the user something is being done --&gt;
&lt;img src=&quot;ajax-loader.gif&quot; alt=&quot;&quot; width=&quot;175px&quot; /&gt;&lt;/div&gt;</pre></div></div>

<p style="text-align: justify;">Now the manifest, html and image files need to be in a single folder. After that go to Chrome and click the customize dropdown button, and select Extensions. In the extensions page, select Load Unpacked Extension and select the folder where the extension files reside.</p>
<p><a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/loadExt.jpg"><img class="aligncenter size-full wp-image-938" title="loadExt" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/loadExt.jpg" alt="" width="573" height="159" /></a></p>
<p>Once the extension is loaded, it shows as unpacked and you can do all the testing here by reloading it everytime any changes are done.<br />
<a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/unpacked.jpg"><img class="aligncenter size-full wp-image-941" title="unpacked" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/unpacked.jpg" alt="" width="328" height="80" /></a></p>
<p style="text-align: justify;">
<p style="text-align: justify;">After testing, we can choose the pack option to package the extension for download. A .crx file would be generated which can be distributed for download. Below are some screenshots of the extension we just created.</p>
<p style="text-align: justify;">
<div id="attachment_942" class="wp-caption aligncenter" style="width: 286px"><a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/inprog.jpg"><img class="size-full wp-image-942" title="inprog" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/inprog.jpg" alt="" width="276" height="175" /></a><p class="wp-caption-text">Data is loading</p></div>
<p><a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/loaded.jpg"><img class="aligncenter size-full wp-image-943" title="loaded" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/02/loaded.jpg" alt="" width="286" height="368" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/02/writing-a-google-chrome-extension-for-displaying-twitter-trending-topics/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Generic Binary Search Tree in C# .NET</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/02/generic-binary-search-tree-in-c-net/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/02/generic-binary-search-tree-in-c-net/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 13:33:19 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Visual STudio 2008]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Data structures]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=895</guid>
		<description><![CDATA[<p style="text-align: justify;">Binary Trees are tree type data structures that contain two child nodes. They can be used to implement Binary Search trees, structures where the left child has a value lesser than the node and the right child has a greater value. Usually Linear collection objects require iteration linearly over the list. In the [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Binary Trees are tree type data structures that contain two child nodes. They can be used to implement Binary Search trees, structures where the left child has a value lesser than the node and the right child has a greater value. Usually Linear collection objects require iteration linearly over the list. In the worst case scenario that the element to be searched is at the end of the list this can take a really really long time. For Binary Trees the search time is cut by half. Since every node tells the search which direction to go and search.</p>
<p style="text-align: justify;">I implemented a basic Binary Tree in .NET for adding custom types. The Tree is generic and can be used to add any type objects. Also the object must implement the generic versions of IComparable and IEquitable for their types and object types. This leaves the search logic&#8217;s implementation to the type rather than the data structure. The complete solution can be downloaded <a href="http://www.ganeshzone.net/Files/BinaryTry.zip">here</a>.</p>
<p>Lets see the declaration of the Binary Tree along with the private members.</p>
<pre class="code"><span style="color: blue;">class </span><span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; <span style="color: blue;">where
    </span>T:<span style="color: #2b91af;">IComparable</span>&lt;T&gt;,
    <span style="color: #2b91af;">IEquatable</span>&lt;T&gt;,
    <span style="color: #2b91af;">IEquatable</span>&lt;<span style="color: blue;">object</span>&gt;,
    <span style="color: #2b91af;">IComparable</span>&lt;<span style="color: blue;">object</span>&gt;
{
    <span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; _parentNode;
    <span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; _leftNode;
    <span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; _rightNode;
    <span style="color: blue;">public </span>T Value { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<p style="text-align: justify;">Each BinaryTree object must be aware of its parent node and its left and right children, and the value it holds.  Lets see the logic for adding the node. The adding logic is recursive. If it finds a null value, then the value is substituted. If not, then the node to be added is compared. If the value is lesser than the same method is called on the leftnode. This happens recursively till we get the node where the value is to be added.</p>
<pre class="code"><span style="color: blue;">public void </span>AddNode(T _nodeTobeAdded)
{
    <span style="color: blue;">if </span>(<span style="color: blue;">null</span>==Value)
        <span style="color: green;">//We found the value. Yay!!
        </span><span style="color: blue;">this</span>.Value = _nodeTobeAdded;
    <span style="color: blue;">else
    </span>{
        <span style="color: green;">//Compare the node to be added. Here the
        //type's IComparable is called.
        </span><span style="color: blue;">if </span>(_nodeTobeAdded.CompareTo(Value) &lt; 0)
        {
            <span style="color: green;">//Go down the left node. If the left node is null,
            //then create a new object, else return the existing object.
            </span>_leftNode = <span style="color: blue;">null</span>==_leftNode ?<span style="color: blue;">new </span><span style="color: #2b91af;">BinaryTree</span>&lt;T&gt;(<span style="color: blue;">this</span>):_leftNode;
            _leftNode.AddNode(_nodeTobeAdded);
        }
        <span style="color: blue;">else
        </span>{
            <span style="color: green;">//Go Down the right node.
            </span>_rightNode = <span style="color: blue;">null</span>==_rightNode?<span style="color: blue;">new </span><span style="color: #2b91af;">BinaryTree</span>&lt;T&gt;(<span style="color: blue;">this</span>):_rightNode;
            _rightNode.AddNode(_nodeTobeAdded);
        }
    }
}</pre>
<p style="text-align: justify;">The GetNode method follows a similar logic, with recursive searching of the tree till a match is found, if no match is found then a null value is returned. Note that only around half of the tree is searched, rather than all of the nodes, as would be the case with a linkedList. GetNode is implemented both for the type and an object. In both cases the Equals method&#8217;s implementation is called, giving the generic type the ability to decide on its own equality logic.</p>
<pre class="code"><span style="color: blue;">public </span>T GetNode(T _nodeToBeGotten)
{
   <span style="color: blue;">if</span>(_nodeToBeGotten.Equals(Value))
       <span style="color: blue;">return </span>Value;
   <span style="color: blue;">else if</span>(_nodeToBeGotten.CompareTo(Value)&lt;0)
       <span style="color: blue;">return null</span>==_leftNode? <span style="color: blue;">default</span>(T): _leftNode.GetNode(_nodeToBeGotten);
   <span style="color: blue;">else
       return null</span>==_rightNode?<span style="color: blue;">default</span>(T): _rightNode.GetNode(_nodeToBeGotten);
}

<span style="color: blue;">public </span>T GetNode(<span style="color: blue;">object </span>o)
{
    <span style="color: blue;">if </span>(Value.Equals(o))
        <span style="color: blue;">return </span>Value;
    <span style="color: blue;">else if </span>(Value.CompareTo(o)&gt;0)
        <span style="color: blue;">return </span>(<span style="color: blue;">null </span>== _leftNode) ?
            <span style="color: blue;">default</span>(T) : _leftNode.GetNode(o);
    <span style="color: blue;">else
        return null </span>== _rightNode ?
            <span style="color: blue;">default</span>(T) : _rightNode.GetNode(o);
}</pre>
<p>Deleting is a bit tricky business. We have four scenarios here:-</p>
<ol>
<li style="text-align: justify;"> Its a leaf node, i.e. both the left and right children are null. This is the simplest scenario. Simply set the node itself to null</li>
<li style="text-align: justify;">The LeftNode is null but the right node is not, in this scenario &#8211; the node&#8217;s right node is set to the parent node&#8217;s right node, and the node is set to null.</li>
<li style="text-align: justify;">The Right node is null but left node is not. It is handled similar to the previous scenario.</li>
<li style="text-align: justify;">Both leftNode and rightNode is not null. In this scenarios we need to go down the leftnode, and iterate to its right most non empty node. This node is to be replaced with the node to be deleted. Just the value needs to be replaced and the right most node should be orphaned.</li>
</ol>
<pre class="code"><span style="color: blue;">public void </span>DeleteNode(<span style="color: blue;">object </span>o)
{
    <span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; _tempNode1 = <span style="color: blue;">this</span>.GetTreeNode(o);
    DeleteNode(_tempNode1);
}

<span style="color: blue;">public void </span>DeleteNode(T _nodeTobeDeleted)
{
    <span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; _tempNode1 = <span style="color: blue;">this</span>.GetTreeNode(_nodeTobeDeleted);
    DeleteNode(_tempNode1);
}

<span style="color: blue;">private void </span>DeleteNode(<span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; _tempNode1)
{
    <span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; _tempNode2 = _tempNode1;
    <span style="color: blue;">if </span>((<span style="color: blue;">null </span>== _tempNode1._leftNode) &amp;&amp; (<span style="color: blue;">null </span>== _tempNode1._rightNode))
        _tempNode1._parentNode = <span style="color: blue;">null</span>;
    <span style="color: blue;">else if </span>(<span style="color: blue;">null </span>== _tempNode1._leftNode)
        _tempNode1._parentNode._rightNode = _tempNode1._rightNode;
    <span style="color: blue;">else if </span>(<span style="color: blue;">null </span>== _tempNode1._rightNode)
        _tempNode1._parentNode._leftNode = _tempNode1._leftNode;
    <span style="color: blue;">else
    </span>{
        <span style="color: blue;">bool </span>_goingRight = <span style="color: blue;">false</span>;
        _tempNode1 = _tempNode1._leftNode;
        <span style="color: blue;">while </span>(_tempNode1._rightNode != <span style="color: blue;">null</span>)
        {
            _tempNode1 = _tempNode1._rightNode;
            _goingRight = <span style="color: blue;">true</span>;
        }
        _tempNode2.Value = _tempNode1.Value;
        <span style="color: blue;">if </span>(_goingRight) _tempNode1._parentNode._rightNode = <span style="color: blue;">null</span>;
        <span style="color: blue;">else </span>_tempNode1._parentNode._leftNode = <span style="color: blue;">null</span>;
        _tempNode1 = <span style="color: blue;">null</span>;
    }
}

<span style="color: green;">//Private methods to get the nodes to be deleted
</span><span style="color: blue;">private </span><span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; GetTreeNode(T _nodeToBeGotten)
{
    <span style="color: blue;">if </span>(_nodeToBeGotten.Equals(Value))
        <span style="color: blue;">return this</span>;
    <span style="color: blue;">else if </span>(_nodeToBeGotten.CompareTo(Value) &lt; 0)
        <span style="color: blue;">return null </span>== _leftNode ? <span style="color: blue;">default</span>(<span style="color: #2b91af;">BinaryTree</span>&lt;T&gt;) : _leftNode.GetTreeNode(_nodeToBeGotten);
    <span style="color: blue;">else
        return null </span>== _rightNode ? <span style="color: blue;">default</span>(<span style="color: #2b91af;">BinaryTree</span>&lt;T&gt;) : _rightNode.GetTreeNode(_nodeToBeGotten);
}

<span style="color: blue;">private </span><span style="color: #2b91af;">BinaryTree</span>&lt;T&gt; GetTreeNode(<span style="color: blue;">object </span>o)
{
    <span style="color: blue;">if </span>(Value.Equals(o))
        <span style="color: blue;">return this</span>;
    <span style="color: blue;">else if </span>(Value.CompareTo(o) &gt; 0)
        <span style="color: blue;">return </span>(<span style="color: blue;">null </span>== _leftNode) ?
            <span style="color: blue;">default</span>(<span style="color: #2b91af;">BinaryTree</span>&lt;T&gt;) : _leftNode.GetTreeNode(o);
    <span style="color: blue;">else
        return null </span>== _rightNode ?
            <span style="color: blue;">default</span>(<span style="color: #2b91af;">BinaryTree</span>&lt;T&gt;) : _rightNode.GetTreeNode(o);
}</pre>
<p>The class which could be added to the tree needs to implement the four interfaces i mentioned. I implemented an Employee class which has just two fields Name and age and the comparison is done based on the age.</p>
<pre class="code"><span style="color: blue;">class </span><span style="color: #2b91af;">Employee </span>: <span style="color: #2b91af;">IComparable</span>&lt;<span style="color: #2b91af;">Employee</span>&gt;,<span style="color: #2b91af;">IEquatable</span>&lt;<span style="color: #2b91af;">Employee</span>&gt;,<span style="color: #2b91af;">IEquatable</span>&lt;<span style="color: blue;">object</span>&gt;,<span style="color: #2b91af;">IComparable</span>&lt;<span style="color: blue;">object</span>&gt;
    {
        <span style="color: blue;">private string </span>_name;
        <span style="color: blue;">private int </span>_age;

        <span style="color: blue;">public int </span>Age{<span style="color: blue;">get</span>{<span style="color: blue;">return </span>_age;}}

        <span style="color: blue;">public </span>Employee(<span style="color: blue;">string </span>_name, <span style="color: blue;">int </span>_age)
        {
            <span style="color: blue;">this</span>._name = _name;
            <span style="color: blue;">this</span>._age = _age;
        }

        <span style="color: blue;">#region </span>IComparable&lt;Employee&gt; Members

        <span style="color: blue;">public int </span>CompareTo(<span style="color: #2b91af;">Employee </span>other)
        {
            <span style="color: blue;">if </span>(<span style="color: blue;">this</span>._age &lt; other._age)
                <span style="color: blue;">return </span>-1;
            <span style="color: blue;">else
                return </span>1;
        }

        <span style="color: blue;">#endregion

        public bool </span>Equals(<span style="color: #2b91af;">Employee </span>other)
        {
            <span style="color: blue;">if </span>(<span style="color: blue;">this</span>._age == other._age)
                <span style="color: blue;">return true</span>;
            <span style="color: blue;">else
                return false</span>;
        }

        <span style="color: blue;">#region </span>IEquatable&lt;object&gt; Members

        <span style="color: blue;">public override bool </span>Equals(<span style="color: blue;">object </span>other)
        {
            <span style="color: blue;">int </span>_age=0;
            <span style="color: blue;">if </span>(<span style="color: blue;">int</span>.TryParse(other.ToString(), <span style="color: blue;">out </span>_age))
            {
                <span style="color: blue;">return this</span>._age == _age ? <span style="color: blue;">true </span>: <span style="color: blue;">false</span>;
            }
            <span style="color: blue;">else return false</span>;
        }

        <span style="color: blue;">#endregion

        #region </span>IComparable&lt;object&gt; Members

        <span style="color: blue;">public int </span>CompareTo(<span style="color: blue;">object </span>other)
        {
            <span style="color: blue;">int </span>_age = 0;
            <span style="color: blue;">if </span>(<span style="color: blue;">int</span>.TryParse(other.ToString(), <span style="color: blue;">out </span>_age))
                <span style="color: blue;">return this</span>._age &lt; _age ? -1 : 1;
            <span style="color: blue;">else return </span>1;
        }

        <span style="color: blue;">#endregion

    </span>}</pre>
<p>This is the main method which adds a few objects to the tree collection.</p>
<pre class="code"><span style="color: blue;">static void </span>Main(<span style="color: blue;">string</span>[] args)
{
    <span style="color: #2b91af;">BinaryTree</span>&lt;Employee&gt; _btree = <span style="color: blue;">new </span><span style="color: #2b91af;">BinaryTree</span>&lt;Employee&gt;(<span style="color: blue;">new </span>Employee(<span style="color: #a31515;">"RootValue"</span>,40));
    _btree.AddNode(<span style="color: blue;">new </span>Employee(<span style="color: #a31515;">"Test1"</span>, 34));
    _btree.AddNode(<span style="color: blue;">new </span>Employee(<span style="color: #a31515;">"Test2"</span>, 26));
    _btree.AddNode(<span style="color: blue;">new </span>Employee(<span style="color: #a31515;">"Test3"</span>, 65));
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/02/generic-binary-search-tree-in-c-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tic-Tac-Toe game in Silverlight</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/01/tic-tac-toe-game-in-silverlight/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/01/tic-tac-toe-game-in-silverlight/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 20:05:12 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=852</guid>
		<description><![CDATA[<p>Recently someone asked me to write a TicTacToe game. So I worked out a rough logic for it. Heres the code. The idea is that the computer first scans through the tic-tac-toe board for any winning positions available. If none are available, then it looks for the manual user&#8217;s winning positions. If its able to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently someone asked me to write a TicTacToe game. So I worked out a rough logic for it. Heres the code. The idea is that the computer first scans through the tic-tac-toe board for any winning positions available. If none are available, then it looks for the manual user&#8217;s winning positions. If its able to find them, it blocks it. If not the next available position is filled. The game can be played by clicking any of the squares below.</p>
<div style="text-align:center">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="340"><param name="source" value="http://www.ganeshzone.net/Silverlight/Objects/TicTacToe.xap"/><param name="onError" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40818.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40818.0" style="text-decoration:none"><br />
 			  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/></p>
<p>		  </a><br />
	    </object> </div>
<p>Here is the code for the game as a silverlight application. The complete project can be downloaded <a href="http://www.ganeshzone.net/Files/TicTacToe.zip">here</a>.</p>
<p>The XAML is quite simple. Just a grid with three rows and three columns</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;gridGame&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;White&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/grid<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;grid</span> .RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rowdefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rowdefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rowdefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;grid</span> .ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;columndefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;columndefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;columndefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>It is then filled with the buttons in the Code Behind. I have hardcoded the grid size of three but this will work with any size.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #0600FF;">public</span> MainPage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            InitializeGameLogic<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> InitializeGameLogic<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> _fromConstructor<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_fromConstructor<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                gridGame.<span style="color: #0000FF;">Children</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            _logic <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TicTacLogic<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;">3</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</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;">3</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    Button _btnGrid <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    _btnGrid.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;btn&quot;</span> <span style="color: #008000;">+</span> i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> j.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    gridGame.<span style="color: #0000FF;">Children</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_btnGrid<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    Grid.<span style="color: #0000FF;">SetRow</span><span style="color: #000000;">&#40;</span>_btnGrid, j<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    Grid.<span style="color: #0000FF;">SetColumn</span><span style="color: #000000;">&#40;</span>_btnGrid, i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    _btnGrid.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">80</span><span style="color: #008000;">;</span>
                    _btnGrid.<span style="color: #0000FF;">Width</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">80</span><span style="color: #008000;">;</span>
                    _btnGrid.<span style="color: #0000FF;">Click</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #000000;">&#40;</span>_btnGrid_Click<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            _logic.<span style="color: #0000FF;">StartGame</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>All the dynamically generated buttons have a single event handler. The idea is to identify the button which sent the event from the name. Once we have the position of the button, the content has to be changed with an X. After the user marks, the computer does its stuff and makes its move. After every marking, a check is done if someone has won or the board is full.</p>
</pre>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">void</span> _btnGrid_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, RoutedEventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Button _userClicked <span style="color: #008000;">=</span> sender <span style="color: #0600FF;">as</span> Button<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> _xPos <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>_userClicked.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span>,<span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> _yPos <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>_userClicked.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">4</span>,<span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _userClicked.<span style="color: #0000FF;">Content</span> <span style="color: #008000;">=</span> GetMarking<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _logic.<span style="color: #0000FF;">ClickUser</span><span style="color: #000000;">&#40;</span>_xPos, _yPos<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #000000;">&#40;</span>_logic.<span style="color: #0000FF;">CheckIfSomeoneWon</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span> _logic.<span style="color: #0000FF;">CheckIfBoardFull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">int</span> _xCom <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">int</span> _yCom <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                _logic.<span style="color: #0000FF;">MoveComputer</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> _xCom, <span style="color: #0600FF;">ref</span> _yCom<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>UIElement _tempElem <span style="color: #0600FF;">in</span> gridGame.<span style="color: #0000FF;">Children</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    Button _comBtn <span style="color: #008000;">=</span> _tempElem <span style="color: #0600FF;">as</span> Button<span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_comBtn <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> _comBtn.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;btn&quot;</span> <span style="color: #008000;">+</span> _xCom <span style="color: #008000;">+</span> _yCom<span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        _comBtn.<span style="color: #0000FF;">Content</span> <span style="color: #008000;">=</span> GetMarking<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;O&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                        break<span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_logic.<span style="color: #0000FF;">CheckIfSomeoneWon</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span> _logic.<span style="color: #0000FF;">CheckIfBoardFull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    InitializeGameLogic<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
                InitializeGameLogic<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> TextBlock GetMarking<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> _mark<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            TextBlock _t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TextBlock<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _t.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> _mark<span style="color: #008000;">;</span>
            _t.<span style="color: #0000FF;">FontSize</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">25</span><span style="color: #008000;">;</span>
            _t.<span style="color: #0000FF;">FontWeight</span> <span style="color: #008000;">=</span> FontWeights.<span style="color: #0000FF;">Bold</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> _t<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The Board class maintains two important things. The position of all the squares and which player has it marked. The Board doesnt depend on the Player objects. It has an object of the WinningPositions object which calculates the winning positions which can be compared with later. This object makes it possible to play  with larger grids.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Board
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">int</span> MAXSIZE<span style="color: #008000;">;</span>
    List<span style="color: #008000;">&lt;</span>positionmarking<span style="color: #008000;">&gt;</span> _boardState <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;/</span>positionmarking<span style="color: #008000;">&gt;&lt;</span>positionmarking<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    List<span style="color: #008000;">&lt;</span>position <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span> _allWinningPositions<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>positionmarking<span style="color: #008000;">&gt;</span> BoardState <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _boardState<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>position <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span> AllWinningPositions <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _allWinningPositions<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> MaxSize <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> MAXSIZE<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Board<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">MAXSIZE</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
        Initialize<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> Board<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _maxSize<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        MAXSIZE <span style="color: #008000;">=</span> _maxSize<span style="color: #008000;">;</span>
        Initialize<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;">private</span> <span style="color: #0600FF;">void</span> Initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _allWinningPositions <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> WinningPositions<span style="color: #000000;">&#40;</span>MAXSIZE<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AllWinningPositions</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _xPos<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>_xPos<span style="color: #008000;">&lt;</span>maxsize <span style="color: #008000;">;</span>_xPos<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _yPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> _yPos <span style="color: #008000;">&lt;</span> MAXSIZE<span style="color: #008000;">;</span> _yPos<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                _boardState.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> PositionMarking<span style="color: #000000;">&#40;</span>_xPos,_yPos<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>
&nbsp;
    <span style="color: #0600FF;">internal</span> <span style="color: #0600FF;">void</span> MarkPosition<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _xUser, <span style="color: #FF0000;">int</span> _yUser, <span style="color: #FF0000;">bool</span> _userMarked<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PositionMarking _tempPos <span style="color: #0600FF;">in</span> _boardState<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_tempPos.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">X</span> <span style="color: #008000;">==</span> _xUser <span style="color: #008000;">&amp;&amp;</span> _tempPos.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">Y</span> <span style="color: #008000;">==</span> _yUser<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                _tempPos.<span style="color: #0000FF;">MarkPosition</span><span style="color: #000000;">&#40;</span>_userMarked<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                break<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
<span style="color: #008080;">#if DEBUG</span>
        PrintBoardState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080;">#endif</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">bool</span><span style="color: #008000;">?</span> MarkedByUser<span style="color: #000000;">&#40;</span>Position _tempPos<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PositionMarking _tempMark <span style="color: #0600FF;">in</span> _boardState<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_tempPos.<span style="color: #0000FF;">X</span> <span style="color: #008000;">==</span> _tempMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">X</span> <span style="color: #008000;">&amp;&amp;</span> _tempPos.<span style="color: #0000FF;">Y</span> <span style="color: #008000;">==</span> _tempMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">Y</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> _tempMark.<span style="color: #0000FF;">IsUserMarked</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Position Not found on Board&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#if DEBUG</span>
    <span style="color: #0600FF;">internal</span> <span style="color: #0600FF;">void</span> PrintBoardState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> _userMarked <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> _compMarked <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PositionMarking _tempMark <span style="color: #0600FF;">in</span> _boardState<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_tempMark.<span style="color: #0000FF;">IsUserMarked</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
                _userMarked <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;(&quot;</span><span style="color: #008000;">+</span> _tempMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;,&quot;</span> <span style="color: #008000;">+</span> _tempMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;) &quot;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_tempMark.<span style="color: #0000FF;">IsUserMarked</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>
                _compMarked <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;(&quot;</span> <span style="color: #008000;">+</span> _tempMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;,&quot;</span> <span style="color: #008000;">+</span> _tempMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;) &quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        Debug.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;USER: &quot;</span> <span style="color: #008000;">+</span> _userMarked<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Debug.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;COMP: &quot;</span> <span style="color: #008000;">+</span> _compMarked<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Debug.<span style="color: #0000FF;">WriteLine</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>
    <span style="color: #000000;">&#125;</span>
<span style="color: #008080;">#endif</span>
    <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">bool</span> IsBoardFull<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PositionMarking _tempMark <span style="color: #0600FF;">in</span> _boardState<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_tempMark.<span style="color: #0000FF;">IsUserMarked</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
        MessageBox.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Board Full&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The Winning Position class is given below and all positions are calculated at initialization. First the horizontal ones are considered and then the vertical ones. The remaining diagonal positons are taken care later. Each winning position is stored in an array of the position object which just holds the value of the Squares. These arrays are stored in a generic List.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WinningPositions
<span style="color: #000000;">&#123;</span>
    List<span style="color: #008000;">&lt;</span>position <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span> _allWinningPositions <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;/</span>position<span style="color: #008000;">&gt;&lt;</span>position <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Position<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _winPos1<span style="color: #008000;">;</span>
    Position<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _winPos2<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;/</span>position<span style="color: #008000;">&gt;&lt;</span>position <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span> AllWinningPositions
    <span style="color: #000000;">&#123;</span>
        get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _allWinningPositions<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> WinningPositions<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _maxSize<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _winPos1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#91;</span>_maxSize<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        _winPos2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#91;</span>_maxSize<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        PopulateWinningPositions<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;">private</span> <span style="color: #0600FF;">void</span> PopulateWinningPositions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span> _iCounter<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> _xPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> _yPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>_xPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> _xPos <span style="color: #008000;">&lt;</span> _winPos2.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> _xPos<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>_yPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> _yPos <span style="color: #008000;">&lt;</span> _winPos2.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> _yPos<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                _winPos1<span style="color: #000000;">&#91;</span>_iCounter<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#40;</span>_xPos, _yPos<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                _winPos2<span style="color: #000000;">&#91;</span>_iCounter<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#40;</span>_yPos, _xPos<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                _iCounter<span style="color: #008000;">++;</span>
            <span style="color: #000000;">&#125;</span>
            _allWinningPositions.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_winPos1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _allWinningPositions.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_winPos2<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _winPos1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#91;</span>_winPos1.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            _winPos2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#91;</span>_winPos2.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            _iCounter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        _iCounter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>_xPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>, _yPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>_xPos <span style="color: #008000;">&lt;</span> _winPos1.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> _xPos<span style="color: #008000;">++</span>, _yPos<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _winPos1<span style="color: #000000;">&#91;</span>_iCounter<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#40;</span>_xPos, _yPos<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            _iCounter<span style="color: #008000;">++;</span>
        <span style="color: #000000;">&#125;</span>
        _allWinningPositions.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_winPos1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _iCounter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>_xPos <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>, _yPos <span style="color: #008000;">=</span> _winPos1.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> _xPos <span style="color: #008000;">&lt;</span> _winPos1.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> _xPos<span style="color: #008000;">++</span>, _yPos<span style="color: #008000;">--</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _winPos2<span style="color: #000000;">&#91;</span>_iCounter<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#40;</span>_xPos, _yPos<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _iCounter<span style="color: #008000;">++;</span>
        <span style="color: #000000;">&#125;</span>
        _allWinningPositions.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_winPos2<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Then there is the Player class of which we create two objects. The user takes care of his own moving logic, all we need to do is to change the state of the square for him. But the computer has its method called the <strong>SmartMove</strong>. Here we first move through the winning positions to identify any open squares that can be marked. If there are no squares, then we look to block the opponent's winning. If even this opportunity is not found, we mark an available open square.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Player
<span style="color: #000000;">&#123;</span>
    Board _currentBoardPosition<span style="color: #008000;">;</span>
    <span style="color: #FF0000;">bool</span> _isComputer<span style="color: #008000;">;</span>
    List<span style="color: #008000;">&lt;</span>position<span style="color: #008000;">&gt;</span> _allMarkedPositions<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Player<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> _isComputer,Board _currentBoardPos<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>._isComputer <span style="color: #008000;">=</span> _isComputer<span style="color: #008000;">;</span>
        _allMarkedPositions <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;/</span>position<span style="color: #008000;">&gt;&lt;</span>position<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _currentBoardPosition <span style="color: #008000;">=</span> _currentBoardPos<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">internal</span> <span style="color: #0600FF;">void</span> SmartMove<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">int</span> _xCom, <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">int</span> _yCom<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">bool</span> _markingDone <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">bool</span> _blockUser <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        Position _userBlock <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Position<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _winPos <span style="color: #0600FF;">in</span> _currentBoardPosition.<span style="color: #0000FF;">AllWinningPositions</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> _userMarked <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> _computerMarked <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> _nullMarked<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
            Position _openPos<span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Position _tempPos <span style="color: #0600FF;">in</span> _winPos<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">bool</span><span style="color: #008000;">?</span> isUserMarked <span style="color: #008000;">=</span> _currentBoardPosition.<span style="color: #0000FF;">MarkedByUser</span><span style="color: #000000;">&#40;</span>_tempPos<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isUserMarked<span style="color: #008000;">==</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
                    _userMarked<span style="color: #008000;">++;</span>
                <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isUserMarked <span style="color: #008000;">==</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>
                    _computerMarked<span style="color: #008000;">++;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    _openPos <span style="color: #008000;">=</span> _tempPos<span style="color: #008000;">;</span>
                    _nullMarked<span style="color: #008000;">++;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_computerMarked <span style="color: #008000;">==</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">&amp;&amp;</span> _nullMarked <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Debug.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Wining block: &quot;</span> <span style="color: #008000;">+</span> _openPos.<span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;,&quot;</span> <span style="color: #008000;">+</span> _openPos.<span style="color: #0000FF;">Y</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                _currentBoardPosition.<span style="color: #0000FF;">MarkPosition</span><span style="color: #000000;">&#40;</span>_openPos.<span style="color: #0000FF;">X</span>, _openPos.<span style="color: #0000FF;">Y</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                _xCom <span style="color: #008000;">=</span> _openPos.<span style="color: #0000FF;">X</span><span style="color: #008000;">;</span>
                _yCom <span style="color: #008000;">=</span> _openPos.<span style="color: #0000FF;">Y</span><span style="color: #008000;">;</span>
                _markingDone <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                break<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_userMarked <span style="color: #008000;">==</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">&amp;&amp;</span> _nullMarked <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                _userBlock <span style="color: #008000;">=</span> _openPos<span style="color: #008000;">;</span>
                _blockUser <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_blockUser <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>_markingDone<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _currentBoardPosition.<span style="color: #0000FF;">MarkPosition</span><span style="color: #000000;">&#40;</span>_userBlock.<span style="color: #0000FF;">X</span>, _userBlock.<span style="color: #0000FF;">Y</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _xCom <span style="color: #008000;">=</span> _userBlock.<span style="color: #0000FF;">X</span><span style="color: #008000;">;</span>
            _yCom <span style="color: #008000;">=</span> _userBlock.<span style="color: #0000FF;">Y</span><span style="color: #008000;">;</span>
            _markingDone <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_markingDone<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//No Winning Position Found or not able to block;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PositionMarking _posMark <span style="color: #0600FF;">in</span> _currentBoardPosition.<span style="color: #0000FF;">BoardState</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>_posMark.<span style="color: #0000FF;">IsUserMarked</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    _markingDone <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                    _currentBoardPosition.<span style="color: #0000FF;">MarkPosition</span><span style="color: #000000;">&#40;</span>_posMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">X</span>, _posMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">Y</span>,<span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    _xCom <span style="color: #008000;">=</span> _posMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">X</span><span style="color: #008000;">;</span>
                    _yCom <span style="color: #008000;">=</span> _posMark.<span style="color: #0000FF;">BoardPosition</span>.<span style="color: #0000FF;">Y</span><span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>position<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>Since the Board needs to maintain the state of each and every position on itself, we have another object for it, which maintains a position object and a nullable boolean. If the boolean is true, then the square is marked by the user, if false - then by the computer. If its still null - its open and can be marked. The marking is done by the object's method which makes a check to make sure no players marking is overwritten by another.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PositionMarking
<span style="color: #000000;">&#123;</span>
    Position _boardPos<span style="color: #008000;">;</span>
    <span style="color: #FF0000;">bool</span><span style="color: #008000;">?</span> _isUserMarked<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Position BoardPosition <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _boardPos<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span><span style="color: #008000;">?</span> IsUserMarked <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _isUserMarked<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> PositionMarking<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _xPos,<span style="color: #FF0000;">int</span> _yPos<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _isUserMarked <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
        _boardPos <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Position<span style="color: #000000;">&#40;</span>_xPos, _yPos<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> MarkPosition<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> _markedByUser<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>_isUserMarked <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        _isUserMarked <span style="color: #008000;">=</span> _markedByUser<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">else</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Position already taken&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Then there is the rest of the code including our business logic layer.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TicTacLogic
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">bool</span> _userToMove<span style="color: #008000;">;</span>
    <span style="color: #FF0000;">bool</span> _resultHasCome<span style="color: #008000;">;</span>
    Player _user<span style="color: #008000;">;</span>
    Player _computer<span style="color: #008000;">;</span>
    Board _gameBoard<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> TicTacLogic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _userToMove <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        _resultHasCome <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">internal</span> <span style="color: #0600FF;">void</span> StartGame<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Initialize<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> <span style="color: #FF0000;">bool</span> ClickUser<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _xUser, <span style="color: #FF0000;">int</span> _yUser<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> _gameBoard.<span style="color: #0000FF;">MarkPosition</span><span style="color: #000000;">&#40;</span>_xUser, _yUser, <span style="color: #0600FF;">true</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> <span style="color: #0600FF;">void</span> MoveComputer<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">int</span> _xCom, <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">int</span> _yCom<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _computer.<span style="color: #0000FF;">SmartMove</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> _xCom, <span style="color: #0600FF;">ref</span> _yCom<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">internal</span> <span style="color: #0600FF;">void</span> Initialize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _gameBoard <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Board<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _user <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Player<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span>,_gameBoard<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _computer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Player<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>,_gameBoard<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> CheckIfSomeoneWon<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Position<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _tempWin <span style="color: #0600FF;">in</span> _gameBoard.<span style="color: #0000FF;">AllWinningPositions</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> _userWon<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> _compWon<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Position _p <span style="color: #0600FF;">in</span> _tempWin<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">bool</span><span style="color: #008000;">?</span> isUserMarked <span style="color: #008000;">=</span> _gameBoard.<span style="color: #0000FF;">MarkedByUser</span><span style="color: #000000;">&#40;</span>_p<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isUserMarked <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isUserMarked <span style="color: #008000;">==</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
                    _userWon<span style="color: #008000;">++;</span>
                <span style="color: #0600FF;">else</span>
                    _compWon<span style="color: #008000;">++;</span>
&nbsp;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_userWon <span style="color: #008000;">==</span> _gameBoard.<span style="color: #0000FF;">MaxSize</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                MessageBox.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;User Won&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_compWon <span style="color: #008000;">==</span> _gameBoard.<span style="color: #0000FF;">MaxSize</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                MessageBox.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Computer Won&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">bool</span> CheckIfBoardFull<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> _gameBoard.<span style="color: #0000FF;">IsBoardFull</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: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Position
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> _xPos<span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> _yPos<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Position<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: #FF0000;">int</span> X <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _xPos<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Y <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _yPos<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Position<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> xPos, <span style="color: #FF0000;">int</span> yPos<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>._xPos <span style="color: #008000;">=</span> xPos<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">this</span>._yPos <span style="color: #008000;">=</span> yPos<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</position></pre>
<p></maxsize></position></positionmarking></position></positionmarking></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/01/tic-tac-toe-game-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency Injection for Test Driven Development</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/01/dependency-injection-for-test-driven-development/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/01/dependency-injection-for-test-driven-development/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 19:43:48 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=831</guid>
		<description><![CDATA[<p style="text-align: justify;">As applications grow more and more complex, they cost more money to develop, test and maintain. Each bug that leaves the development stage, needs the QA&#8217;s effort to identify and notify the developer who again spends time to fix it and get it signed off from the QA. Hence its imperative that good [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">As applications grow more and more complex, they cost more money to develop, test and maintain. Each bug that leaves the development stage, needs the QA&#8217;s effort to identify and notify the developer who again spends time to fix it and get it signed off from the QA. Hence its imperative that good code is written the first time around. This fundamental need is what gave birth to what we call Test Drive Development (TDD), i.e. where the developer writes an automated test case first, sees it failing and then writes the code for it to verify the test passes. This technique is also called the red-green style of development.</p>
<p style="text-align: justify;">Automated testing is an integral part of Test Driven Development. A developer simply doesn&#8217;t have the time to do functional unit testing of his application, an effort which would neither be reproducible, nor comprehensive. Hence developers use Automated testing tools which decrease the testing effort, even if a little bit more code needs to be written.</p>
<p style="text-align: justify;">The biggest problem while automating testing for applications, especially web applications is the tight coupling between layers making it impossible to test one layer without the another. This can create a lot of complications, since you might get false positives and negatives. Imagine a scenario where you write a test case for a business layer method which retrieves some data from the database and processes it. The aim here is to only test the business layer&#8217;s functionality, but if there is a concrete dependency on the data layer, then the result of the test would inevitably depend on the retrieval of data. This is where you could get false positives or negatives.</p>
<p style="text-align: justify;">Hence the reasoning that the test cases should be limited to only the functionality being tested. Consider the below code.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008080; font-style: italic;">//Your Business Class</span>
    <span style="color: #FF0000;">class</span> BusinessObjects
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//This is a concrete dependency on the dataLayer Object</span>
        <span style="color: #0600FF;">private</span> DataObjects _dataLayerObj<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> BusinessObjects<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;">//Initializing the DataLayer object</span>
            _dataLayerObj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataObjects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//Get the customer's Name in upper case</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetCustomerNameInUpperCase<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _customerID<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>_dataLayerObj.<span style="color: #0000FF;">RunScalarQuery</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Your Select Query here&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</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></pre></div></div>

<p>So If you write a test on the <strong><em>GetCustomerNameInUpperCase</em></strong> method, it would need to have the database up and running in order to pass. To avoid this, a technique called dependency injection is used in which the application&#8217;s layers are loosely coupled with each other enabling us to test each layer&#8217;s functionality independent of each other.</p>
<p style="text-align: justify;">This is accomplished by introducing an interface from which the Data layer would inherit and do its work. The business layer would be aware only of these interfaces. Business layer would no longer care about the implementation and thus would allow us to define our own implementation of the interface. This implementation can be &#8220;<strong>injected</strong>&#8221; through an additional constructor. Lets see the code for the data layer now.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008080; font-style: italic;">//This is the Interface on which the business layer</span>
    <span style="color: #008080; font-style: italic;">//would depend and not the data layer class itself</span>
    <span style="color: #FF0000;">interface</span> IData
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> RunScalarQuery<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> _selectQuery<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">//Other code is oblivious to the implementation behind</span>
    <span style="color: #008080; font-style: italic;">//the data layer as long as it returns the name</span>
    <span style="color: #FF0000;">class</span> DataObjects<span style="color: #008000;">:</span> IData
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//Implementation of the interface</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> RunScalarQuery<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> _selectQuery<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//Do all the database related stuff</span>
            <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;Some Customer's Name&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Now we would need to modify the business layer to make it depend on the interface.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">   <span style="color: #FF0000;">class</span> BusinessObjects
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//It now depends on the interface</span>
        <span style="color: #0600FF;">private</span> IData _dataLayerObj<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> BusinessObjects<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;">//Initializing the DataLayer object</span>
            _dataLayerObj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataObjects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//This is the additional constructor that is used</span>
        <span style="color: #008080; font-style: italic;">//to inject a custom implementation</span>
        <span style="color: #0600FF;">public</span> BusinessObjects<span style="color: #000000;">&#40;</span>IData _dataObject<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>._dataLayerObj <span style="color: #008000;">=</span> _dataObject<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//Get the customer's Name in upper case</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetCustomerNameInUpperCase<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> _customerID<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>_dataLayerObj.<span style="color: #0000FF;">RunScalarQuery</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Your Select Query here&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</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></pre></div></div>

<p style="text-align: justify;">Now lets write the test case for this method. Instead of using the default constructor making the Business layer go to the database for the customer&#8217;s name, our very own Mock Class does the trick by supplying a dummy value.</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> BusinessLayerTests
    <span style="color: #000000;">&#123;</span>
        BusinessObjects _bObjects<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>SetUp<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Init<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;">//Pass our own implementation of IDATA using</span>
            <span style="color: #008080; font-style: italic;">//the additional constructor we defined</span>
            _bObjects <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BusinessObjects<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> MockClass<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>
&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> CheckIfUserNameIsUpper<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;">//User regular Expressions to make sure all upper cases are returned</span>
            <span style="color: #FF0000;">string</span> _returnedName <span style="color: #008000;">=</span> _bObjects.<span style="color: #0000FF;">GetCustomerNameInUpperCase</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2342</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>_returnedName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">IsTrue</span><span style="color: #000000;">&#40;</span>Regex.<span style="color: #0000FF;">IsMatch</span><span style="color: #000000;">&#40;</span>_returnedName, <span style="color: #666666;">&quot;^[A-Z]+$&quot;</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>
&nbsp;
    <span style="color: #008080; font-style: italic;">//This inherirs from IData</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MockClass <span style="color: #008000;">:</span> IData
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080;">#region IData Members</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> RunScalarQuery<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> _selectQuery<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//Simply return a lower case name</span>
            <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;lowercaseName&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">This does seem like too much work to test such a simple functionality, but believe me, the benefits grow exponentially with the size of the project. As you start following this approach, it would be easier to test as you go, making the chances of missing out on that tiny bug much lesser. And at the end of the day, nothing makes the developer more happy than seeing all his unit tests pass with the large green NUnit bar.</p>
<p style="text-align: justify;"><a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/01/testsPass.jpg"><img class="aligncenter size-medium wp-image-848" title="testsPass" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/01/testsPass-300x67.jpg" alt="" width="300" height="67" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/01/dependency-injection-for-test-driven-development/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Deploying ASP.NET MVC Apps on Shared Hosting</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/01/deploying-asp-net-mvc-apps-on-shared-hosting/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/01/deploying-asp-net-mvc-apps-on-shared-hosting/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 18:20:39 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=823</guid>
		<description><![CDATA[<p>Here are the steps to deploy MVC applications to servers with shared hosting providers.</p>

In your Hosting provider&#8217;s IIS management panel, create a sub directory and configure it as an appplication- root, so that it is recognized as an asp.net application.


Set your MVC dll&#8217;s Copy To Local property to true, so that they are copied to [...]]]></description>
			<content:encoded><![CDATA[<p>Here are the steps to deploy MVC applications to servers with shared hosting providers.</p>
<ul>
<li>In your Hosting provider&#8217;s IIS management panel, create a sub directory and configure it as an appplication- root, so that it is recognized as an asp.net application.</li>
</ul>
<ul>
<li>Set your MVC dll&#8217;s Copy To Local property to true, so that they are copied to the bin directory of the project.</li>
</ul>
<ul>
<li>Edit your web.config file and update the debug property to false. This holds true for all web applications.</li>
</ul>
<ul>
<li>Update the connection strings to your hosting provider&#8217;s connection strings.</li>
</ul>
<p>Thats it. Your MVC application should run fine. Mine <a href="http://ganeshzone.net/mvctestdeploy/">does</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/01/deploying-asp-net-mvc-apps-on-shared-hosting/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ASP.NET Evolution: WebForms v/s MVC</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/01/asp-net-evolution-webforms-vs-mvc/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/01/asp-net-evolution-webforms-vs-mvc/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 05:43:42 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Visual STudio 2008]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=789</guid>
		<description><![CDATA[<p style="text-align: justify;">Though MVC is an age old architectural style that has existed since the 70s, its a relatively new entrant in the .NET world. I had written a general introduction about MVC earlier. That post is available here. While Java, Ruby, Python all had their MVC frameworks for quite some time, ASP.NET was quite comfortable [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Though MVC is an age old architectural style that has existed since the 70s, its a relatively new entrant in the .NET world. I had written a general introduction about MVC earlier. That post is available <a href="http://blog.ganeshzone.net/blog/index.php/2009/11/asp-net-mvc-applications/">here</a>. While Java, Ruby, Python all had their MVC frameworks for quite some time, ASP.NET was quite comfortable with the Webform based architecture till recently. And Webforms arent really a bad way to code at all, you work with almost a Windows style of programming, dragging dropping controls. Double clicking them to code their event handlers. In fact due to this Rapid application development, ASP.NET was amazingly simple to work with. However, like with all other simple ways, ASP.NET webforms also take a lot of control away from the developer.</p>
<p style="text-align: justify;">MVC seeks to give developers an alternative, where they have complete control over the application including the viewstate of the controls. My first look was at ASP.NET MVC was a training session given by an architect from Microsoft, and I absolutely didn&#8217;t like the complexity it brought to a simple demo application that was showed. I found that we were writing too much code, again bringing back spaghetti code that died with ASP. But over time, after digging into MVC, I found that it made a lot of sense. If my need is just to display a set of data, why should I sacrifice performance over all of the gridview&#8217;s bulky viewstate. Rather, it makes sense to use a HTML table and apply CSS to style it.  That is exactly the flexibility that MVC gives you, instead of relying on built in abstraction done by Webform architecture, you get to control every detail of your application.</p>
<h2>WebForms</h2>
<p style="text-align: justify;">ASP.NET Webforms are radically different from any other server side scripting technology. For one, its event driven. Just like Windows programming, you assign an event handler to an element, like a button and it calls the handler when the button is clicked. No longer, do you have to keep on submitting the form to handle click events and write bulky code to do simple stuff. ASP.NET made the server side code clean &#8211; in fact a helluva lot cleaner than ASP.</p>
<p style="text-align: justify;">But all this ignores the age old truth about the web/HTTP &#8211; that it is stateless. The server forgets about the page the minute its sent to the client and thats the end of it. So how is context maintained in WebForms? The answer is <strong>Viewstate</strong>. All of the control states are Base64 encoded and recreated on the server again where the request is executed. There is a lot of built-in plumbing to achieve this level of abstraction.</p>
<p style="text-align: justify;">The main problem with webforms is that though it cleans up your code and markup and seperates them neatly in your Visual studio solution, it horribly clutters the rendered markup to the browser. Every control takes up more and more viewstate and the nested control names which are really long, make the markup bulky, and since this does a round trip on every request, it starts impacting performance and eventually scalability.</p>
<p style="text-align: justify;">One more concern is the Testability, due to the event driven approach, the GUI is tighly coupled with your business layer and there isnt any effective way to test your web page. Automating such testing could be an tedious effort and you would most probably end up doing manual testing.</p>
<p style="text-align: justify;">Despite all of these problems, Webforms are not a bad architecture. It is extremely mature and extensible. A lot of good documentation exists for it and it still makes sense for quite a few scenarios.</p>
<h2>MVC</h2>
<p style="text-align: justify;">With MVC, it was love at first byte. In fact I never realized how inefficient webforms were, until i worked with MVC. Instead of a page receiving the request, its the Controller which lives at the heart of MVC. Once it gets the request, it decides which view to render and may use models.</p>
<p style="text-align: justify;">One great thing about MVC is the clean Seperation of Concerns (SoC ) which you are able to achieve out of the box. Both the Controllers and the Views can be tested separately using testing tools like NUnit or MBUnit. So that means no longer. Manual testing doesn&#8217;t really make any sense for most scenarios, because the effort is not repeatable and when you want to try with a different input, you would need to start all over again.</p>
<p style="text-align: justify;">
<div id="_mcePaste" style="text-align: justify;">Also, you have complete control over the HTML that your controls generate. No more grappling with heavy Viewstate and decreased performance. Also there is no concept of a postback in MVC. Hence although, we can make use of ASP.NET server controls, we cant use either ViewState or Postback in them. Microsoft is working to create a library of MVC aware server controls that would be more suitable for this architectural style. Also, you have complete control over the HTML that your controls generate. No more grappling with heavy Viewstate and decreased performance. Also there is no concept of a postback in MVC. Hence although, we can make use of ASP.NET server controls, we cant use either ViewState or Postback in them. Microsoft is working to create a library of MVC aware server controls that would be more suitable for this architectural style.</div>
<p style="text-align: justify;">One major point that is oft repeated is that we shouldn&#8217;t jump the gun to convert every existing webforms applications into MVC just because its the in thing. You would have to rewrite your application completely, and the benefit derived would often not be worth the effort. But I would certainly  recommend that  you use MVC for any new applications you create. Its certainly the smarter way.</p>
<p style="text-align: justify;">
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/01/asp-net-evolution-webforms-vs-mvc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Thread Synchronization techniques Part 2 &#8211; The ReaderWriterLock class</title>
		<link>http://blog.ganeshzone.net/blog/index.php/2010/01/thread-synchronization-techniques-part-2-the-readerwriterlock-class/</link>
		<comments>http://blog.ganeshzone.net/blog/index.php/2010/01/thread-synchronization-techniques-part-2-the-readerwriterlock-class/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 19:22:32 +0000</pubDate>
		<dc:creator>Ganesh Ranganathan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Visual STudio 2008]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Multithreading]]></category>

		<guid isPermaLink="false">http://blog.ganeshzone.net/blog/?p=754</guid>
		<description><![CDATA[<p style="text-align: justify;">In continuation to my earlier post about multithreading in .NET, I am writing about the ReaderWriterLock class which is one more method of thread synchronization.</p>
<p style="text-align: justify;">The ReaderWriterLock is based on the fact that the Monitor locking (lock keyword in C#) doesnt really make any distinction whether the thread accessing the variable is [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">In continuation to my earlier post about multithreading in .NET, I am writing about the ReaderWriterLock class which is one more method of thread synchronization.</p>
<p style="text-align: justify;">The ReaderWriterLock is based on the fact that the Monitor locking (lock keyword in C#) doesnt really make any distinction whether the thread accessing the variable is reading it or writing to it. If its only reading it, then we should not need an exclusive lock. So If we allow multiple threads to read the value, it would be much faster than having mutually exclusive locks, which means a more scalable application? On paper, yes &#8211; but not in reality.</p>
<p style="text-align: justify;">The problem with ReaderWriterLock is with its implementation. Several experts have slammed this technique and found that outside of limited scenarios, it is actually far slower than the <strong>Monitor.Enter</strong> method used to get an exclusive lock. ReaderWriterLock gives higher priority to reader threads then writers. This makes sense if you have many readers and only a few writers. So a lot of readers are able to read the resource while the writer has to wait longer to get the lock. But what If you have equal or more writers. The process of favoring readers make writer threads queued up and take a very long time to complete.</p>
<p style="text-align: justify;">Jeffrey Richter says that this performance loss is due to support for Recursion in the ReaderWriterLock class. Due to this, the class needs to maintain a record of the number of times each thread acquires the lock and increment and decrement the counter. When multiple reader threads acquire the same lock (remember ReaderWriterLock class allows simultaneous reads), a counter is maintained for each thread. This overhead is what causes the ReaderWriterLock to pale in comparison to the Monitor class. It is approximately 6 times slower.</p>
<p style="text-align: justify;">Here is a code sample. The Threads 1 and 3 are reader threads while the Thread 2 is a writer one. When you take a look at the output, its clear that the readers are granted simultaneous access to the _value variable, but when the writer thread is writing to it, all the readers wait in queue patiently for it to finish executing. I have made the DoWorkRead method parametrized in order to identify the thread on which the method is being executed.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Program
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> _value <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    ReaderWriterLock _rwLock <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ReaderWriterLock<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <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>
        Program _prgm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Program<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;">&amp;</span>lt<span style="color: #008000;">;</span> <span style="color: #FF0000;">20</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Thread _t1Obj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span>_prgm.<span style="color: #0000FF;">DoWorkRead</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Reader Thread</span>
            <span style="color: #008080; font-style: italic;">//Writer Thread</span>
            Thread _t2Obj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ThreadStart<span style="color: #000000;">&#40;</span>_prgm.<span style="color: #0000FF;">DoWorkWrite</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//Reader Again</span>
            Thread _t3Obj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span>_prgm.<span style="color: #0000FF;">DoWorkRead</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
            <span style="color: #008080; font-style: italic;">//Start all threads</span>
            _t1Obj.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Thread 1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _t2Obj.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _t3Obj.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Thread 3&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//Wait for them to finish execution</span>
            _t1Obj.<span style="color: #0000FF;">Join</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _t2Obj.<span style="color: #0000FF;">Join</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _t3Obj.<span style="color: #0000FF;">Join</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: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> DoWorkRead<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> threadName<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//Accquire Reader Lock.</span>
        _rwLock.<span style="color: #0000FF;">AcquireReaderLock</span><span style="color: #000000;">&#40;</span>Timeout.<span style="color: #0000FF;">Infinite</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Read start: Thread: &quot;</span> <span style="color: #008000;">+</span> threadName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> _value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>threadName.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Thread 1&quot;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #008080; font-style: italic;">//Irregular sleeps makes more chances of</span>
            <span style="color: #008080; font-style: italic;">//Multiple threads trying to access it</span>
            <span style="color: #008080; font-style: italic;">//at same time</span>
            Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">else</span>
            Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">250</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Read end  : Thread: &quot;</span> <span style="color: #008000;">+</span> threadName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> _value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _rwLock.<span style="color: #0000FF;">ReleaseReaderLock</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;">//Release Lock</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> DoWorkWrite<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _rwLock.<span style="color: #0000FF;">AcquireWriterLock</span><span style="color: #000000;">&#40;</span>Timeout.<span style="color: #0000FF;">Infinite</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>Writer start: &quot;</span> <span style="color: #008000;">+</span> _value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _value<span style="color: #008000;">++;</span> <span style="color: #008080; font-style: italic;">//Writing</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Writer End: &quot;</span> <span style="color: #008000;">+</span> _value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        _rwLock.<span style="color: #0000FF;">ReleaseWriterLock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">WriteLine</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: #000000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">
<p style="text-align: justify;">A look at the output shows the once the writer acquires a lock, it is mutually exclusive, but both the reader threads are able to access the variable _value.</p>
<p style="text-align: justify;">
<p><a href="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/01/Output1.jpg"><img class="aligncenter size-full wp-image-787" title="Output" src="http://blog.ganeshzone.net/blog/wp-content/uploads/2010/01/Output1.jpg" alt="" width="314" height="334" /></a></p>
<p style="text-align: justify;">
<p style="text-align: justify;">One more problem with the ReaderWriterLock class is that it allows Reader threads to acquire writer locks. If you set an infinite timeout, it will create a deadlock situation, where the thread just waits to get the Writer lock but cant because the very same thread holds on to the Reader lock and is yet to release it. So the application just waits and waits.</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> DoWorkRead<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> threadName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//Accquire Reader Lock.</span>
            _rwLock.<span style="color: #0000FF;">AcquireReaderLock</span><span style="color: #000000;">&#40;</span>Timeout.<span style="color: #0000FF;">Infinite</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Read start: Thread: &quot;</span> <span style="color: #008000;">+</span> threadName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> _value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//NEVER EVER DO THE BELOW. IT WILL CREATE A DEADLOCK</span>
            _rwLock.<span style="color: #0000FF;">AcquireWriterLock</span><span style="color: #000000;">&#40;</span>Timeout.<span style="color: #0000FF;">Infinite</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Read start: Thread: &quot;</span> <span style="color: #008000;">+</span> threadName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> _value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _rwLock.<span style="color: #0000FF;">ReleaseReaderLock</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;">//Release Lock</span>
        <span style="color: #000000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Keeping all these issues in mind, Microsoft introduced a new class &#8211; the ReaderWriterLockSlim class from .NET 3.0 onwards, while leaving the old one in there as well, for backwards compatibility. This class placed Lock recursion in the hands of the developers by giving an enum that could be set during object recursion. With recursion out of the way, performance is dramatically better and comparable to the Monitor class. It also took care of the deadlock situation above. As a conclusion, it is always better to avoid the ReaderWriterLock class and use the slim implementation instead. If you are on .NET 2.0, then since that option is not open to you, its better to look at some other synchronization technique.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ganeshzone.net/blog/index.php/2010/01/thread-synchronization-techniques-part-2-the-readerwriterlock-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
