<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Peter Hallam's WebLog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/peterhal/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/peterhal/feed.xml</link>
    <description>C# Compiler</description>
    <lastBuildDate>Wed, 17 Apr 2019 04:03:27 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Peterhal - Last day at Microsoft</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/peterhal-last-day-at-microsoft</link>
      <pubDate>Fri, 20 Jul 2007 19:18:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2007/07/20/peterhal-last-day-at-microsoft/</guid>
      <description><![CDATA[I've been promising myself an extended vacation for a long time now, but working at Microsoft has...]]></description>
      <content:encoded><![CDATA[<P>I've been promising myself an extended vacation for a long time now, but working at Microsoft has been so much fun I've kept postponing it. Well, this is it - my last day at Microsoft.</P>
<P mce_keep="true">&nbsp;</P>
<P>Feel free to&nbsp;email me at 'hotweird' at hotmail.</P>]]></content:encoded>
    </item>
    <item>
      <title>What Makes a Good Programmer?</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/what-makes-a-good-programmer</link>
      <pubDate>Sun, 04 Feb 2007 19:24:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2007/02/04/what-makes-a-good-programmer/</guid>
      <description><![CDATA[I just read two salon articles about Scott Rosenberg's new book Dreaming in Code. His thesis is that...]]></description>
      <content:encoded><![CDATA[<P>I just read two salon articles about Scott Rosenberg's new book Dreaming in Code. His thesis is that "<A class="" title="programming is hard" href="http://www.salon.com/books/int/2007/02/03/leonard/?source=rss" mce_href="http://www.salon.com/books/int/2007/02/03/leonard/?source=rss">programing is hard</A>" and uses the experiences of the development of the Chandler project to frame the discussion. Scott identifies&nbsp;a common problem in large software development - <A class="" title="inconsistent terminology" href="http://www.salon.com/books/feature/2007/02/03/rosenberg/index.html" mce_href="http://www.salon.com/books/feature/2007/02/03/rosenberg/index.html">inconsistent terminology</A> use across the software development team. At the same time he complements the members of the development team as being talented programmers. These two ideas struck me as incongruous.&nbsp;How can a bunch of "ace programmers" end up with such a basic problem? Should programmers encountering this problem really be deserving of the accolade "ace"? And what skills would I expect from an "ace" programmer anyhow?</P>
<P>Before I go any further, I want to make it clear that I don't want to talk down Scottt or the Chandler team. Scott makes a number of great points that reflect my own experiences. Virtually every large software project I've seen has had problems akin to those Scott describes. Especially every large project I've worked on. I'm certainly as guilty of these difficulties as any programmer. But the examples that Scott describes are good grist for discussion about software development.</P>
<P>When people talk about talented programmers they are usually refering to their ability to write correct code, whch solves problems quickly and elegantly. I suspect that that is what Scott was implying by describing the Chandler team as ace programmers. Raw coding skills vary widely across individuals. When it comes down to it some programmers are just more talented than others at writing code. Large team projects - projects with more than 3 programers with schedules of more than a few months - require a whole set of skills beyond raw coding. On large teams, excellent coding skills are a prerequisite. Alone, they qualify you as a beginner, not an ace.</P>
<P><STRONG>From Beginner to Good</STRONG></P>
<P>For me, the mark of a good team programmer is someone who does not suggest rewriting everything from scratch. Scott makes the very valid point that programmers like to program - and that they don't like to understand code written by others. Truer words were never spoken.&nbsp;Time and time again I've seen smart novice programmers suggest rewriting&nbsp;code produced by other programmers.&nbsp;I've noticed an inverse relationship betwen the esteem I hold for a programmer and the number of times I've heard them suggest a rewrite of a large software project.</P>
<P>Good team programers are capable and willing&nbsp;to work with a codebase that is too large for a single person to rewrite within the project's schedule. They recognize that the existing codebase has shortcomings and they have the maturity and discipline to know that attempting to 'fix' everyone of those shortcomings will almost certainly kill the project. Instead they identify those areas which must be improved on to deliver the project, and&nbsp;they have the skills to make significant changes to a large and ugly (because every large piece of software is ugly - but that's another story) codebase without causing the whole thing to destabilize.</P>
<P><STRONG>And From Good To Great</STRONG></P>
<P>If the mark of a good team programmer is the ability to work (usually grudgingly)&nbsp;with the code of others, then the mark of a great team programer is the ability to produce code that other programmers on the team will gladly use. Programmers are the most fickle sort, and if you can produce code that other, less skilled, coders will use without suggesting a rewrite - then you've elevated yourself to the top of the programing heap. Great programmers produce code that is so good that it will prevent the cross programer problems that plague most large software projects.</P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>Code Review: Double Checked Locking Code</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/code-review-double-checked-locking-code</link>
      <pubDate>Tue, 12 Dec 2006 13:16:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2006/12/12/code-review-double-checked-locking-code/</guid>
      <description><![CDATA[A friend of mine recently sent me some code to review:
&nbsp;
Hi Peter,
Do you...]]></description>
      <content:encoded><![CDATA[<META content=Word.Document name=ProgId>
<META content="Microsoft Word 11" name=Generator>
<META content="Microsoft Word 11" name=Originator><LINK href="DoubleCheckedLock_files/filelist.xml" rel=File-List mce_href="DoubleCheckedLock_files/filelist.xml">
<STYLE>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;
	mso-font-charset:2;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:0 268435456 0 0 -2147483648 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0in;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
span.SpellE
	{mso-style-name:"";
	mso-spl-e:yes;}
span.GramE
	{mso-style-name:"";
	mso-gram-e:yes;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.25in 1.0in 1.25in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
 /* List Definitions */
 @list l0
	{mso-list-id:192961706;
	mso-list-type:hybrid;
	mso-list-template-ids:978583592 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:.5in;
	mso-level-number-position:left;
	text-indent:-.25in;
	font-family:Symbol;}
ol
	{margin-bottom:0in;}
ul
	{margin-bottom:0in;}
-->
</STYLE>

<DIV class=Section1>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-no-proof: yes">A friend of mine recently sent me some code to review:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Hi Peter,<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Do you have any suggestions on how to clean up this code?<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">In particular the creation of a particular singleton type doesn't look very pretty in this model:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">private</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> DoubleCheckedLock&lt;SingletonClass&gt; myInstance = <SPAN style="COLOR: blue">new</SPAN> DoubleCheckedLock&lt;SingletonClass&gt;();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> SingletonClass Current {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">get</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">this</SPAN>.myInstance.GetSingleton(<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">delegate</SPAN>() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">this</SPAN>.CreateSingleton(OrderContext, <SPAN style="COLOR: #a31515">"third"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">I’m sharing my comments on the code, as it demonstrates some important design principles. Here’s the original code:<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">class</SPAN> DoubleCheckedLock&lt;T&gt; where T : <SPAN style="COLOR: blue">class<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">volatile</SPAN> T myInstance;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">readonly</SPAN> <SPAN style="COLOR: blue">object</SPAN> lockObject = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">object</SPAN>();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> T GetSingleton(CreateObject c) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (<SPAN style="COLOR: blue">this</SPAN>.myInstance == <SPAN style="COLOR: blue">null</SPAN>) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">lock</SPAN> (lockObject) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (<SPAN style="COLOR: blue">this</SPAN>.myInstance == <SPAN style="COLOR: blue">null</SPAN>) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.myInstance = c();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Debug.Assert(<SPAN style="COLOR: blue">this</SPAN>.myInstance != <SPAN style="COLOR: blue">null</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">this</SPAN>.myInstance;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">delegate</SPAN> T CreateObject();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">abstract</SPAN> <SPAN style="COLOR: blue">class</SPAN> DoubleCheckedLock2&lt;T&gt; where T : <SPAN style="COLOR: blue">class<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">protected</SPAN> <SPAN style="COLOR: blue">abstract</SPAN> T CreateObject();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">private</SPAN> DoubleCheckedLock&lt;T&gt; myInstance = <SPAN style="COLOR: blue">new</SPAN> DoubleCheckedLock&lt;T&gt;();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> T Instance {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">get</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">this</SPAN>.myInstance.GetSingleton(<SPAN style="COLOR: blue">this</SPAN>.CreateObject);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal><o:p>&nbsp;</o:p></P>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">And here are my comments:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<UL style="MARGIN-TOP: 0in" type=disc>
<LI class=MsoNormal style="mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">The name <SPAN class=SpellE>DoubleCheckedLock</SPAN> describes the implementation. Names should reflect how the client will use a symbol, not how something is implemented. For clients, the class implements a lazily created, thread safe singleton object. Aka, a Singleton <SPAN class=GramE>object</SPAN> that does the right thing. Naming the class 'Singleton' will make client code read much better.<o:p></o:p></SPAN></LI></UL>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<UL style="MARGIN-TOP: 0in" type=disc>
<LI class=MsoNormal style="mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">I'm not a fan of public nested types, so I'd recommend moving the <SPAN class=SpellE>CreateObject</SPAN> delegate to a top level type. That's a style choice, but I've found it to wear well in practice.<o:p></o:p></SPAN></LI></UL>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<UL style="MARGIN-TOP: 0in" type=disc>
<LI class=MsoNormal style="mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Names of types, including delegate types, should be nouns, not verbs or verb phrases. <SPAN class=SpellE>CreateObject</SPAN> should be renamed Creator.<o:p></o:p></SPAN></LI></UL>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<UL style="MARGIN-TOP: 0in" type=disc>
<LI class=MsoNormal style="mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Passing in the creator delegate is necessary for this pattern so don't sweat it. However, passing in the delegate to the '<SPAN class=SpellE>GetSingleton</SPAN>' method allows for some surprising behavior. It allows different calls to '<SPAN class=SpellE>GetSingleton</SPAN>' to supply different values which will surely cause confusion. This confusion can be designed out by passing in the creator delegate as an argument to the Singleton constructor.<o:p></o:p></SPAN></LI></UL>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<UL style="MARGIN-TOP: 0in" type=disc>
<LI class=MsoNormal style="mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Once the <SPAN class=SpellE>GetSingleton</SPAN> parameter is removed, it can become a property named 'Value'. Again, this makes your client code read better.<o:p></o:p></SPAN></LI></UL>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<UL style="MARGIN-TOP: 0in" type=disc>
<LI class=MsoNormal style="mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Add the 'sealed' and '<SPAN class=SpellE>readonly</SPAN>' modifiers unless you are specifically expecting to use a class as a base or modify a field after construction. The additional modifiers self document the code, and make you think through your design more fully.<o:p></o:p></SPAN></LI></UL>
<P class=MsoNormal style="mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<UL style="MARGIN-TOP: 0in" type=disc>
<LI class=MsoNormal style="mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">The DoubleCheckedLock2 abstract base class is a classic example of inheriting for containment - representing a 'has a' relationship between the derived class and the DoubleCheckedLock2 base class. Inheritance should always represent an 'is a' relationship, never a 'has a' relationship. You will save yourself many headaches by avoiding base classes which force you to use your one precious base class to represent a 'has a' relationship. Get rid of the DoubleCheckedLock2 class. The fact that it has such a crummy name, and that there is no good name to give this thing is a clear indication that something is wrong with it.<o:p></o:p></SPAN></LI></UL>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Now the client code looks like this:<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">sealed</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">class</SPAN> Client {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">readonly</SPAN> Singleton&lt;ExpensiveResource&gt; expensiveValue;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>Client() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.expensiveValue = <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">new</SPAN> Singleton&lt;ExpensiveResource&gt;(<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: blue">delegate</SPAN>() <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">new</SPAN> ExpensiveResource();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> ExpensiveResource ExpensiveValue { <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">get</SPAN> { <SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">this</SPAN>.expensiveValue.Value; } <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Which is pretty damn sexy, if I don't say so myself.<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Here's the implementation:<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">delegate</SPAN> T Creator&lt;T&gt;();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">class</SPAN> Singleton&lt;T&gt; <SPAN style="COLOR: blue">where</SPAN> T : <SPAN style="COLOR: blue">class</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">readonly</SPAN> <SPAN style="COLOR: blue">object</SPAN> lockObject = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">object</SPAN>();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">volatile</SPAN> T myInstance;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">readonly</SPAN> Creator&lt;T&gt; creator;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> Singleton(Creator&lt;T&gt; creator) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Debug.Assert(creator != <SPAN style="COLOR: blue">null</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.creator = creator;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> T Value {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">get</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (<SPAN style="COLOR: blue">this</SPAN>.myInstance == <SPAN style="COLOR: blue">null</SPAN>) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">lock</SPAN> (lockObject) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (<SPAN style="COLOR: blue">this</SPAN>.myInstance == <SPAN style="COLOR: blue">null</SPAN>) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.myInstance = <SPAN style="COLOR: blue">this</SPAN>.creator();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.creator = <SPAN style="COLOR: blue">null</SPAN>;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: green">// allow creator object to be GC-ed as soon as possible<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Debug.Assert(<SPAN style="COLOR: blue">this</SPAN>.myInstance != <SPAN style="COLOR: blue">null</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">this</SPAN>.myInstance;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0.5in; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Happy coding,<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Peter<o:p></o:p></SPAN></P></DIV>]]></content:encoded>
    </item>
    <item>
      <title>New F# Release.</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/new-f-release</link>
      <pubDate>Mon, 04 Dec 2006 02:31:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2006/12/04/new-f-release/</guid>
      <description><![CDATA[Don Syme and the F# team have just released a new version of F#. Check it out here:...]]></description>
      <content:encoded><![CDATA[<P>Don Syme and the F# team have just released a new version of F#. Check it out here: <A href="http://blogs.msdn.com/dsyme/archive/2006/11/30/f-1-1-13-now-available.aspx">http://blogs.msdn.com/dsyme/archive/2006/11/30/f-1-1-13-now-available.aspx</A></P>
<P mce_keep="true">&nbsp;</P>
<P>Peter</P>]]></content:encoded>
    </item>
    <item>
      <title>What Do Programmers Really Do Anyway? The data is in!</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/what-do-programmers-really-do-anyway-the-data-is-in</link>
      <pubDate>Mon, 27 Nov 2006 16:59:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2006/11/27/what-do-programmers-really-do-anyway-the-data-is-in/</guid>
      <description><![CDATA[The smart folks over in the MSR-Human Interactions&nbsp;in Programming&nbsp;team have done some...]]></description>
      <content:encoded><![CDATA[<P>The smart folks over in the MSR-Human Interactions&nbsp;in Programming&nbsp;team have done some interesting research into the question of "What Do Programmers Really Do Anyway?". I'm more than a little pleased to see that the data is consistent with the assertions I made in my previous blog posts.</P>
<P><FONT size=2>Some of their results are here:</FONT></P>
<P><FONT size=2><A href="http://research.microsoft.com/research/pubs/view.aspx?type=Technical%20Report&amp;id=994" mce_href="http://research.microsoft.com/research/pubs/view.aspx?type=Technical%20Report&amp;id=994">http://research.microsoft.com/research/pubs/view.aspx?type=Technical%20Report&amp;id=994</A></P>
<P></FONT><A href="http://research.microsoft.com/hip/papers/Ko2007BugFixing.pdf" mce_href="http://research.microsoft.com/hip/papers/Ko2007BugFixing.pdf"><U><FONT color=#0000ff size=2>http://research.microsoft.com/hip/papers/Ko2007BugFixing.pdf</U></FONT></A></P><FONT size=2>
<P>Their team's web site is:</P>
<P></FONT><A href="http://research.microsoft.com/hip/" mce_href="http://research.microsoft.com/hip/"><U><FONT color=#0000ff size=2>http://research.microsoft.com/hip/</U></FONT></A></P>
<P><FONT size=2>I'm looking forward to more great work from these folks,</FONT></P>
<P>Peter</P>]]></content:encoded>
    </item>
    <item>
      <title>C# Automatically Implemented Properties - My Video Debut</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/c-automatically-implemented-properties-my-video-debut</link>
      <pubDate>Mon, 27 Nov 2006 16:55:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2006/11/27/c-automatically-implemented-properties-my-video-debut/</guid>
      <description><![CDATA[Hey Folks,
Charlie Calvert video taped&nbsp;our discussion on some of the new C# 3.0 features. Find...]]></description>
      <content:encoded><![CDATA[<P>Hey Folks,</P>
<P><A class="" href="http://blogs.msdn.com/charlie/" mce_href="http://blogs.msdn.com/charlie/">Charlie Calvert</A> video taped&nbsp;our discussion on some of the new C# 3.0 features. Find it here: <A href="http://wm.microsoft.com/ms/msdn/visualcsharp/peter_hallam_2006_11/PeterHallam01.wmv" mce_href="http://wm.microsoft.com/ms/msdn/visualcsharp/peter_hallam_2006_11/PeterHallam01.wmv">http://wm.microsoft.com/ms/msdn/visualcsharp/peter_hallam_2006_11/PeterHallam01.wmv</A>.</P>
<P>Cheers,</P>
<P>Peter</P>]]></content:encoded>
    </item>
    <item>
      <title>What Do Programmers Really Do Anyway? (aka Part 2 of the Yardstick saga)</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/what-do-programmers-really-do-anyway-aka-part-2-of-the-yardstick-saga</link>
      <pubDate>Wed, 04 Jan 2006 13:36:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2006/01/04/what-do-programmers-really-do-anyway-aka-part-2-of-the-yardstick-saga/</guid>
      <description><![CDATA[Way back in 2002 when we started working on Whidbey, I captured my thoughts on the direction we...]]></description>
      <content:encoded><![CDATA[<DIV><FONT face=Arial size=2>Way back in 2002 when we started working on Whidbey, I captured my thoughts on the direction we should take for C# and Visual Studio in two large emails. In the first email </FONT><a href="http://blogs.msdn.com/peterhal/archive/2004/08/11/213120.aspx"><FONT face=Arial size=2>The Yardstick</FONT></A><FONT face=Arial><FONT size=2>&nbsp;<SPAN class=740242718-02082002>I spent a lot of time saying that you must evaluate features against the amount of time they save versus the amount of time they cost. A big saving in an infrequent task is not worth even a small cost in a frequent task, and more importantly, even a small saving in a frequent task will have a much more significant effect than a huge saving in an infrequent task. </SPAN></FONT></FONT></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>This follow up email&nbsp;captures my thoughts on "Where we as tool developers should focus to provide the most benefit for the programmers that use our tools?" To answer this </FONT></SPAN><SPAN class=740242718-02082002><FONT face=Arial size=2>question we must first answer the question:</FONT></SPAN></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>What do professional programmers really do with their time anyway?</STRONG></FONT></SPAN></DIV></BLOCKQUOTE>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Here is the result of my observation way back in August of 2002, and after reviewing it, I find that it still rings true today. I'd be delighted to hear how your experiences as programmers stack up...</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>So after taking out the time spent playing Halo and consuming mass quantities of caffeine, here's what I do all day:</FONT></SPAN></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Design Code</FONT></SPAN> </DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Write New Code</FONT></SPAN> </DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Understand Existing Code</FONT></SPAN> </DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Modify Existing Code</FONT></SPAN> </DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Verify Existing Code Still Works</FONT></SPAN></DIV></BLOCKQUOTE>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Let me expand on each of these tasks, to explain precisely what I mean.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Design Code</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Design&nbsp;involves analyzing a new problem, and mapping out the broad flow of code which will be used to solve the problem. To date the #1 tool for this activity is a&nbsp;big whiteboard (note the 8' x 4' monster on my North wall) and 1 to 3 engaged brains focused on the problem. There are some tools which attempt to formalize the design process using visual designers to view and edit the relationships in your code. My whiteboard is pretty effective at this task however, and I have yet to really want to even investigate a formal tool for an informal process of brainstorming and design.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Write New Code</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>For me, writing new code means typing in code in a virgin source file and getting it to compilable state. I'll also include writing a new method from scratch in this category. I do not include adding code to an existing method, or copying code from an existing method and modifying the copy to do something new. I'll leave those&nbsp;activities in the modifying existing code task. Intellisense is THE power tool for writing new code.It has easily increased the speed at which a programmer can write new code by a factor of 2x to 5x. Visual designers (like the Winforms and Data designers) and code wizards are useful for getting some common coding patterns started.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Understanding Existing Code</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Understanding existing code means taking a look at some code to understand precisely what is going on. Answering questions like: Exactly what are the inputs to this method? What is the output of this piece of code? Exactly what does the callstack and data look like&nbsp;when this&nbsp;particular code path get hit? Why is this code doing what it should? Why does this code not do what I want it to? Note that this includes understanding&nbsp;code that I have source for (usually written by me, or someone in my organization) but it also includes understanding the details of libraries (like the BCL and WinForms) that I don't have source for. Lastly this also includes understanding the overall structure of an existing library or application. Answering questions like: What are the main data structures and how do they interact?</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>The primary tool used in this activity is the editor - think 'lots of time staring at source code'. Code outlining (collapse to definitions) is a great feature in this area. A lot of time is spent tracing up and down call stacks, trying to understand the flow of the bits. In theory class view and symbolic navigation (goto definition and goto reference) would be very useful, but I still find myself using find in files as my primary navigation tool for source code. </FONT></SPAN><SPAN class=740242718-02082002><FONT face=Arial size=2>The Object Browser, ildasm&nbsp;and Help are the primary tools for understanding libraries of managed code. Looking at the code spit by Visual Designers is a great way to understand how the underlying code libraries work. </FONT></SPAN><SPAN class=740242718-02082002><FONT face=Arial size=2>Perhaps the most useful tool is the debugger. The 'find the bug' part of debugging fits entirely in the understanding code activity. Symbolic debuggers aid greatly in understanding of code flow, but perhaps more importantly the debugger shows the shape of the real data structures that your code is operating on. Still, most of understanding code happens without the debugger, and relies on the editor.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Modifying Existing Code</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Modifying code is related to writing new code, but it is different enough to call out separately. Modifying existing code is either bug fixing or adding new features. When fixing a (simple) bug in a method there is some existing behavior in the code which must be preserved. The coder must ensure that the fix doesn't break any of the existing non-buggy behavior. Adding new features to code is different from writing new code in that it never involves just adding code. New feature work almost always starts with redesigning the existing code base so that it is sympathetic to the new feature area. Only once the code has been refactored can the adding of new code begin. Once the new code has been written it must still be hooked into the existing code which requires some modification of existing code. As an example, I am currently working on adding generics to the C# compiler. This requires a fundamental change in the data structures used by the compiler which I've been working on for most of the last week, and will continue well into the next. When I'm done this change I will have changed about 5% of the lines of code in the compiler (some 4,500 lines of code) while adding exactly no new code. Only once the refactoring is complete can the real work of adding the individual parts of generics into the compiler begin. Not all features require this dramatic a refactoring, but it is not atypical either. Currently the tools used to modify existing code include intellisense and find in files. Visual designers can also help here, provided that the code was authored in the designer and the designer is not confused by all that grungy real world code which you've added to your project.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Verifying Existing Code</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Verifying existing code means writing and running test suites to ensure that recent bug fixes haven't caused regressions. It also means stepping through recently changed code in the debugger to verify the new behavior. Industry thought leaders have recently made a big fuss about regression testing, and have expounded testing frameworks like JUnit. It has been my experience that regression testing always pays off, but that is another mail entirely.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Priorities</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Of the above tasks VS is the primary tool for 3. Writing new code, understanding existing code, and modifying existing code. The real question is "What percentage of time&nbsp;do real developers&nbsp;spend in each of these three activities?". The answer may surprise you, so think hard before continuing? </FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>My answers are:</FONT></SPAN></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>New Code:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2%</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Modifying Existing Code:&nbsp;&nbsp;&nbsp;&nbsp; 20%</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Understanding Code:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 78%</FONT></SPAN></DIV></BLOCKQUOTE>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Now, I fit solidly in the Einstein user profile. I code all day every day. I'm a C++ power user. I read x86 assembly natively and can decipher the raw bytes of machine code in a pinch. I've been working on the same code base for 3.5 years, and will likely continue to work on that code base for another 3.5 years. A better question is, what do these numbers look like for Elvis. Now, many, many moons ago I used to be Elvis. I had a spanking new 386 on my desk. My primary tool for writing code was a pencil. Browsing code involved a printer and a highlighter. Yes, yes&nbsp;the year was 1988, and I'm writing a Futures and Options accounting system for Citibank in DBASE III+ as an intern... Oh my god I'm wearing the most god-awful blue pinstripe suit. </FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>So what are Elvis's numbers like:</FONT></SPAN></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>New Code:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5%</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Modifying Existing Code:&nbsp;&nbsp;&nbsp;&nbsp; 25%</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Understanding Code:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 70%</FONT></SPAN></DIV></BLOCKQUOTE>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>No, I am not making this up.</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Let me explain where these numbers come from. First: Why is 5 times more time spent modifying code than writing new code? The answer is that new code becomes old code almost instantly. Write some new code. Go for coffee. All of sudden you've got old code.&nbsp;Brand spanking&nbsp;new code reflects at most only the initial design however most design doesn't happen up front. Most development projects use the iterative development methodology. Design, code, test, repeat. Repeat a lot. Only the coding in the first iteration qualifies as all new code. After the first iteration coding quickly shifts to be more and more modifying rather than new coding. Also, almost all code changes made while bug fixing falls into the modifying code category. Look at VS, our stabilization (aka bug fixing) milestones are as long as our new feature milestones. Modifying code consumes much more of&nbsp;a professional developer's&nbsp;time than writing new code.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Secondly, why does understanding code take 3 times more of a developers time than modifying code? The answer here is that before modifying code, you must first understand what it does. This is true of any refactoring of existing code - you must understand the behavior of the code so that you can guarantee that the refactoring didn't change anything unintended. When debugging, much more time is spent understanding the problem than actually fixing it, and once you've fixed it, you need to understand the new code to ensure that the fix was valid. And lastly, even when coding new code, you never start from scratch. You will be calling existing code to do most of your work. Either user written code or a library supplied by Microsoft or a third party for which no source is available. Before calling this existing code you must understand it in precise detail. When writing my first XML enabled app, I spent much more time figuring out the details of the XML class libraries than I did actually writing code. When adding new features you must understand the existing features so that you can reuse where appropriate. Understanding code is by far the activity at which professional developers spend most of their time.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Visual Studio is not&nbsp;Focused on&nbsp;Real Coders</STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>I recently asked a couple of PM friends of mine (who will remain nameless to protect the innocent)&nbsp;where&nbsp;they figured developers spent their time.&nbsp;Their estimates ranked writing&nbsp;and modifying&nbsp;code above understanding code. Looking at the feature list for Whidbey I think that this is consistent with many folks across the division.&nbsp;It sounds reasonable&nbsp;that writing code is the primary activity for folks who write code for a living but it is in fact very far from the truth. I think there are several reasons why our current focus is on writing code rather than understanding code.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Many of our new features are designed by folks who write small demo apps. Lets face it, PM's aren't professional developers. They need to come up with code snippets and examples which are trimmed down for presentations and conferences. This results in features which demo well, but will in fact provide little or even negative benefit to the real coding task of understanding code. Staring at code for hours&nbsp;just doesn't demo well.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Our usability studies are run over a timeline of 3 - 4 hours. This includes problem definition, problem solution and post mortem. The last 2 usability studies I saw were basically "write this self contained dumbed down piece of code from scratch". </FONT></SPAN><SPAN class=740242718-02082002><FONT face=Arial size=2>This does not even remotely resemble real world professional coding. The last time I had a coding project like that I was in college. Early in college. A much more representative task would be to send a coder at an existing piece of code that they'd never seen, that was undocumented, badly written, badly architected and had several bugs. Then tell them to add a new feature while maintaining the existing behavior as much as possible. It may be difficult to get anything useful out of a short study, but it would be much more representative of real professional development. Even in the usability studies that I have seen, the users have spent a ton of time in help trying to understand our class libraries (aka understanding existing code).</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Many of our ideas for new features are a response to questions from users on our newsgroups. Again, this skews the data towards the new user, writing their first program. Most of our questions on the newsgroups come from new users who have just installed the product. Well, guess what? No matter how good the product is they will always ask 'dumb' questions while they try and get their head around this monster new product they've just installed. Once they've gotten a little familiar with the product their usage of the product will change drastically. The things which they spend time on in the first few weeks will be dramatically different the things that they spend their time on after they have learned the basics of the product. We really don't get any good feedback from newsgroups on the real day to day usability of our product.</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>In <a href="http://blogs.msdn.com/peterhal/archive/2004/08/11/213120.aspx">The Yardstick</A>, I outlined a system for evaluating features. You count up the time saved and compare that&nbsp;against the time cost to give a net time saved. Lets run a couple of examples here:</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2><STRONG>Example 1: </STRONG></FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002></SPAN><SPAN class=740242718-02082002><FONT face=Arial size=2>Time Saved: 10% of new code time</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>Time Cost: 1% of understanding code time</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>This looks like a pretty attractive feature. saving 10%, costing 1% looks good. But now factor in the fact that much more time is spent understanding code then writing new code and try again:</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><FONT face=Arial size=2>
<DIV><SPAN class=740242718-02082002></SPAN><SPAN class=740242718-02082002><FONT face=Arial size=2>Real Time Saved: 10% of new code time = 0.5% of Total Dev Time</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002>Real </SPAN><SPAN class=740242718-02082002><FONT face=Arial size=2>Time Cost: 1% of understanding code time = 0.7% of Total Dev Time</FONT></SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>Net Time Saved: -0.2% of Total Dev Time</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>Now we realize that even this small sacrifice of understanding code results in a net loss in productivity for our customers.</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002><STRONG>Example 2:</STRONG></SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>Time Saved: 10% of understanding code time</SPAN></DIV>
<DIV><SPAN class=740242718-02082002>Time Cost: 10% of writing new code time</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>At first glance this looks like a wash, no saving, but factor in the relative time again:</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>Real Time Saved: 10% of understanding code time = 7% of Total Dev Time</SPAN></DIV>
<DIV><SPAN class=740242718-02082002>Real Time Cost: 10% of writing new code time = 0.5% of Total Dev Time</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>Net Time Saved: 6.5% of Total Dev Time</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>Again, because much more time is spent in the understanding code task this feature is in fact a significant usability win.</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>So now the question comes : where should we spend our resources to improve VS in ways which will most benefit our customers. Clearly we should focus our efforts on making it easier for users to understand existing code, but the conclusions are in fact much more dramatic than that. If we could reduce the amount of time spent understanding code by a mere 10%, that would save the user more time than a 100% reduction in the amount of time spent writing new code. Think about what that really means for a moment. If we spent a ton of work, making intellisense, designers&nbsp;and wizards so good that writing new code took no time at all. Zero time. The ESP coding interface. That would still have less developer impact than a 10% reduction in the amount of time developers spend understanding the code base they are working in. The other conclusion to draw from the above, is that any new feature which impedes the developers primary focus of understanding code will allmost certainly be a net usability loss. In fact I'll go one step further and say that cutting existing features which impede the user's ability to understand existing code will result in more productive programmers.</SPAN></DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002></SPAN>&nbsp;</DIV>
<DIV><SPAN class=740242718-02082002>Peter</SPAN></DIV></FONT></SPAN></DIV>]]></content:encoded>
    </item>
    <item>
      <title>C# Stumper: Why does this code not compile?</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/c-stumper-why-does-this-code-not-compile</link>
      <pubDate>Wed, 09 Nov 2005 15:50:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2005/11/09/c-stumper-why-does-this-code-not-compile/</guid>
      <description><![CDATA[Hey folks,
First off, I want to appologize for not having any activity on my blog for a while. I...]]></description>
      <content:encoded><![CDATA[<P>Hey folks,</P>
<P>First off, I want to appologize for not having any activity on my blog for a while. I just got back from a wonderful 3 week vacation&nbsp;in Spain. Now that I'm back, rested and limber, here's a twisted peice of C# code which is gauranteed to turn your brain inside out. </P>
<P>Why does the compiler (correctly) give an error message on the override in the following code?</P>
<P><FONT face="Courier New" color=blue><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">abstract</SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <FONT color=blue><SPAN style="COLOR: blue">class</SPAN></FONT> <FONT color=teal><SPAN style="COLOR: teal">A</SPAN></FONT>&lt;T&gt;<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">{<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; <FONT color=blue><SPAN style="COLOR: blue">public</SPAN></FONT> <FONT color=blue><SPAN style="COLOR: blue">abstract</SPAN></FONT> T getT();<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR>&nbsp;&nbsp;&nbsp; <FONT color=blue><SPAN style="COLOR: blue">class</SPAN></FONT> <FONT color=teal><SPAN style="COLOR: teal">B</SPAN></FONT> : <FONT color=teal><SPAN style="COLOR: teal">A</SPAN></FONT>&lt;<FONT color=teal><SPAN style="COLOR: teal">B</SPAN></FONT>&gt;<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; {<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT color=blue><SPAN style="COLOR: blue">public</SPAN></FONT> <FONT color=blue><SPAN style="COLOR: blue">override</SPAN></FONT> <FONT color=teal><SPAN style="COLOR: teal">B</SPAN></FONT> getT()<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT color=blue><SPAN style="COLOR: blue">throw</SPAN></FONT> <FONT color=blue><SPAN style="COLOR: blue">new</SPAN></FONT> System.<FONT color=teal><SPAN style="COLOR: teal">Exception</SPAN></FONT>(<FONT color=maroon><SPAN style="COLOR: maroon">"The method or operation is not implemented."</SPAN></FONT>);<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; }<BR></SPAN></FONT><FONT face="Courier New"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">}<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal><FONT face="Courier New" size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p><FONT face="Times New Roman" size=3>I got this code from a coworker, and I must confess that the first time I saw it I was convinced that the behaviour was a compiler bug. It turns out that the compiler correctly diagnoses the problem, though even after seeing the error message it took me a while to figure out what's going on. I'll post a discussion in a couple of days.</FONT></o:p></SPAN></FONT></P>
<P class=MsoNormal><FONT face="Times New Roman" size=3><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>Any Takers?</o:p></SPAN></FONT></P>
<P class=MsoNormal><FONT face="Times New Roman" size=3><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>Peter<BR></o:p></SPAN></FONT><FONT face="Times New Roman" size=3><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>C# Guy</o:p></SPAN></FONT></P>]]></content:encoded>
    </item>
    <item>
      <title>Many C# Questions: Switching on non-constant values.</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/many-c-questions-switching-on-non-constant-values</link>
      <pubDate>Fri, 12 Aug 2005 20:50:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2005/08/12/many-c-questions-switching-on-non-constant-values/</guid>
      <description><![CDATA[I finally decided to play with the style settings on my blog. As you may have guessed, I'm a bit of...]]></description>
      <content:encoded><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">I finally decided to play with the style settings on my blog. As you may have guessed, I'm a bit of a newbie when it comes to websites and blogging. Let me know what you think of the new look.&nbsp;Last weeks posting generated some great comments. Tzagotta asks:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Why are constant expressions required in case labels? I see a switch statement conceptually the same as a series of chained ifs/else ifs.<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">This is an interesting question. It seems reasonable that you should be able to switch on an expression of any type, and have case labels which can take any value – even values which are not compile time constants. A simple example would look something like this:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">class</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> Program {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place w:st="on">Main</st1:place>() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> myFirstObject = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> mySecondObject = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> obj = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">switch</SPAN> (obj) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> myFirstObject:<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>...; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> mySecondObject: ...; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">default</SPAN>:<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>...; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">which would be equivalent to this code:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">class</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> Program {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> <st1:place w:st="on">Main</st1:place>() {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> myFirstObject = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> mySecondObject = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">object</SPAN> obj = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(obj == myFirstObject) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>...<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>} <SPAN style="COLOR: blue">else</SPAN> <SPAN style="COLOR: blue">if</SPAN> (obj == mySecondObject) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>...<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>} <SPAN style="COLOR: blue">else</SPAN> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>...<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">At first glance this seems like a great idea – the switch is slightly more compact and readable than the chained ifs, both desirable characteristics of a programing language. If the construct could easily be limited to this simple example it might be a reasonable language extension to consider. Unfortunately, things are not as simple as they appear at first glance…<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Firstly, what happens when myFirstObject is equal to mySecondObject? Well that depends on which case label came first in your switch. The order of the case labels becomes significant in determining which block of code gets executed. Because the case label expressions are not constant the compiler cannot verify that the values of the case labels are distinct, so this is a possibility which must be catered to. This runs counter to most programmers’ intuition about the switch statement in a couple of ways. Most programmers would be surprised to learn that changing the order of their case blocks changed the meaning of their program. To turn it around, it would be surprising if the expression being switched on was equal to an expression in a case label, but control didn’t go to that label.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Secondly, allowing non-constant expressions as case labels lets in some significantly less desirable code as well. For example, this code would also be legal:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">switch</SPAN> (obj) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> myFirstObject:<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>...; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> mySecondObject: ...; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> EraseMyHardDrive(): ...; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">default</SPAN>:<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>...; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">In C#, any non-constant expression can yield side effects. There is currently no notion of a non-constant expression which does not have side effects in C#. Seeing code like this will leave the reader scratching their head wondering when their hard drive will get erased.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">And lastly, programmers coming from a C/C++ background will expect that the execution of a switch statement will be fast and that it will take about the same time to reach any particular case label. When the case labels are constant integral values (or strings), the compiler can do some great optimizations to make the execution speed of the construct match the coder’s expectation. With non-constant case labels, getting to the 100<SUP>th</SUP> case label would take significantly more time than getting to the 10<SUP>th</SUP> case label. Again, this would be surprising to the programmer. <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Peter<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">C# Guy<o:p></o:p></SPAN></P>]]></content:encoded>
    </item>
    <item>
      <title>Many Questions: Switch On Enum</title>
      <link>https://docs.microsoft.com/archive/blogs/peterhal/many-questions-switch-on-enum</link>
      <pubDate>Mon, 01 Aug 2005 21:15:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/peterhal/2005/08/01/many-questions-switch-on-enum/</guid>
      <description><![CDATA[Just a quick one this week:&nbsp;
&nbsp;
Why is it that you cannot use enum constants in a switch...]]></description>
      <content:encoded><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Just a quick one this week:&nbsp;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Why is it that you cannot use enum constants in a switch statement's cases without first casting them to type int?<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Often you will want to use Enum constants as case labels in switch statements. Sometimes, the compiler will complain and require a cast to </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">int</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"> on each case label. This will look something like this:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>enum</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> Color { Red, Green, Blue };<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> i = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">switch</SPAN> (i)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: green">// cast required! Aarg!<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> (<SPAN style="COLOR: blue">int</SPAN>)Color.Red:<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> (<SPAN style="COLOR: blue">int</SPAN>)Color.Green:<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> (<SPAN style="COLOR: blue">int</SPAN>)Color.Blue:<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">The confusion stems from a subtle difference between C++ and C#. In C++, values of enum type are implicitly convertible to int. In C#, conversions between an enum type and its underlying type are explicit and require a cast. To maintain consistency, the requirement for the cast carries over into the use of enums in switch statements.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">However, you can use expressions of enum type as case labels without a cast, but only in a type safe way. The rule is that the type of the expression being switched on, the ‘governing type’ of the switch statement in C# language spec terminology, must match the type of the expressions in the case labels. <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">For example:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>enum</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> Color { Red, Green, Blue };<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>Color c = ...;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">switch</SPAN> (c)<SPAN style="COLOR: green"> // Governing type is ‘Color’ not ‘int’ so ...<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: green">// ... no cast required<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> Color.Red:<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> Color.Green:<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">case</SPAN> Color.Blue:<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">One of the design goals for enums in C# was to treat them as first class types that were truly distinct from their underlying type. This is one of the subtle ways that this decision manifests itself in the language. <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Peter<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">C# Guy<o:p></o:p></SPAN></P>]]></content:encoded>
    </item>
  </channel>
</rss>