<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Mad about .NET</title>
    <description> A blog from Jose Fco Bonnin</description>
    <link>http://www.josefcobonnin.com/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 1.4.5.0</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://www.josefcobonnin.com/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.josefcobonnin.com/syndication.axd</blogChannel:blink>
    <dc:creator>Jose Fco Bonnin</dc:creator>
    <dc:title>Mad about .NET</dc:title>
    <geo:lat>40.260000</geo:lat>
    <geo:long>3.420000</geo:long>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/josefcobonnin" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <title>NDepend</title>
      <description>&lt;p&gt;People who knows me also knows that I'm a big fan of static analysis tools. Thanks to &lt;a href="http://codebetter.com/blogs/patricksmacchia/" target="_blank"&gt;Patrick Smacchia&lt;/a&gt; I had the chance to test &lt;a href="http://www.ndepend.com" target="_blank"&gt;NDepend&lt;/a&gt;, probably the most complete tool for static code analysis. It is not worthy to enumerate all the features the tool offers, because the documentation and the web site already do a wonderful job with it. Just to mention some of them with NDepend you will be able to measure up to 82 predefined metrics to manage things like coupling, instability, abstractness, dependencies, naming conventions ...&lt;/p&gt; &lt;p&gt;Aside all the different, some of them unique, metrics the tool incorporates there are many features that make of NDepend a great tool like: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;Possibility to incorporate new custom rules or modify the existing ones via the Code Query Language, a language with a syntax similar to any SQL language that allows you writing queries against your code structure. CQL makes so easy to create custom rules that there is no fair comparison with FxCop or StyleCop.&lt;/li&gt; &lt;li&gt;Possibility to introduce CQL constraints in the code that will allow you building really creative rules embedded in your code.&lt;/li&gt; &lt;li&gt;MSBuild and NAnt tasks to integrate with your TFS or CruiseControl.&lt;/li&gt; &lt;li&gt;Integration with Visual Studio and Reflector.&lt;/li&gt; &lt;li&gt;Different graphs that allow you seeing your code in a visual manner.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;It is also remarkable the job Patrick has done by creating several demos on how to perform different operations with NDepend to take more profit of it. You can find them online, together with the description of the different metrics and links to technical articles to his blog. It is just pity that you cannot link easier to it from the tool itself. It would be fantastic if in next versions you could just right click in the failing rule and provide an option to reach the help.&lt;/p&gt; &lt;p&gt;Regardless I think the tool is great I must say that I didn't like some of the naming conventions rules do not follow the guidelines provided by &lt;a href="http://msdn.microsoft.com/en-us/library/ms229042.aspx" target="_blank"&gt;MSDN&lt;/a&gt; and one of the greatest books about the subject: &lt;a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613?ie=UTF8&amp;amp;qid=1224001412&amp;amp;sr=11-1" target="_blank"&gt;Framework Design Guidelines&lt;/a&gt;. I also found some of the metrics a little bit arbitrary, like code should have more than 20% of comments, I completely understand you need to put a value to break a rule, but I would love to see more detailed information on how he arrived to the conclusion that some rules should have a specific value instead of another one. Of course, this is part of his .NET knowledge and programming experience. Anyway, this shouldn't be a limitation because if for any reason you do not agree with a rule, just modify it, disable it ... you can do it if you have good reasons for it!!&lt;/p&gt; &lt;p&gt;I like to see the tool not only as a bunch of rules, but as the infrastructure that will allow to build and manage your corporate standards.&lt;/p&gt; &lt;p&gt;I want to finish the post saying that I like NDepend, I think this is one of the must-have developer tools, so if you haven't tried yet go the website and get an evaluation copy to check it.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/GKYhiQZ8VoM" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/GKYhiQZ8VoM/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/06/14/NDepend.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=8eac7b3d-afd9-4d41-a24e-e62a4b6488d4</guid>
      <pubDate>Sun, 14 Jun 2009 13:09:46 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=8eac7b3d-afd9-4d41-a24e-e62a4b6488d4</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=8eac7b3d-afd9-4d41-a24e-e62a4b6488d4</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/06/14/NDepend.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=8eac7b3d-afd9-4d41-a24e-e62a4b6488d4</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=8eac7b3d-afd9-4d41-a24e-e62a4b6488d4</feedburner:origLink></item>
    <item>
      <title>Presenting at large events such as TechEd</title>
      <description>&lt;p&gt;
INETA APAC is organizing a series of best practices webcasts of interest to the community. The webcasts apply to all members of the community, leaders, user group members and MVP&amp;rsquo;s, who might not yet be at the stage where they speak at large events. The topic would be of interest to all, and would help more people to aspire to be speakers, and get an understanding of &lt;a href="http://www.josefcobonnin.com/post/2008/07/30/Speakers.aspx" target="_blank"&gt;what is involved in being a good speaker&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
Here are the details of the first webcast: 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Best Practices Webcast: Presenting at large events such as TechEd &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Session abstract:&lt;/strong&gt; Presenting at large events such as TechEd involves a lot of preparation. Too many speakers leave far too much to chance. Doing a good job does not happen by accident. In this session, Greg will share experience and tips related to the process of getting selected, preparing content and delivering that content. Many of the tips can also be applied to improving the delivery of technical material in classroom or user group situations. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Presenter Bio:&lt;/strong&gt; Greg Low is an internationally recognized consultant, developer and trainer. He is the country lead for Solid Quality, a SQL Server MVP and one of only three Microsoft Regional Directors for Australia. Greg also hosts the popular SQL Down Under podcast (www.sqldownunder.com), is a board member of PASS (the Professional Association for SQL Server) and an author with Microsoft Press and Rational Press. He regularly speaks at large events around the world. 
&lt;/p&gt;
&lt;p&gt;
INETA APAC invites you to select a convenient time of your choice for this webcast, fill the survey(takes less than 3 minutes) and we will notify you of the final survey results and the exact webcast timing.&lt;br /&gt;
&lt;a href="http://www.surveymonkey.com/s.aspx?sm=AdhgSxdrRqvyUP9KZQRWpA_3d_3d"&gt;http://www.surveymonkey.com/s.aspx?sm=AdhgSxdrRqvyUP9KZQRWpA_3d_3d&lt;/a&gt;&lt;br /&gt;
Please note that Survey closing date is : May 4, 2009
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/nnyQl4h6Kdg" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/nnyQl4h6Kdg/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/04/29/Presenting-at-large-events-such-as-TechEd.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=d86896ef-37af-4183-953e-d15dea8ce706</guid>
      <pubDate>Wed, 29 Apr 2009 15:10:00 -0300</pubDate>
      <category>Ineta</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=d86896ef-37af-4183-953e-d15dea8ce706</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=d86896ef-37af-4183-953e-d15dea8ce706</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/04/29/Presenting-at-large-events-such-as-TechEd.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=d86896ef-37af-4183-953e-d15dea8ce706</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=d86896ef-37af-4183-953e-d15dea8ce706</feedburner:origLink></item>
    <item>
      <title>Static Code Analysis</title>
      <description>&lt;p&gt;These days I've been updating the CA settings on some old projects, I wanted to modify the configuration of certain rules that were removed with the shipping of Visual Studio 2008. You can obtain detailed information about what rules are shipped with each version of CA in &lt;a href="http://blogs.msdn.com/fxcop/archive/2008/01/07/faq-which-rules-shipped-in-which-version.aspx" target="_blank"&gt;this post of the FxCop blog&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;As you can see in the post, some of the rules were removed because they do not apply anymore or because they were too noisy compared with the benefit introduced. One of the rules that make feel more upset when I knew it was removed was &lt;em&gt;"CA1818 - Do not concatenate strings inside loops".&lt;/em&gt; This rule threw an error (I will continue in my imaginary world where everybody sets warnings as errors in production code) with code like:&lt;/p&gt; &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt; s = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 5; i++)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    s += i.ToString();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;One of the first things you learn in .NET is about the immutability of the strings, you can find lot of literature talking about how to handle properly strings. Even &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=8A2E454D-F30E-4E72-B531-75384A0F1C47&amp;amp;displaylang=en" target="_blank"&gt;Improving .NET Application Performance and Scalability&lt;/a&gt;, one of the best papers I've seen about .NET performance, makes an explicit reference to do not concatenate strings when the number of concatenations is unknown.&lt;/p&gt;
&lt;p&gt;So, today that I've been working again with the rule, I had the curiosity (hope) to verify the rule was not removed because it was too noisy but because the CLR was improved to avoid the issue. I know this is again living in my imaginary world, but I'm a bit stubborn...what I've done is to compile a console project with the code above with 2.0 (VS 2005) and 3.5 (VS 2008), first one fires the error, second one doesn't. First step has been to look for differences in the IL generated, both cases have:&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;.entrypoint&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="rem"&gt;// Code size       39 (0x27)&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;.maxstack  2&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;.locals init ([0] &lt;span class="kwrd"&gt;string&lt;/span&gt; s,&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;       [1] int32 i)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;IL_0000:  ldsfld   &lt;span class="kwrd"&gt;string&lt;/span&gt; [mscorlib]System.String::Empty&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;IL_0005:  stloc.0&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;IL_0006:  ldc.i4.0&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;IL_0007:  stloc.1&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;IL_0008:  br.s     IL_001c&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;IL_000a:  ldloc.0&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;IL_000b:  ldloca.s i&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;IL_000d:  call     instance &lt;span class="kwrd"&gt;string&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;                   [mscorlib]System.Int32::ToString()&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;IL_0012:  call     &lt;span class="kwrd"&gt;string&lt;/span&gt; [mscorlib]System.String::Concat(&lt;span class="kwrd"&gt;string&lt;/span&gt;,&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;                                                          &lt;span class="kwrd"&gt;string&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;IL_0017:  stloc.0&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;IL_0018:  ldloc.1&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;IL_0019:  ldc.i4.1&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;IL_001a:  add&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;IL_001b:  stloc.1&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;IL_001c:  ldloc.1&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;IL_001d:  ldc.i4.5&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;IL_001e:  blt.s    IL_000a&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;IL_0020:  call     valuetype [mscorlib]System.ConsoleKeyInfo &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;                   [mscorlib]System.Console::ReadKey()&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;IL_0025:  pop&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;IL_0026:  ret&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;No improvements on the compiler side. Next step has been to verify the native code generated, for that I used the command &lt;em&gt;!u&lt;/em&gt; at WinDbg (&lt;a href="http://www.josefcobonnin.com/post/2008/08/03/Inline-Methods.aspx" target="_blank"&gt;in this previous post&lt;/a&gt; you can see how to obtain the native code after the method is jitted). The code for both projects looks like (note that some memory addresses will be different):&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;CA1818.Program.Main(System.String[])&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;Begin 009d0070, size 55&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;009d0070 55           push ebp&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;009d0071 8bec         mov  ebp,esp&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;009d0073 57           push edi&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;009d0074 56           push esi&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;009d0075 83ec10       sub  esp,10h&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;009d0078 33c0         xor  eax,eax&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;009d007a 8945f4       mov  dword ptr [ebp-0Ch],eax&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;009d007d 8b3d2c10d602 mov  edi,dword ptr ds:[2D6102Ch] (&lt;span class="str"&gt;""&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;009d0083 33d2         xor  edx,edx&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;009d0085 8955f4       mov  dword ptr [ebp-0Ch],edx&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;009d0088 837df405     cmp  dword ptr [ebp-0Ch],5&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;009d008c 7d26         jge  009d00b4&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;009d008e 8b75f4       mov  esi,dword ptr [ebp-0Ch]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;009d0091 e80a0cca6f   call mscorlib_ni+0x220ca0 (70670ca0) &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;009d0096 50           push eax&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;009d0097 8bce         mov  ecx,esi&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;009d0099 33d2         xor  edx,edx&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;009d009b e8bad00971   call &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;                      mscorwks!LogHelp_TerminateOnAssert+0xb82 &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                      (71a6d15a) &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;009d00a0 8bd0         mov  edx,eax&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;009d00a2 8bcf         mov  ecx,edi&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;009d00a4 e8a7ebc36f   call mscorlib_ni+0x1bec50 (7060ec50)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;009d00a9 8bf8         mov  edi,eax&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;009d00ab ff45f4       inc  dword ptr [ebp-0Ch]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;009d00ae 837df405     cmp  dword ptr [ebp-0Ch],5&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;009d00b2 7cda         jl   009d008e&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;009d00b4 8d4de8       lea  ecx,[ebp-18h]&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;009d00b7 33d2         xor  edx,edx&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;009d00b9 e8f6381570   call mscorlib_ni+0x6d39b4 (70b239b4)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;009d00be 8d65f8       lea  esp,[ebp-8]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;009d00c1 5e           pop  esi&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;009d00c2 5f           pop  edi&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;009d00c3 5d           pop  ebp&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;009d00c4 c3           ret&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;We see there are no improvements either on the jitter side. The next to verify is if the strings are being discarded on each iteration creating a new one or not. To do that I used some good profilers that are on the market but in the end I decided to show how I did it with WinDbg because anybody can download it for free.&lt;/p&gt;
&lt;p&gt;With &lt;em&gt;!DumpHeap -type System.String&lt;/em&gt; we can see all the string instances of our application. This returns a list with the memory address of the string instances, to view the contents of the object we just need to do a &lt;em&gt;!do (&lt;/em&gt;dump object) of the address we want to check. So, just taking some samples from the instances with higher memory addresses we can already see the next:&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;0:003&amp;gt; !&lt;span class="kwrd"&gt;do&lt;/span&gt; 01c83ae0 &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;Name: System.String&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;MethodTable: 706c0a00&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;EEClass: 7047d64c&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;Size: 24(0x18) bytes&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;String: 012&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;0:003&amp;gt; !&lt;span class="kwrd"&gt;do&lt;/span&gt; 01c83af8 &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;Name: System.String&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;MethodTable: 706c0a00&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;EEClass: 7047d64c&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;Size: 20(0x14) bytes&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;String: 3&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;0:003&amp;gt; !&lt;span class="kwrd"&gt;do&lt;/span&gt; 01c83b0c&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;Name: System.String&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;MethodTable: 706c0a00&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;EEClass: 7047d64c&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;Size: 26(0x1a) bytes&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;String: 0123&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;0:003&amp;gt; !&lt;span class="kwrd"&gt;do&lt;/span&gt; 01c83b28&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;Name: System.String&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;MethodTable: 706c0a00&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;EEClass: 7047d64c&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;Size: 20(0x14) bytes&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;String: 4&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;0:003&amp;gt; !&lt;span class="kwrd"&gt;do&lt;/span&gt; 01c83b3c &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;Name: System.String&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;MethodTable: 706c0a00&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;EEClass: 7047d64c&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;Size: 28(0x1c) bytes&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;String: 01234&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;From the results above we can see how on each iteration we have two strings, the resulting of i.ToString() and the resulting of concatenation. &lt;/p&gt;
&lt;p&gt;Conclusion, we still creating new strings and discarding the previous version for GC on each manipulation of the string. Therefore, I suppose the rule was removed just because it was too noisy. &lt;/p&gt;
&lt;p&gt;Once I stopped playing with WinDbg I come back to the earth to say what I wanted to say from the beginning. It's pity to see that a performance issue that has been repeated so many times, now is ignored just because the rule is too noisy. I know lot of people could argue that using the StringBuilder we also discard old versions of strings when the size of the string becomes bigger than the buffer, but still better than discarding all modifications. &lt;/p&gt;
&lt;p&gt;I don't understand why a good string handling was that important before and now is just ignored by the main CA tool used by .NET developers.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/_aWll92w_Nk" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/_aWll92w_Nk/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (Jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/04/23/Static-Code-Analysis.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=0469c152-5bbe-4fbf-a39e-e27021f23bbb</guid>
      <pubDate>Thu, 23 Apr 2009 15:45:06 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>Jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=0469c152-5bbe-4fbf-a39e-e27021f23bbb</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=0469c152-5bbe-4fbf-a39e-e27021f23bbb</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/04/23/Static-Code-Analysis.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=0469c152-5bbe-4fbf-a39e-e27021f23bbb</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=0469c152-5bbe-4fbf-a39e-e27021f23bbb</feedburner:origLink></item>
    <item>
      <title>Signing with Certificates</title>
      <description>&lt;p&gt;Recently, I recorded a Spanish video about how to encrypt/sign information using certificates and I thought it would be nice to write also a post about it. The thing is that when I tried to post it I received an alert about having a post with the same title, I already wrote this post &lt;a href="http://www.josefcobonnin.com/post/2007/02/20/Encrypting-with-Certificates.aspx" target="_blank"&gt;more than two years ago&lt;/a&gt; :(, so while I try to figure out why I have this memory leak in my brain I will write only the missed part: how sign data using X509 certificates.&lt;/p&gt; &lt;p&gt;Since we already have in the previous post the code to load certificates we will focus on two methods Sign and VerifyHash.&lt;/p&gt; &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] Sign(&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] hash, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    X509Certificate2 certificate)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (hash == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"hash"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (certificate == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"certificate"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (RSACryptoServiceProvider provider = &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="kwrd"&gt;new&lt;/span&gt; RSACryptoServiceProvider())&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    { &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        provider.FromXmlString(&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            certificate.PrivateKey.ToXmlString(&lt;span class="kwrd"&gt;true&lt;/span&gt;));&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; provider.SignHash(&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            hash,&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;            CryptoConfig.MapNameToOID(&lt;span class="str"&gt;"SHA1"&lt;/span&gt;));&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    }  &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; VerifyHash(&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] hash, &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] signature, &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    X509Certificate2 certificate)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (hash == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"hash"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (signature == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"signature"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (certificate == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"certificate"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (RSACryptoServiceProvider provider = &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;        &lt;span class="kwrd"&gt;new&lt;/span&gt; RSACryptoServiceProvider())&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;        provider.FromXmlString(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;            certificate.PublicKey.Key.ToXmlString(&lt;span class="kwrd"&gt;false&lt;/span&gt;));&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; provider.VerifyHash(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;            hash, &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;            CryptoConfig.MapNameToOID(&lt;span class="str"&gt;"SHA1"&lt;/span&gt;), &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;            signature);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;The idea behind these two methods is the next: &lt;/p&gt;
&lt;p&gt;We have one party A who needs to send a message to B. B wants to be sure that the messaged received has not been modified by a third party C. To do that, A will create a digital signature that will be sent together with the message. To generate the signature A needs to compute a hash of the message, this hash is then used with method "Sign". Once A has the signature he can transmit the message and the signature to B.&lt;/p&gt;
&lt;p&gt;When B receives the message and the signature, B needs to verify the hash. So, B computes again a hash of the received message and uses the method VerifyHash, which will return true if the message has not been modified. The next code shows you an example on how to call both methods.&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;string&lt;/span&gt; certificateName = &lt;span class="str"&gt;"Test"&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    X509Certificate2 cert = LoadCertificate(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        StoreName.My, &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        StoreLocation.LocalMachine, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        certificateName);  &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] messageSent = &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        UTF8Encoding.UTF8.GetBytes(&lt;span class="str"&gt;"Message sent from A to B"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    &lt;span class="rem"&gt;// A generates the signature before send to B&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    SHA1Managed sha1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; SHA1Managed();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] hashA = sha1.ComputeHash(messageSent);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] signature = Sign(hashA, cert);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    &lt;span class="rem"&gt;// B receives the message and the digital signature&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] messageReceived = &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        UTF8Encoding.UTF8.GetBytes(&lt;span class="str"&gt;"Message sent from A to B"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    &lt;span class="rem"&gt;//byte[] messageReceived = &lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;    &lt;span class="rem"&gt;//  UTF8Encoding.UTF8.GetBytes("Message hacked by C");&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;    &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] hashB = sha1.ComputeHash(messageReceived);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (VerifyHash(hashB, signature, cert))&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        Console.WriteLine(&lt;span class="str"&gt;"Verified"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;        Console.WriteLine(&lt;span class="str"&gt;"Not verified"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;    Console.ReadKey();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;This piece of code using the rest of methods mentioned shows you how we can verify a message has not been altered during the transmission. If you comment lines 19,20 and uncomment 21,22 you will see that after message has been altered the hash cannot be validated anymore. These short methods are one the basis of security when we try to guarantee information is not being altered from origin to destine. We have used a very basic message, but imagine you have an online shop and you don´t want people can alter from the message the number of items that will be delivered after you have authorized a payment.&lt;/p&gt;
&lt;p&gt;Checking similar methods like SignData and VerifyData will be left as an exercise for the reader.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/6jl40Zs8erc" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/6jl40Zs8erc/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (Jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/04/21/Signing-with-Certificates.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=361a63a4-bb42-49cb-b5eb-5c63fc8cb2d2</guid>
      <pubDate>Tue, 21 Apr 2009 14:22:32 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>Jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=361a63a4-bb42-49cb-b5eb-5c63fc8cb2d2</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=361a63a4-bb42-49cb-b5eb-5c63fc8cb2d2</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/04/21/Signing-with-Certificates.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=361a63a4-bb42-49cb-b5eb-5c63fc8cb2d2</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=361a63a4-bb42-49cb-b5eb-5c63fc8cb2d2</feedburner:origLink></item>
    <item>
      <title>Singleton Pattern</title>
      <description>&lt;p&gt;Yesterday I was viewing a &lt;a href="http://channel9.msdn.com/posts/Daniel+Garzon/Patron-Singleton/" target="_blank"&gt;video&lt;/a&gt; (Spanish) that explains about how to implement the Singleton pattern in .NET. After some implementations the video ends up showing the best way to have a thread safe implementation of the singleton pattern.&lt;/p&gt;
&lt;p&gt;The problem is the video ends saying that the next two implementations are exactly the same on a functional level, which is not completely true. &lt;/p&gt;
&lt;p&gt;Implementation 1&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton1&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; DateTime creationTime;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton1 instance = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; synchro = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Singleton1()&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        creationTime = System.DateTime.Now;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton1 Instance&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        get&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (instance == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;                &lt;span class="kwrd"&gt;lock&lt;/span&gt; (synchro)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;                {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (instance == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;                    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                        instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton1();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;                    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;                }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; instance;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; CreationTime&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;        get&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; creationTime.ToString(&lt;span class="str"&gt;"hh:mm:ss.ffffff"&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Implementation 2&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton2&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; DateTime creationTime;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton2 instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton2();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Singleton2()&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        creationTime = System.DateTime.Now;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton2 Instance&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        get&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; instance;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; CreationTime&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;        get&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; creationTime.ToString(&lt;span class="str"&gt;"hh:mm:ss.ffffff"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;The second implementation is correct, since it shows how you can avoid the lock while creating the object instance of the Singleton class. The reason why this is thread safe is because .NET guarantees that the &lt;a href="http://msdn.microsoft.com/en-us/library/ms229018.aspx" target="_blank"&gt;Type Constructor&lt;/a&gt; (or &lt;a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-335.pdf" target="_blank"&gt;Type Initializer&lt;/a&gt; on the ECMA standard) is executed only once per AppDomain and is thread-safe.&lt;/p&gt;
&lt;p&gt;Regardless both of them are thread-safe, the difference is that the first one is lazy loaded while the second one is eager loaded. In order to implement the singleton pattern using a lazy loaded version and still take profit of the Type Constructors, you need to add a container for the object instance of Singleton2. Doing this the Type Constructor will only be initialized when you access the property (or any other member making reference to it). Below you can see the correct implementation for it.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; LazyLoading&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; DateTime creationTime;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; LazyLoading()&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        creationTime = DateTime.Now;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; LazyLoading Instance&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        get&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; SingletonContainer.instance;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; CreationTime&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        get&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; creationTime.ToString(&lt;span class="str"&gt;"hh:mm:ss.ffffff"&lt;/span&gt;);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; SingletonContainer&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; LazyLoading instance = &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; LazyLoading();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/40yzE3JyLyE" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/40yzE3JyLyE/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/04/13/Singleton-Pattern.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=dd308c4b-2bd5-4e5d-b670-3d91e46d3302</guid>
      <pubDate>Mon, 13 Apr 2009 14:13:00 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=dd308c4b-2bd5-4e5d-b670-3d91e46d3302</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=dd308c4b-2bd5-4e5d-b670-3d91e46d3302</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/04/13/Singleton-Pattern.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=dd308c4b-2bd5-4e5d-b670-3d91e46d3302</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=dd308c4b-2bd5-4e5d-b670-3d91e46d3302</feedburner:origLink></item>
    <item>
      <title>User Group Support Services</title>
      <description>&lt;p&gt;I have recently started to volunteer as UGSS Regional Lead for EMEA.&lt;/p&gt; &lt;p&gt;For those who know UGSS and the RL, you will know that this is a position covered by Microsoft personnel, my case is a bit different because I agreed to volunteer during this FY to help out Sanjay Shetty who is the official responsible for EMEA and APAC.&lt;/p&gt; &lt;p&gt;If you do not know the UGSS I highly suggest you visit &lt;a href="http://ugss.codezone.com"&gt;http://ugss.codezone.com&lt;/a&gt;, where you will be able to find more information about what UGSS does for the community. &lt;/p&gt; &lt;p&gt;As our manager Graham Jones already mentioned in this &lt;a href="http://blogs.technet.com/grahamtwatson/archive/2008/10/02/announcing-the-ugss-regional-leads.aspx" target="_blank"&gt;post&lt;/a&gt;, the role of a regional lead is to help user group leaders to be as successful as possible independently to which organization, if any, their user groups belong. So, if you have any question or suggestion we all will be extremely glad to help you.&lt;/p&gt; &lt;p&gt;In this short period as RL, I have had the possibility to be amazed for the huge amount of projects, initiatives my colleagues are involved. &lt;/p&gt; &lt;p&gt;I'm still landing but I'm pretty sure the community will be also amazed about the good things that are coming out of the work the UGSS does.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/heQFcoQ1oKM" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/heQFcoQ1oKM/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/04/04/User-Group-Support-Services.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=1558b66b-4dab-4765-8d43-e8c1304bdfd4</guid>
      <pubDate>Sat, 04 Apr 2009 14:56:49 -0300</pubDate>
      <category>General</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=1558b66b-4dab-4765-8d43-e8c1304bdfd4</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=1558b66b-4dab-4765-8d43-e8c1304bdfd4</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/04/04/User-Group-Support-Services.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=1558b66b-4dab-4765-8d43-e8c1304bdfd4</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=1558b66b-4dab-4765-8d43-e8c1304bdfd4</feedburner:origLink></item>
    <item>
      <title>Docking as Windows 7 in Vista and XP</title>
      <description>&lt;p&gt;I was one of those lucky guys who got the Windows 7 virtual machine at the PDC for the first time and I already liked. When the new beta was released I decided to install it on my old laptop and I love it, I really mean it. I think it's great, there are a lot of small features that you immediately like with almost not noticing them. One of them is the cool docking feature that allows you drag a window over the limits of the screen to change it's size and position. &lt;/p&gt; &lt;p&gt;When I saw I immediately wondered how this could be done using C#. The idea would be to detect when a window is being dragged to the limits of our screen, when it reaches them display a frame and finally change the size and position of the window being dragged.&amp;nbsp; &lt;/p&gt; &lt;p&gt;At a first view, using some &lt;a href="http://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx" target="_blank"&gt;Windows Hook&lt;/a&gt; and capturing a couple of messages can do the trick. The problem is that we can only install global hooks for WH_KEYBOARD_LL and WH_MOUSE_LL, the other global hooks require a native DLL export to inject itself in another process. This is already a problem because discards the possibility to monitor messages before and after they are processed by a window,&amp;nbsp; in addition we cannot use either the MouseProc that gives us information about the mouse and more important the value of the HitTest, which is necessary to know what part of the window the user is clicking. What we will do is to try to use the WH_MOUSE_LL with some work around. I already wrote about hooks more than one year ago, you can read it &lt;a href="http://www.josefcobonnin.com/post/2007/06/19/Hook-Keyboard-and-Mouse.aspx" target="_blank"&gt;here&lt;/a&gt; if you are not familiarized with them. &lt;/p&gt; &lt;p&gt;With that hook we will obtain information about mouse events, we will focus on the messages WM_LBUTTONDOWN, WM_MOUSEMOVE and WM_LBUTTONUP. In the attached code I created a hook helper that captures the hook callbacks and generates .NET events to facilitate its use. You can see below the event handler: &lt;/p&gt; &lt;div class="csharpcode"&gt; &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; hookHelper_HookMouseEvent(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, 
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    HookMouseEventArgs e)
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;switch&lt;/span&gt;(e.Message)
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;case&lt;/span&gt; NativeConstants.WM_LBUTTONDOWN:
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            CheckWindow(e.Point);
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        &lt;span class="kwrd"&gt;case&lt;/span&gt; NativeConstants.WM_MOUSEMOVE:
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            CheckPosition(e.Point);
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="kwrd"&gt;case&lt;/span&gt; NativeConstants.WM_LBUTTONUP:
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            ResizeWindow(e.Point);
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;    }
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;}
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In the method CheckWindow we decide if we Window where the user has clicked is a window we are interested to monitor, to do it we retrieve the window handle using the API WindowFromPoint and doing a Hit test over the window. The Hit test can be done by sending the message WM_NCHITTEST specifying the handle of the window and the coordinates of the point, this will return us a value indicating what part of the window has been clicked, in our case we are interested only if the user clicks the Title of the window, which value is HTCAPTION. &lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CheckWindow(Point point)
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    Win32.NativeStructs.Point p = 
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        &lt;span class="kwrd"&gt;new&lt;/span&gt; Win32.NativeStructs.Point();
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    p.x = point.X;
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    p.y = point.Y;
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    windowHandle = NativeMethods.WindowFromPoint(p);
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (windowHandle != IntPtr.Zero)
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    {
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        IntPtr lParam = &lt;span class="kwrd"&gt;new&lt;/span&gt; IntPtr(65536 * p.y + p.x);
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        IntPtr hitTestPtr = NativeMethods.SendMessage(windowHandle, 
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            (&lt;span class="kwrd"&gt;uint&lt;/span&gt;)NativeConstants.WM_NCHITTEST, (&lt;span class="kwrd"&gt;uint&lt;/span&gt;)0, lParam);
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; ((&lt;span class="kwrd"&gt;int&lt;/span&gt;)hitTestPtr == NativeConstants.HTCAPTION)
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        {
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            dragging = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        }
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    }
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;}
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The messages, return values and functions are declared in the header Winuser.h, the easier to have all the information about the windows API is to go to MSDN, of course, but also to download the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=F26B1AA4-741A-433A-9BE5-FA919850BDBF&amp;amp;displaylang=en" target="_blank"&gt;Windows SDK&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Next step is to give to the user the visual clue that something will happen with the window, to do it Windows 7 shows an animation below the pointer and a frame simulating where the window will be placed. To do it we will not code any fancy thing, we can simulate it by just opening a standard .NET form setting its properties ControlBox, ShowInTaskBar and TransparencyKey to our desired values. To know the size we need to give to our frame we just need to check the property WorkingArea from the .NET class System.Windows.Forms.SystemInformation. The only trick we will do is to display the form with the function SetWindowPos, because it allow us controlling the Z-Order of the window and to open the form without activate it, this is handy to avoid our fake frame is placed on top of the window we are actually moving. To do it we use the function SetWindowPos including the handle of the window to insert after and the parameter SWP_NOACTIVATE. &lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;NativeMethods.SetWindowPos(frame.Handle,
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    windowHandle,
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    0, 0, 
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    SystemInformation.WorkingArea.Width / 2, 
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    SystemInformation.WorkingArea.Height,
&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    NativeConstants.SWP_SHOWWINDOW | 
&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    NativeConstants.SWP_NOACTIVATE);
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We are near to the end, we just need to complete the last step, change the size and position of the window to the desired the values using again the function SetWindowPos for the left and right placement. To maximize we just use the function ShowWindow passing as parameter SW_SHOWMAXIMIZED. &lt;/p&gt;
&lt;p&gt;Windows 7 also restores the original size of the window after you drag away from the docked position, this could be done by capturing first the size and position of the window using the functions GetWindowPlacement and SetWindowPlacement, if you use them do not forget to set the length of your WindowPlacement structure using Marshal.SizeOf(). &lt;/p&gt;
&lt;p&gt;If you are not familiarized with P/Invoke in .NET you can make use of tools like &lt;a href="http://www.codeplex.com/clrinterop/" target="_blank"&gt;PInvoke Interop Assistant&lt;/a&gt; from Microsoft CLR Interop Team or the &lt;a href="http://www.pinvoke.net/" target="_blank"&gt;P/Invoke&lt;/a&gt; wiki. &lt;/p&gt;
&lt;p&gt;Now that all seems to be under control, if you download the code you will see that we have a big problem. When we release the button the size doesn't seem to change, but it actually changes to the size and position we have given.&amp;nbsp; The problem is that the window is in the moving state and when we release the button after set our size, it comes back to the last size the window had before releasing the mouse button. If we make use of Microsoft Spy++ we can see that after set the position the window still receives the messages like WM_WINDOWPOSCHANGED or WM_EXITSIZEMOVE. &lt;/p&gt;
&lt;p&gt;I've tried with some of the messages captured previously using the function SendMessage to see if I could exit the moving/sizing state but without success. So, at the end I couldn't resolve it using C#, if you have ideas on how this can be solved they will be welcome. As I was joking with a friend a couple of days ago I feel I've lost my "mojo". &lt;/p&gt;
&lt;p&gt;It is curious to see how our mind works, when I completely stopped thinking about what message send in order to make it work, I thought I could try the last trick by setting the size after the resize was completed. The way to do that is pretty simply, we just need to enable a timer that starts after we capture the WM_LBUTTONUP.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.josefcobonnin.com/file.axd?file=2009%2f1%2fMadAboutNet.WindowsDocker.zip"&gt;MadAboutNet.WindowsDocker.zip (404.63 kb)&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/Fi4W_PdpmNs" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/Fi4W_PdpmNs/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (Jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/01/31/Docking-as-Windows-7-in-Vista-and-XP.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=97abf97e-8532-44c9-b47f-8bcdc8180cb6</guid>
      <pubDate>Sat, 31 Jan 2009 16:32:00 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>Jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=97abf97e-8532-44c9-b47f-8bcdc8180cb6</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=97abf97e-8532-44c9-b47f-8bcdc8180cb6</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/01/31/Docking-as-Windows-7-in-Vista-and-XP.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=97abf97e-8532-44c9-b47f-8bcdc8180cb6</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=97abf97e-8532-44c9-b47f-8bcdc8180cb6</feedburner:origLink></item>
    <item>
      <title>Xml Documentation Comments - Exceptions II</title>
      <description>&lt;p&gt;People who has been working with me knows that I'm a big fan of FxCop (or Code Analysis), I think static code analysis tools are very useful and a must-have in any development team, since ensure developers follow the company's guidelines and&amp;nbsp; write correct code (at least it helps a lot). In addition, I see them also as a great ally to improve the learning curve of new developers.&lt;/p&gt; &lt;p&gt;While FxCop analyzes managed code assemblies checking for improvements&amp;nbsp; about design, localization, performance, security, interoperability, etc.&amp;nbsp; &lt;a href="http://blogs.msdn.com/sourceanalysis/" target="_blank"&gt;StyleCop&lt;/a&gt; focuses mainly in code style guidelines.&lt;/p&gt; &lt;p&gt;In the previous &lt;a href="http://www.josefcobonnin.com/post/2009/01/11/Xml-Documentation-Comments-Exceptions-I.aspx"&gt;post&lt;/a&gt; we saw how to document exceptions using the Xml Documentation features. Here we will see how to take profit of &lt;a href="http://code.msdn.microsoft.com/sourceanalysis" target="_blank"&gt;StyleCop SDK&lt;/a&gt;&amp;nbsp; to create a custom rule that validates we are documenting those exceptions.&lt;/p&gt; &lt;p&gt;To create our rule we just need to create a class library project, add references to the assemblies Microsoft.StyleCop.dll and Microsoft.StyleCop.CSharp.dll, create a class that inherits from SourceAnalyzer, add an attribute and embed an Xml file with the necessary Metatada for our rules. All these steps are entirely documented in the StyleCop SDK documentation, so I do not waste too much time explaining it.&lt;/p&gt; &lt;p&gt;Before we start we need to know that the StyleCop engine incorporates a custom C# language service that is used to create a rich model representing the original C# code. What our rule will do is to use that model to visit the different elements that can throw exceptions, analyze the element's body to find the throw statements and extract the type of that exceptions, it will extract the exceptions documented using the Xml comments, finally it will compare the thrown exceptions with the documented ones and will add a violation for all the discrepancies found.&lt;/p&gt; &lt;p&gt;We know that we can use throw statements inside methods (including constructors), indexers and properties. So, taking profit of the Visitor implementation that the API provide us we will visit all the CsElement of a CsDocument looking for the declared methods, indexers and properties. Below you can see the callback method we have passed to the WalkDocument method.&lt;/p&gt; &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; VisitElements(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    CsElement element, &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    CsElement parentElement, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; usingDirectives)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.Cancel)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    &lt;span class="rem"&gt;// We store the using directives for future use&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (element.ElementType == ElementType.UsingDirective)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        usingDirectives.Add(&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            ((UsingDirective)element).NamespaceType);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    &lt;span class="rem"&gt;// There are severeal element types that can throw exceptions: &lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    &lt;span class="rem"&gt;// Methods, Constructors, Indexers and Properties&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (!element.Generated &amp;amp;&amp;amp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        (element.ElementType == ElementType.Method || &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;         element.ElementType == ElementType.Constructor ||&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;         element.ElementType == ElementType.Indexer || &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;         element.ElementType == ElementType.Property))&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.CheckExceptionDocumentation(element, &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;            usingDirectives);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Lines 17 to 24 allow us filtering the elements for which we are going to check the documentation. You can see also from line 10 to 13 that we store the using directives defined in the document, later on I will explain why. &lt;/p&gt;
&lt;p&gt;The next is to analyze more in detail each element we have filtered, what we will do is first look for the exception tags added&amp;nbsp; in the Xml comments and then compare them with the type of the exceptions thrown in the given element. The first step is as easy as perform a Linq Xml query over a property called Header the CsElement has. For the second one we will create a CodeWalkerStatementVisitor that we will pass to the WalkElement method, this callback will be used to iterate the element's statements looking for all the the throw statements defined.&lt;/p&gt;
&lt;p&gt;To do it we only need to check the StatementType property belogning to the class Statement and make sure its type is StatementType.Throw. Once we have the ThrowStatement we can investigate the type of the exception that will be thrown. The demo rule can analyze the next scenarios:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;New expressions. i.e. throw new System.Exception(); 
&lt;li&gt;Literal expressions. i.e. var ex = new Exception(); throw ex; 
&lt;li&gt;Method invocation expressions. i.e. throw GetMyExceptionInstance(); &lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;The ThrowStatement class has a property called ThrownExpression that allow us identifying the expression used to throw the exception. &lt;/p&gt;
&lt;p&gt;For the first scenario, we need to iterate the different tokens of the NewExpression and extract the exception type. This is done in the code using the next method.&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ExtractExceptionType(NewExpression expression)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;for&lt;/span&gt; (Node&amp;lt;CsToken&amp;gt; node = expression.Tokens.First; node != &lt;span class="kwrd"&gt;null&lt;/span&gt;; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        node = node.Next)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        CsToken token = node.Value;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (token.CsTokenType == CsTokenType.Other)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; token.Text;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;For the second scenario we have a small handicap, the literal represents a variable that is not defined in the throw statement, instead it is defined in upper statements or even in elements different than the one where the exception is thrown i.e.&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;ArgumentNullException ex = &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoSomething(&lt;span class="kwrd"&gt;string&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;value&lt;/span&gt; == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ex;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;In the code above the element containing the throw statement is the method DoSomething, but the variable ex is defined in a class element.&lt;/p&gt;
&lt;p&gt;For this reason before we can obtain the type of the exception, we first need to retrieve the variables defined by the parent statements of the throw statement. After that we just need to find the variable among the retrieved ones to determine its type and return it for further analysis.&lt;/p&gt;
&lt;p&gt;The third scenario is the MethodInvocationExpression, here we have the biggest handicaps because there are many possibilities in which we can obtain an exception from a method. The easiest case is a direct method call, we will have MethodInvocationExpression containing the LiteralExpression with the name of the method, but imagine the next situation: &lt;/p&gt;
&lt;p&gt;throw new MyClass().GetException(); &lt;/p&gt;
&lt;p&gt;In this case, the expression is a member access and we need to go to the right side to find the literal with the name of the method.&lt;/p&gt;
&lt;p&gt;Once we have the obtained the name of the method, we can perform a search in the Document looking for that Method element in order to retrieve its return type that, of course, will be our exception type. We do not need to worry about overloads with different return types, because even if the CLR supports it C# does not and we are analyzing C# code. &lt;/p&gt;
&lt;p&gt;At this point we have been able to retrieve all the exception types thrown by the throw statements and we can compare with the ones we have documented. Unfortunately we are not done yet and there is another point to solve.&lt;/p&gt;
&lt;p&gt;The problem we have is that the object model representing the code we analyze, does not stores the type of the variables , return types of methods, etc. as the .NET type "Type". Instead they are represented in best case as a TokenType with a text representation. This has sense for many reasons, one is because StyleCop analyzes code that doesn't necessarily compiles therefore it's possible the type does not exists yet. &lt;/p&gt;
&lt;p&gt;So, how this affects to the rule? Since we do not have the real types for the Exceptions we want to verify but string representations of them, the next code will cause a false positive:&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="rem"&gt;/// &amp;lt;exception cref="System.Exception"&amp;gt;&amp;lt;/exception&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoSomething()&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Exception();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The false positive appears because at the time we compare the type throw with the type documented we will compare "System.Exception" with "Exception". That's the reason why we have stored the using directives in a previous step, because before to compare we will try to obtain the full type name doing a Type.GetType() concatenating the different namespaces imported into the document. In case, that we are still not able to get the type (i.e. exception defined in a different assembly not accessible from StyleCop) we will compare both texts without the namespace, this is using Exception instead of System.Exception.&lt;/p&gt;
&lt;p&gt;I'm really curious to see if StyleCop will be benefited when the new managed C# compiler is there, part of the project to offer the C# compiler as a service will include a complete language object model. I suppose it will be a natural step to use that new model, but this is way far from today since, regardless the C# team is already working on it, it is supposed to come after C# 4.0.&lt;/p&gt;
&lt;p&gt;With this rule we are covering the most common scenarios where exceptions are thrown, but there are cases that will fail. i.e. If the exception thrown is obtained from a method declared in a different document, the rule will not be able to find that method and consequently the type of the exception.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.josefcobonnin.com/file.axd?file=2009%2f1%2fStyleCopRule.zip"&gt;StyleCopRule.zip (21.66 kb)&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/BLAIkXOW-dw" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/BLAIkXOW-dw/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/01/15/Xml-Documentation-Comments-Exceptions-II.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=cb1196c0-eae3-4fa1-8926-d8f78085dd05</guid>
      <pubDate>Thu, 15 Jan 2009 16:12:45 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=cb1196c0-eae3-4fa1-8926-d8f78085dd05</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=cb1196c0-eae3-4fa1-8926-d8f78085dd05</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/01/15/Xml-Documentation-Comments-Exceptions-II.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=cb1196c0-eae3-4fa1-8926-d8f78085dd05</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=cb1196c0-eae3-4fa1-8926-d8f78085dd05</feedburner:origLink></item>
    <item>
      <title>Xml Documentation Comments - Exceptions I</title>
      <description>&lt;p&gt;Long time ago my team was investigating about a new beta technology called .NET, we needed to compare it against Java and decide what would be our new development platform (I guess it's clear which one we successfully decided to use). One of the things I liked about Java were &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html#11.2" target="_blank"&gt;Checked Exceptions&lt;/a&gt;, I thought it was handy to&amp;#160; have this enforced by the compiler. Nowadays I'm 100% convinced I wouldn't like to have them in .NET and it seems &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=checked+exceptions+suck&amp;amp;start=0&amp;amp;sa=N" target="_blank"&gt;I'm not the only one&lt;/a&gt;. If you are curious to know why checked exceptions where not added to .NET you can read the &lt;a href="http://www.artima.com/intv/handcuffs.html" target="_blank"&gt;interview done to Anders Hejlsberg&lt;/a&gt; some years after the beta release.&lt;/p&gt;  &lt;p&gt;In any case, it's clear that even if we do not want the compiler forces us to handle exceptions, there is no doubt that the information about the exceptions that can be thrown by a method are very useful for developers. In order to help us with this we can make use of the &lt;a href="http://msdn.microsoft.com/en-us/library/b2s063f7.aspx" target="_blank"&gt;Xml Documentation Comments&lt;/a&gt;, to do it we have the &lt;a href="http://msdn.microsoft.com/en-us/library/w1htk11d.aspx" target="_blank"&gt;exception&lt;/a&gt; tag that allow us introducing the information regarding the exceptions&amp;#160; thrown by our Constructors, Methods, Properties and Indexers, this information will then be listed not only in the documentation we generate with tools like &lt;a href="http://msdn.microsoft.com/en-us/vstudio/bb608422.aspx" target="_blank"&gt;Sandcastle&lt;/a&gt; or &lt;a href="http://ndoc.sourceforge.net/" target="_blank"&gt;NDoc&lt;/a&gt;&amp;#160; but also when we use Intellisense.&lt;/p&gt;  &lt;p&gt;Below you can see an example about how to make use of it.&lt;/p&gt; &lt;style type="text/css"&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="rem"&gt;/// Does something&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="rem"&gt;/// &amp;lt;param name=&amp;quot;value&amp;quot;&amp;gt;A string value&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="rem"&gt;/// &amp;lt;exception cref=&amp;quot;System.ArgumentNullException&amp;quot;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="rem"&gt;/// Thrown when value is null&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&lt;span class="rem"&gt;/// &amp;lt;/exception&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoSomething(&lt;span class="kwrd"&gt;string&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;value&lt;/span&gt; == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; System.ArgumentNullException(&lt;span class="str"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The compiler gives us some help to make sure we write the Xml comments, but unfortunately it is not enabled by default. To do it we need to go to the build properties of our project and configure it to generate the documentation files.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.josefcobonnin.com/image.axd?picture=WindowsLiveWriter/XmlDocumentationCommentsExceptionsI_BDF7/image_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="340" alt="image" src="http://www.josefcobonnin.com/image.axd?picture=WindowsLiveWriter/XmlDocumentationCommentsExceptionsI_BDF7/image_thumb.png" width="426" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;After we configure it the compiler will generate errors (warnings if you do not work with &amp;quot;Treat warnings as errors&amp;quot;, but I like to think nobody has this option disabled in professional software) for the public or visible elements that do not have the Xml comments.&amp;#160; This is a good start point, the compiler validates the Xml comments exist, but does not check the validity of them. i.e. somebody can add the tags and let the values empty. Therefore if we want to check our documentation is properly formatted we need to look for static analysis tools that do this work.&lt;/p&gt;

&lt;p&gt;In the next part of this post we will see how to create a custom rule for StyleCop that checks the exceptions thrown by our code are documented.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/edTjLYa07WM" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/edTjLYa07WM/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2009/01/11/Xml-Documentation-Comments-Exceptions-I.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=141875c4-8940-43dd-b6e6-d02a31808de7</guid>
      <pubDate>Sun, 11 Jan 2009 08:55:52 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=141875c4-8940-43dd-b6e6-d02a31808de7</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=141875c4-8940-43dd-b6e6-d02a31808de7</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2009/01/11/Xml-Documentation-Comments-Exceptions-I.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=141875c4-8940-43dd-b6e6-d02a31808de7</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=141875c4-8940-43dd-b6e6-d02a31808de7</feedburner:origLink></item>
    <item>
      <title>Visual Studio 2010 Extensibility</title>
      <description>&lt;p&gt;If you open Visual Studio 2010 the first thing you probably will notice is that the Start Page has been completely redesigned. The Start Page is now created with WPF, yes you have read it correctly, and it is not the only thing that uses WPF, the code editor is also WPF based.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.josefcobonnin.com/image.axd?picture=WindowsLiveWriter/VisualStudio2010Extensibility_14541/OriginalStartPage_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="342" alt="OriginalStartPage" src="http://www.josefcobonnin.com/image.axd?picture=WindowsLiveWriter/VisualStudio2010Extensibility_14541/OriginalStartPage_thumb.jpg" width="397" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As you can imagine the page can be customized (which by the way was already possible in Visual Studio 2003), to do it you only need to modify the template found in the directory &amp;quot;%ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\StartPages\&amp;lt;culture&amp;gt;&amp;quot;, place your modified version in the folder &amp;quot;%UserProfile%\Documents\Visual Studio 10\StartPages&amp;quot; and that's all, your customized StartPage will be loaded immediately without needing restart Visual Studio.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.josefcobonnin.com/image.axd?picture=WindowsLiveWriter/VisualStudio2010Extensibility_14541/MadAboutNETSection_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="306" alt="MadAboutNETSection" src="http://www.josefcobonnin.com/image.axd?picture=WindowsLiveWriter/VisualStudio2010Extensibility_14541/MadAboutNETSection_thumb.jpg" width="392" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The screenshot above shows an easy customization that I've done. I've just added a new TrayItem on the left side and took profit of the dll Microsoft.VisualStudio.Shell.UI to run some Visual Studio commands to open an IE window, view Visual Studio as full screen and open the Team Explorer window. Below you can see the necessary XAML to do it.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;lt;!-- Customized tray--&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;lt;!-- Mad About .NET group --&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;vs:TrayItem&lt;/span&gt; &lt;span class="attr"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Mad about .NET&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;x:Uid&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Custom_Header&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;vs:TrayGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;vs:TrayGroupItem&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Mad about .NET Blog&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;            &lt;span class="attr"&gt;ImageSource&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;pack://application:,,,/Microsoft.VisualStudio.Shell.UI;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;                          Component/Packages/StartPage/Images/12_settings.png&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            &lt;span class="attr"&gt;Command&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;{x:Static vs:VSCommands.Browse}&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            &lt;span class="attr"&gt;CommandParameter&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;{StaticResource Links.MadAboutNET}&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            &lt;span class="attr"&gt;x:Uid&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;MadaboutNET_Item&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;vs:TrayGroupItem&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Go Full Screen&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            &lt;span class="attr"&gt;ImageSource&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;pack://application:,,,/Microsoft.VisualStudio.Shell.UI;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;                          Component/Packages/StartPage/Images/12_settings.png&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            &lt;span class="attr"&gt;Command&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;{x:Static vs:VSCommands.ExecuteCommand}&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            &lt;span class="attr"&gt;CommandParameter&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;View.FullScreen&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            &lt;span class="attr"&gt;x:Uid&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TeamExplorer_Item&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;vs:TrayGroupItem&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Open Team Explorer&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;            &lt;span class="attr"&gt;ImageSource&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;pack://application:,,,/Microsoft.VisualStudio.Shell.UI;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;                          Component/Packages/StartPage/Images/12_settings.png&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;            &lt;span class="attr"&gt;Command&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;{x:Static vs:VSCommands.ExecuteCommand}&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            &lt;span class="attr"&gt;CommandParameter&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;View.TeamExplorer&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;            &lt;span class="attr"&gt;x:Uid&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TeamExplorer_Item&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;        &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;vs:TrayGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;vs:TrayItem&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Regardless the utility or futility of customizing the StartPage, we need to look further. We must look the new extensibility options that Visual Studio 2010 will bring, incorporating WPF opens a new world of options to extend our favorite development tool. But this is just a small piece of the cake, if you want to know more you should check the &lt;a href="http://www.codeplex.com/MEF"&gt;Managed Extensibility Framework&lt;/a&gt; and don't forget to visit the blog of the &lt;a href="http://blogs.msdn.com/vsxteam/"&gt;VSX Team&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/mC-UxY6yzyM" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/mC-UxY6yzyM/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2008/11/25/Visual-Studio-2010-Extensibility.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=22c1dccf-8a80-4708-a90a-a90ff9210d93</guid>
      <pubDate>Tue, 25 Nov 2008 18:08:29 -0300</pubDate>
      <category>Visual Studio</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=22c1dccf-8a80-4708-a90a-a90ff9210d93</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=22c1dccf-8a80-4708-a90a-a90ff9210d93</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2008/11/25/Visual-Studio-2010-Extensibility.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=22c1dccf-8a80-4708-a90a-a90ff9210d93</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=22c1dccf-8a80-4708-a90a-a90ff9210d93</feedburner:origLink></item>
    <item>
      <title>PDC &amp;amp; TechEd 2008 Hangover</title>
      <description>&lt;p&gt;
These last weeks have been quite frenetic for me. I&amp;#39;ve moved to a new house and before notice I was sleeping in a different house I woke up in a plane going to LA for the PDC, 3 days after I was back from the PDC I went to Amsterdam for the quarterly meetings of my company. Again after 3 days, I was in Barcelona for the TechEd. I guess you imagine how has been coming back to the office.
&lt;/p&gt;
&lt;p&gt;
I must confess I&amp;#39;m not used to travel that much in a short time, I normally do it once a month and is more than enough for me.
&lt;/p&gt;
&lt;p&gt;
The thing is now that I&amp;#39;m back at home and up to date with the work, at least as much up to date as I can be, I start feeling the hangover of the PDC &amp;amp; TechEd. They look already very far away, but I have not been able yet to digest all the cool things I&amp;#39;ve seen at both events. 
&lt;/p&gt;
&lt;p&gt;
I hardly believe I was able to talk in person with people I usually follow like &lt;a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg"&gt;Anders Heljsberg&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/maoni/"&gt;Maoni Stephens&lt;/a&gt; or &lt;a href="http://www.bluebytesoftware.com"&gt;Joe Duffy&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
I really don&amp;#39;t know where to start&amp;nbsp; &lt;a href="http://www.microsoft.com/azure/windowsazure.mspx"&gt;Windows Azure&lt;/a&gt;, &lt;a href="http://www.microsoft.com/windows/windows-7/"&gt;Windows 7&lt;/a&gt;, &lt;a href="http://www.microsoft.com/windowsserver2008/en/us/r2.aspx"&gt;Windows 2008 R2&lt;/a&gt;, &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&amp;amp;displaylang=en"&gt;Visual Studio 2010&lt;/a&gt;, &lt;a href="http://www.microsoft.com/net/oslo.aspx"&gt;Oslo&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/security/aa570351.aspx"&gt;Geneva&lt;/a&gt;, &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&amp;amp;displaylang=en"&gt;.NET Framework 4.0&lt;/a&gt;, &lt;a href="http://code.msdn.microsoft.com/csharpfuture"&gt;C# 4.0&lt;/a&gt; ... 
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;m so excited about most of the things that were announced that I would like&amp;nbsp;to have more hours to check all of them. 
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/vbSCJK_4g0Q" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/vbSCJK_4g0Q/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (Jose)</author>
      <comments>http://www.josefcobonnin.com/post/2008/11/23/PDC-and-TechEd-2008-Hangover.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=edc31976-b286-4037-9b72-ed86c57f7f5e</guid>
      <pubDate>Sun, 23 Nov 2008 06:13:00 -0300</pubDate>
      <category>General</category>
      <dc:publisher>Jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=edc31976-b286-4037-9b72-ed86c57f7f5e</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=edc31976-b286-4037-9b72-ed86c57f7f5e</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2008/11/23/PDC-and-TechEd-2008-Hangover.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=edc31976-b286-4037-9b72-ed86c57f7f5e</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=edc31976-b286-4037-9b72-ed86c57f7f5e</feedburner:origLink></item>
    <item>
      <title>Post Event: Re-descubriendo LINQ</title>
      <description>&lt;p&gt;23rd, October I did a session about LINQ for the .NET User Group&amp;nbsp; &lt;a href="http://www.madriddotnet.com/" target="_blank"&gt;Madriddotnet&lt;/a&gt;.&lt;/p&gt; &lt;div id="__ss_694651" style="width: 425px; text-align: left"&gt;&lt;a title="Re Descubriendo A Linq" style="display: block; margin: 12px 0px 3px; font: 14px helvetica,arial,sans-serif; text-decoration: underline" href="http://www.slideshare.net/jfbonnin/re-descubriendo-a-linq-presentation?type=powerpoint"&gt;Re Descubriendo A Linq&lt;/a&gt;&lt;embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc=redescubriendoalinq-1225031698958107-8&amp;amp;stripped_title=re-descubriendo-a-linq-presentation" width="425" height="355" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always"&gt;&lt;/embed&gt; &lt;div style="font-size: 11px; padding-top: 2px; font-family: tahoma,arial; height: 26px"&gt;View SlideShare &lt;a title="View Re Descubriendo A Linq on SlideShare" style="text-decoration: underline" href="http://www.slideshare.net/jfbonnin/re-descubriendo-a-linq-presentation?type=powerpoint"&gt;presentation&lt;/a&gt; or &lt;a style="text-decoration: underline" href="http://www.slideshare.net/upload?type=powerpoint"&gt;Upload&lt;/a&gt; your own. (tags: &lt;a style="text-decoration: underline" href="http://slideshare.net/tag/linq"&gt;linq&lt;/a&gt;)&lt;/div&gt;&lt;/div&gt; &lt;p&gt;During the event we tried to know more in deep how LINQ works, from &lt;a href="http://research.microsoft.com/Comega/" target="_blank"&gt;C-Omega&lt;/a&gt; to LINQ to SQL, doing an attempt to solve many of the questions that normally arise when you want to obtain the most of LINQ. &lt;/p&gt; &lt;p&gt;We found Visual Studio 2008 comes with a nice bunch of samples to helps us including visualizers like the &lt;a href="http://msdn.microsoft.com/en-us/library/bb397975.aspx" target="_blank"&gt;Expression Tree Visualizer&lt;/a&gt;, which allows you to see the expression tree created from our queries and the &lt;a href="http://msdn.microsoft.com/en-us/library/bb629285.aspx" target="_blank"&gt;LINQ Query Visualizer&lt;/a&gt; to see the translation done to Transact/SQL. We knew that it is possible to create queries on the fly using the &lt;a href="http://msdn.microsoft.com/en-us/library/bb397982.aspx" target="_blank"&gt;Dynamic Query Library&lt;/a&gt;&lt;/p&gt; &lt;p&gt;We also talked about the benefits of the expression trees and the visitor pattern. You have some sample implementations like the one done by &lt;a href="http://blogs.msdn.com/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx" target="_blank"&gt;Matt Warren&lt;/a&gt;&lt;/p&gt; &lt;p&gt;In the last part of the event we focused on tips for LINQ to SQL to move to a multi-tier architecture. We explained how easy is to serialize LINQ entities to be used over WCF on both directions, this is from Master to Detail and from Detail to Master. How to workaround the fact that circular references are not supported by default by the DataContractSerializer. We gave some ideas about how to do local change tracking of entities when we are disconnected from the DataContext, providing some examples of complete implementations like the one done at &lt;a href="http://www.desarrollaconmsdn.com/msdn/msdnvideo2008.aspx" target="_blank"&gt;MSDN Video&lt;/a&gt; (Spanish).&lt;/p&gt; &lt;p&gt;As you can see we talked about many different things and we verified how LINQ can help us during our day to day work making our life much easier if we apply some good practices.&lt;/p&gt; &lt;p&gt;If you attended to the event I hope you enjoyed as much as I did.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/Lud9-GWwSVM" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/Lud9-GWwSVM/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2008/10/26/Post-Event-Re-descubriendo-LINQ.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=3d0e42af-2626-48a3-ae4b-00820eb808ec</guid>
      <pubDate>Sun, 26 Oct 2008 11:25:41 -0300</pubDate>
      <category>LINQ</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=3d0e42af-2626-48a3-ae4b-00820eb808ec</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=3d0e42af-2626-48a3-ae4b-00820eb808ec</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2008/10/26/Post-Event-Re-descubriendo-LINQ.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=3d0e42af-2626-48a3-ae4b-00820eb808ec</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=3d0e42af-2626-48a3-ae4b-00820eb808ec</feedburner:origLink></item>
    <item>
      <title>Post Event: Descubriendo el CLR</title>
      <description>&lt;p&gt;On Tuesday I did an online webcast about the CLR. I hope the people who joined us enjoyed the event and found the contents interesting. &lt;/p&gt; &lt;p&gt;The event was recorded and you can download it from the &lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032389673&amp;amp;Culture=es-ES" target="_blank"&gt;Microsoft Events website&lt;/a&gt; (Spanish).&lt;/p&gt; &lt;p&gt;During the presentation I did a demo in which I was showing how we can see with WinDBG when a method has been jitted or not. We saw it for the constructor but I stopped before show it for other methods to avoid wasting too much time on the same thing. &lt;/p&gt; &lt;p&gt;One of the attendees has asked me today why the method was shown as not jitted even after it was clearly executed. The problem was simply that I attached WinDGB to the release version of my demo application and the JIT optimized the method replacing it by an inlined version. If I had attached the debug version this would not happen and the method would be shown as jitted. If you want to have more information about it I widely explained in a previous post about &lt;a href="http://www.josefcobonnin.com/post/2008/08/03/Inline-Methods.aspx" target="_blank"&gt;Inline methods&lt;/a&gt; where I used the same demo application as in the webcast.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/DDieVeCMRxU" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/DDieVeCMRxU/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (Jose)</author>
      <comments>http://www.josefcobonnin.com/post/2008/10/09/Post-Event-Descubriendo-el-CLR.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=7b0931b1-c0cd-4ade-9963-c987510600a2</guid>
      <pubDate>Thu, 09 Oct 2008 14:58:26 -0300</pubDate>
      <category>General</category>
      <dc:publisher>Jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=7b0931b1-c0cd-4ade-9963-c987510600a2</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=7b0931b1-c0cd-4ade-9963-c987510600a2</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2008/10/09/Post-Event-Descubriendo-el-CLR.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=7b0931b1-c0cd-4ade-9963-c987510600a2</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=7b0931b1-c0cd-4ade-9963-c987510600a2</feedburner:origLink></item>
    <item>
      <title>Culminis Changes</title>
      <description>&lt;p&gt;Around a week ago &lt;a href="http://www.culminis.com" target="_blank"&gt;Culminis&lt;/a&gt; announced the next phase of the Culminis transformation has started. &lt;/p&gt; &lt;p&gt;As you probably know Culminis has become a volunteer organization similar to &lt;a href="http://www.ineta.org" target="_blank"&gt;Ineta&lt;/a&gt;. This change started long time ago, something that &lt;a href="http://blogs.technet.com/grahamtwatson" target="_blank"&gt;Graham Watson&lt;/a&gt; explained with a post describing the key changes about how Microsoft would support the User Groups from now.&lt;/p&gt; &lt;p&gt;I must confess my first reaction was not very positive, I loved how Culminis was working until that day and I was afraid how the new volunteer structure would affect all the services provided. &lt;/p&gt; &lt;p&gt;In any case, I'm sure all the people we are volunteering at Culminis will do our best to satisfy the demands of the user groups. So, now it's time to give our maximum support to the board of directors. If you want to know more about them you can check it &lt;a href="http://www.culminis.com/BOD" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/Z_duN4GZ3D4" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/Z_duN4GZ3D4/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (Jose)</author>
      <comments>http://www.josefcobonnin.com/post/2008/10/06/Culminis-Changes.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=da2f4a68-6c69-4e30-9608-e9690d050119</guid>
      <pubDate>Mon, 06 Oct 2008 13:49:48 -0300</pubDate>
      <category>Culminis</category>
      <dc:publisher>Jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=da2f4a68-6c69-4e30-9608-e9690d050119</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=da2f4a68-6c69-4e30-9608-e9690d050119</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2008/10/06/Culminis-Changes.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=da2f4a68-6c69-4e30-9608-e9690d050119</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=da2f4a68-6c69-4e30-9608-e9690d050119</feedburner:origLink></item>
    <item>
      <title>Static Fields</title>
      <description>&lt;p&gt;&lt;em&gt;&amp;quot;Types may declare locations that are associated with the type rather than any particular value of the type. Such locations are static fields of the type. As such, static fields declare a location that is shared by all values of the type. Just like non-static (instance) fields, a static field is typed and that type never changes. Static fields are always restricted to a single application domain basis, but they may also be allocated on a per-thread basis.&amp;quot;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The paragraph above is the definition of static field extracted from the &lt;a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm" target="_blank"&gt;CLI specification&lt;/a&gt;. The text means that when we create a static field &amp;quot;f&amp;quot; inside a class &amp;quot;C&amp;quot;, the value will not belong to any of the instances we create of C, instead its value is shared across all the instances and therefore &amp;quot;belongs&amp;quot; to the type C itself.&lt;/p&gt;  &lt;p&gt;I've created a very simple class with one static and two instance fields, which you can see below.&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 700px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; BusinessLogic&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; instanceField1;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; instanceField2;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; staticField3 = &lt;span style="color: #006080"&gt;3&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; BusinessLogic()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;         instanceField1 = &lt;span style="color: #006080"&gt;1&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt;         instanceField2 = &lt;span style="color: #006080"&gt;2&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I've run the code and I've created 5 instances of the class above, which we will check with WinDBG. To do it we can execute the next command:&lt;/p&gt;

&lt;p&gt;!DumpHeap -type BusinessLogic&lt;/p&gt;

&lt;p&gt;That will show something similar to the next:&lt;/p&gt;

&lt;p&gt; Address&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MT&amp;#160;&amp;#160;&amp;#160;&amp;#160; Size
  &lt;br /&gt;&lt;strong&gt;01dfa98c&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/strong&gt;006668c4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 16&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;strong&gt;01dfe898&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 006668c4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 16&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;strong&gt;01e027a4&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 006668c4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 16&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;strong&gt;01e0668c&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 006668c4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 16&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;strong&gt;01e13104&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 006668c4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 16&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;total 5 objects

  &lt;br /&gt;Statistics:

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MT&amp;#160;&amp;#160;&amp;#160; Count&amp;#160;&amp;#160;&amp;#160; TotalSize&amp;#160;&amp;#160; Class Name

  &lt;br /&gt;006668c4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 5&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 80&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BusinessLogic

  &lt;br /&gt;Total 5 objects&lt;/p&gt;

&lt;p&gt;Once we have the address of the 5 instances we created we can dump the objects one by one by using the command !DumpObj [Address]. If we take the first one it will display something similar to:&lt;/p&gt;

&lt;p&gt;Name: BusinessLogic
  &lt;br /&gt;MethodTable: 001c68bc

  &lt;br /&gt;&lt;strong&gt;EEClass: 00281abc&lt;/strong&gt;

  &lt;br /&gt;Size: 16(0x10) bytes

  &lt;br /&gt;Fields:

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MT&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Field&amp;#160;&amp;#160; Offset&amp;#160;&amp;#160; Type&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; VT&amp;#160;&amp;#160; Attr&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Value&amp;#160;&amp;#160;&amp;#160; Name

  &lt;br /&gt;6f642b38&amp;#160; 4000001&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Int32&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160; instance&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; instanceField1

  &lt;br /&gt;6f642b38&amp;#160; 4000002&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 8&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Int32&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160; instance&amp;#160; 2&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; instanceField2

  &lt;br /&gt;6f642b38&amp;#160; 4000003&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1c&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Int32&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160; static&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; staticField3&lt;/p&gt;

&lt;p&gt;What we have done with this command is to examine the fields of one of the instances in memory of BusinessLogic. We can see that the object has the fields instanceField1, instanceField2 and staticField3 and its values are 1,2 and 3 respectively. So, all is as expected. &lt;/p&gt;

&lt;p&gt;Lets focus now on the cool thing of static fields. We can see how &amp;quot;staticField3&amp;quot; is shared across al the instances we create of BusinessLogic by examing the EEClass of the objects. The 5 instances we created before have the same EEClass: &amp;quot;00281abc&amp;quot;. If we examine it with the command !DumpClass 00281abc we will see that &amp;quot;staticField3&amp;quot; is present at EEClass level and its value is already initialized.&lt;/p&gt;

&lt;p&gt;Class Name: BusinessLogic
  &lt;br /&gt;mdToken: 02000002

  &lt;br /&gt;Parent Class: 6f3d3ef0

  &lt;br /&gt;Module: 001c64f0

  &lt;br /&gt;Method Table: 001c68bc

  &lt;br /&gt;Vtable Slots: 4

  &lt;br /&gt;Total Method Slots: 6

  &lt;br /&gt;Class Attributes: 100001&amp;#160; &lt;br /&gt;NumInstanceFields: 2

  &lt;br /&gt;NumStaticFields: 1

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MT&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Field&amp;#160; Offset&amp;#160;&amp;#160; Type&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; VT&amp;#160;&amp;#160;&amp;#160; Attr&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Value&amp;#160;&amp;#160; Name

  &lt;br /&gt;6f642b38&amp;#160;&amp;#160;&amp;#160; 4000001&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 4&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Int32&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160; instance&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; instanceField1

  &lt;br /&gt;6f642b38&amp;#160;&amp;#160;&amp;#160; 4000002&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 8&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Int32&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160; instance&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; instanceField2

  &lt;br /&gt;&lt;strong&gt;6f642b38&amp;#160; 4000003&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1c&amp;#160;&amp;#160;&amp;#160; System.Int32&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160; static&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3&amp;#160;&amp;#160;&amp;#160;&amp;#160; staticField3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At this point we have demonstrated how the static fields and their values are shared across all the instances we create of a class.&lt;/p&gt;

&lt;p&gt;In this sample we have used value types for the static field, but this also applies for reference types. This is very cool, because it allow us creating class designs where objects with a heavy load construction can be instantiated just once&amp;#160; i.e.&lt;/p&gt;

&lt;p&gt;static object staticField;
  &lt;br /&gt;....

  &lt;br /&gt;if (staticField == null)

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; staticField = new object();&lt;/p&gt;

&lt;p&gt;This simple code will allow sharing the same instance of staticField across the entire application domain helping with the performance if staticField is hard to construct.&lt;/p&gt;

&lt;p&gt;This does not mean that from now you must create all your &amp;quot;hard to construct&amp;quot; fields as static, because like always the gold hammer does not exist and static fields have a downside.&lt;/p&gt;

&lt;p&gt;As we have seen the static field values are related to the EEClass, which are allocated on the loader heaps that are AppDomain specific, so this means they will be in memory until the AppDomain is unloaded.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/josefcobonnin/~4/W1KWcKh-CCU" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/josefcobonnin/~3/W1KWcKh-CCU/post.aspx</link>
      <author>jfbonnin.nospam@nospam.baleareson.net (jose)</author>
      <comments>http://www.josefcobonnin.com/post/2008/09/20/Static-Fields.aspx#comment</comments>
      <guid isPermaLink="false">http://www.josefcobonnin.com/post.aspx?id=3632856a-986b-4175-958d-26abdc08386d</guid>
      <pubDate>Sat, 20 Sep 2008 07:52:32 -0300</pubDate>
      <category>.NET Framework</category>
      <dc:publisher>jose</dc:publisher>
      <pingback:server>http://www.josefcobonnin.com/pingback.axd</pingback:server>
      <pingback:target>http://www.josefcobonnin.com/post.aspx?id=3632856a-986b-4175-958d-26abdc08386d</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.josefcobonnin.com/trackback.axd?id=3632856a-986b-4175-958d-26abdc08386d</trackback:ping>
      <wfw:comment>http://www.josefcobonnin.com/post/2008/09/20/Static-Fields.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.josefcobonnin.com/syndication.axd?post=3632856a-986b-4175-958d-26abdc08386d</wfw:commentRss>
    <feedburner:origLink>http://www.josefcobonnin.com/post.aspx?id=3632856a-986b-4175-958d-26abdc08386d</feedburner:origLink></item>
  </channel>
</rss>
