<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Neal's Blog</title><link>http://blogs.msdn.com/nealho/default.aspx</link><description /><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Overrides and overload resolution, revisited</title><link>http://blogs.msdn.com/nealho/archive/2006/08/14/700200.aspx</link><pubDate>Tue, 15 Aug 2006 00:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:700200</guid><dc:creator>nealho</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/nealho/comments/700200.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=700200</wfw:commentRss><description>&lt;p&gt; I've got some fun LINQ-related discussion to post soon, but I
thought it'd be good to add a bit of a wrap-up to the last discussion
here about overload resolution.&amp;nbsp; I was happy at first having
verified that our behavior matched the spec, so I moved on to various
other things.&amp;nbsp; However, the customer who brought the issue to me
in the first place was unsatisfied with this response.&amp;nbsp; He wanted
to know the reasoning behind the behavior, not just that we had
documented our behavior correctly.&amp;nbsp; Thanks to &lt;a href="http://blogs.extremeoptimization.com/jeffrey/"&gt;Jeffrey Sax&lt;/a&gt; for raising this issue, and hounding me about
    it until I delved deeper and really thought about C# language design. &amp;nbsp;Since
    much of my job involves verifying the compiler's behavior against the spec, it's easy
    to forget that I also need to be continually evaluating that spec and the decisions
    we've made in the past.&amp;nbsp;
    His &lt;a href="http://blogs.extremeoptimization.com/jeffrey/archive/2006/02/10/7134.aspx"&gt;blog post&lt;/a&gt; on the subject is a great read and highlights both the design principles behind
        the decision to prefer more derived methods and some of the problems with it, so
    I won't go into too much detail, but if you read my previous article on overload
    resolution, I highly recommend it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=700200" width="1" height="1"&gt;</description></item><item><title>Overrides and Overload Resolution</title><link>http://blogs.msdn.com/nealho/archive/2006/01/19/515173.aspx</link><pubDate>Fri, 20 Jan 2006 05:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:515173</guid><dc:creator>nealho</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/nealho/comments/515173.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=515173</wfw:commentRss><description>    &lt;p&gt;I got an e-mail earlier this week asking a fairly common question regarding C#&amp;rsquo;s
    overload resolution.&amp;nbsp; Before I launch into a discussion of the actual behavior
    of the compiler, think about what the output of the following program will
    be:&lt;/p&gt;

&lt;pre style="font-size:14px"&gt;&lt;span class="keyword"&gt;using&lt;/span&gt; System;

&lt;span class="keyword"&gt;public class&lt;/span&gt; &lt;span class="type"&gt;Program&lt;/span&gt; {
    &lt;span class="keyword"&gt;public static void&lt;/span&gt; Main() {
        &lt;span class="type"&gt;Derived&lt;/span&gt; d = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;Derived&lt;/span&gt;();
        &lt;span class="type"&gt;Console&lt;/span&gt;.WriteLine(d.Method(&lt;span class="string"&gt;"My String"&lt;/span&gt;));
    }
}

&lt;span class="keyword"&gt;public class&lt;/span&gt; &lt;span class="type"&gt;Base&lt;/span&gt; {
    &lt;span class="keyword"&gt;public virtual string&lt;/span&gt; Method(&lt;span class="keyword"&gt;string&lt;/span&gt; s) {
        &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="string"&gt;"Base.Method(string)"&lt;/span&gt;;
    }
}

&lt;span class="keyword"&gt;public class&lt;/span&gt; &lt;span class="type"&gt;Derived&lt;/span&gt; : &lt;span class="type"&gt;Base&lt;/span&gt; {
    &lt;span class="keyword"&gt;public override string&lt;/span&gt; Method(&lt;span class="keyword"&gt;string&lt;/span&gt; s) {
        &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="string"&gt;"Derived.Method(string)"&lt;/span&gt;;
    }
    &lt;span class="keyword"&gt;public virtual string&lt;/span&gt; Method(&lt;span class="keyword"&gt;object&lt;/span&gt; o) {
        &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="string"&gt;"Derived.Method(object)"&lt;/span&gt;;
    }
}&lt;/pre&gt;

&lt;p&gt;
    My first thought, as well as the first thought of most people I&amp;rsquo;ve talked to about
    this, is that this code would call Derived.Method(string) because it is the best
    overload.&amp;nbsp; However, as you either know from having tried it or have already
    guessed from the very fact that I&amp;rsquo;m writing this post, the method
    that actually gets called is Derived.Method(object).&amp;nbsp; In tracking this issue
    down, I started where I usually do&amp;mdash;with the C# language spec.&amp;nbsp; As I skimmed
    through overload resolution, it seemed to confirm what I had thought originally&amp;mdash;a
    method that requires no conversion is a better choice than a method that requires
    a conversion.&amp;nbsp; Puzzled, I moved on, and read the part about member access,
    where I concluded that the result of looking up Method on an instance of Derived
    should be the method group containing both overloads.&amp;nbsp; Well, that was no help,
    so I read deeper.&amp;nbsp; Looking at the next section, about invocation expressions,
    I read the following:&amp;nbsp; &amp;ldquo;The set of candidate methods is reduced to contain only methods from the most derived types.&amp;rdquo;&amp;nbsp; Some normative text followed,
    but this is the detail I had been missing.&lt;/p&gt;
    &lt;p&gt;
        The methods in a derived class are preferred to inherited methods.&amp;nbsp; Add to
        that the fact that the compiler doesn&amp;rsquo;t consider overrides when performing overload
        resolution&amp;mdash;only methods introduced by a given class are considered to be on that class—and, surprisingly enough, the compiler is making the correct
        choice.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=515173" width="1" height="1"&gt;</description></item><item><title>C#, Java, and CS Education</title><link>http://blogs.msdn.com/nealho/archive/2006/01/05/509892.aspx</link><pubDate>Fri, 06 Jan 2006 03:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:509892</guid><dc:creator>nealho</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/nealho/comments/509892.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=509892</wfw:commentRss><description>    &lt;p&gt;
        I read an &lt;a href="http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html"&gt;
            interesting article&lt;/a&gt; the other day about how CS is taught in school now.&amp;nbsp;
        &amp;nbsp;The article basically argues that you can&amp;rsquo;t learn real computer science
        skills in Java.&amp;nbsp; Without debating his attitude towards the Java language itself
        (and by extension, the C# language), I thought I&amp;rsquo;d take a moment to write
        some thoughts regarding the article.&lt;/p&gt;
    &lt;p&gt;
        I went to Rice University, and while I don&amp;rsquo;t think it is exactly what he describes
        so disparagingly as a &amp;ldquo;Java School,&amp;rdquo; Java was the primary language that
        I used while there.&amp;nbsp; At Rice, the introductory programming course is taught
        in Scheme; computer organization is taught in some combination of C and SPARC assembly;
        programming languages and compilers are taught in some combination of C, Java, and
        Scheme; of course, operating systems is taught in C; however, intermediate programming,
        algorithms and data structures, and most software engineering courses are taught
        in Java (some of the newer ones in C#, but the two are effectively the same for
        the purposes of this discussion).&amp;nbsp; Oddly enough, I knew almost nothing about
        C++ until my summer internship after junior year, where I spent the entire summer
        getting a painful crash course in how C++ programming is &lt;em&gt;not&lt;/em&gt; like Java
        programming.&amp;nbsp; But I digress.&lt;/p&gt;
    &lt;p&gt;
        You&amp;rsquo;ll note a preponderance of non-Java languages in that laundry list of
        courses and languages, and I think that&amp;rsquo;s important to have.&amp;nbsp; It &lt;em&gt;is&lt;/em&gt;
        important for computer scientists to be able to write real code in C, using pointers
        and hacking bits for all they&amp;rsquo;re worth, because there is a lot of real-world
        code that needs that level of control.&amp;nbsp; It&amp;rsquo;s equally important for computer
        scientists to be able to think about programs at a functional level:&amp;nbsp; functional
        programs teach you the important lesson that code and data are really the same thing.&amp;nbsp;
        So what, then, are we even doing teaching people silly object-oriented languages
        like C# and Java?&lt;/p&gt;
    &lt;p&gt;
        In my opinion, we are teaching those languages precisely because they are &amp;ldquo;easier.&amp;rdquo;&amp;nbsp;
        It&amp;rsquo;s &lt;em&gt;easier&lt;/em&gt; to express your intent in a good OO language than it
        is in C.&amp;nbsp; You gain the powerful abstraction of classes, which, in addition
        to the usual yadda yadda about polymorphism and various OO patterns, come with nearly
        everything you wanted from functional closures (though I won&amp;rsquo;t claim that
        you get the same elegance and power as Lisp/Scheme macros&amp;mdash;but OO vs. Scheme
        as a language of choice is not the topic today).&amp;nbsp; Thanks to the managed environment,
        you don&amp;rsquo;t have to worry about corrupting some random part of memory or blowing
        your stack to smithereens, nor do you have to allocate memory with careful precision
        so that you can remember to free it later.&amp;nbsp; You have all the tools you need
        to write your code while decreasing the overhead involved.&lt;/p&gt;
    &lt;p&gt;
        I think he&amp;rsquo;s right that Java isn&amp;rsquo;t the be-all and end-all of computer
        science (neither is C#, much as I&amp;rsquo;d love to claim that it is).&amp;nbsp; He&amp;rsquo;s
        absolutely right that the sort of &amp;ldquo;mental flexibility you get from learning
        about&amp;rdquo; pointers and functional programming makes you a better programmer in
        any language.&amp;nbsp; Still, he has a certain elitism about his article that rubs
        me the wrong way.&amp;nbsp; I &lt;em&gt;think&lt;/em&gt; I understand pointers, but I wouldn&amp;rsquo;t
        want the fact that I hate working with C-style linked lists to keep me out of a
        job that itself required no C programming.&amp;nbsp; Even if CS departments at universities
        should be training their students for real programming, they shouldn&amp;rsquo;t have
        a responsibility to &amp;ldquo;weed out&amp;rdquo; students who can&amp;rsquo;t grasp the entirety
        of Scheme in a single day.&lt;/p&gt;
    &lt;p&gt;
        In the end, I suppose that you do have to understand pointers because
        all of your programs run on real computers with code that, somewhere underneath,
        is using those pointers.&amp;nbsp; Despite having to understand them, I don&amp;rsquo;t
        think anyone should be required to &lt;em&gt;like&lt;/em&gt; them.&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=509892" width="1" height="1"&gt;</description></item><item><title>Enumerated Types:  Enumerating the Values</title><link>http://blogs.msdn.com/nealho/archive/2005/12/28/507932.aspx</link><pubDate>Thu, 29 Dec 2005 07:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:507932</guid><dc:creator>nealho</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/nealho/comments/507932.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=507932</wfw:commentRss><description>&lt;p&gt;One of the things you lose when doing class-based enums is the support of the System.Enum class and its static methods for all enum types. &amp;nbsp;Take
a look at the &lt;a href="http://msdn2.microsoft.com/6c69y7dk(en-US,VS.80).aspx"&gt;documentation on MSDN&lt;/a&gt;&amp;mdash;the only methods that really remain applicable
are GetValues, which returns an array of all values in the enum, and GetNames, which returns an array of all the names of the values in the enum. &amp;nbsp;(Because
it&amp;rsquo;s trivial to write GetNames from GetValues, I&amp;rsquo;ll omit that in my discussion here.) &amp;nbsp;Other methods are either not applicable (GetUnderlyingType,
IsDefined, ToObject) or really belong as instance methods on the enums themselves (GetName, Format).&lt;/p&gt;

&lt;p&gt;Indeed, it seems silly that you would be unable to enumerate the values in your enumerated type programatically. &amp;nbsp;As a side note before I begin, I
would prefer to have my GetValues method return a &lt;a href="http://en.wikipedia.org/wiki/Set"&gt;set&lt;/a&gt;, but the base class library in the .NET framework lacks
a such a concept, so I&amp;rsquo;ll muddle through with lists and arrays. &amp;nbsp;The first solution I&amp;rsquo;ve used is to add a static set to the enum class
itself, then have the constructor of the enum add the new instance to that set:&lt;/p&gt;

&lt;pre style="font-size:14px"&gt;&lt;span class="keyword"&gt;public abstract class&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; {
    &lt;span class="keyword"&gt;public static readonly&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; One = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;TypeOne&lt;/span&gt;();
    &lt;span class="keyword"&gt;public static readonly&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; Two = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;TypeTwo&lt;/span&gt;();
    &lt;span class="comment"&gt;// ...&lt;/span&gt;

    &lt;span class="keyword"&gt;private static readonly&lt;/span&gt; &lt;span class="type"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="type"&gt;EnumType&lt;/span&gt;&amp;gt; allValues = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="type"&gt;EnumType&lt;/span&gt;&amp;gt;();

    &lt;span class="keyword"&gt;private&lt;/span&gt; EnumType() {
        allValues.Add(this);
    }

    &lt;span class="keyword"&gt;public static&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt;[] GetValues() {
        &lt;span class="keyword"&gt;return&lt;/span&gt; allValues.ToArray();
    }

    &lt;span class="comment"&gt;// ...&lt;/span&gt;
}&lt;/pre&gt;

&lt;p&gt;This seems to me to be a pretty good solution for a single enum type. &amp;nbsp;The main problem is that you end up having to duplicate this code in every
single enum type you create, which could be a lot of extra work if you have a number of enum types. &amp;nbsp;It might be a good time to take a step back and
think about the best way to do this.&lt;/p&gt;

&lt;p&gt;What the .NET framework&amp;rsquo;s GetValues method
does is take the System.Type it&amp;rsquo;s given as a parameter, use reflection on the type to get the values, and assign them to a System.Array, since
System.Enum doesn&amp;rsquo;t know the runtime type of the Type you give at compile-time. &amp;nbsp;Well, even if we use classes, we still will have to use
reflection to get the values&amp;mdash;there&amp;rsquo;s no way to know them at the time you write the base enum type&amp;mdash;but we can avoid requiring you to
pass in the Type as a parameter and return a correctly-typed array type rather than just the general Array supertype with a clever application of
generics. &amp;nbsp;Here&amp;rsquo;s the solution I propose:&lt;/p&gt;

&lt;pre style="font-size:14px"&gt;
&lt;span class="keyword"&gt;public abstract class&lt;/span&gt; &lt;span class="type"&gt;EnumBase&lt;/span&gt;&amp;lt;E&amp;gt; &lt;span class="keyword"&gt;where&lt;/span&gt; E : &lt;span class="type"&gt;EnumBase&lt;/span&gt;&amp;lt;E&amp;gt; {
    &lt;span class="keyword"&gt;public static&lt;/span&gt; E[] GetValues() {
        &lt;span class="type"&gt;FieldInfo&lt;/span&gt;[] fields = &lt;span class="keyword"&gt;typeof&lt;/span&gt;(E).GetFields(&lt;span class="type"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class="type"&gt;BindingFlags&lt;/span&gt;.Static);
        &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="type"&gt;Array&lt;/span&gt;.ConvertAll&amp;lt;&lt;span class="type"&gt;FieldInfo&lt;/span&gt;, E&amp;gt;(fields, &lt;span class="keyword"&gt;delegate&lt;/span&gt;(&lt;span class="type"&gt;FieldInfo&lt;/span&gt; f) { return (E) f.GetValue(&lt;span class="keyword"&gt;null&lt;/span&gt;); });
    }
}

&lt;span class="keyword"&gt;public abstract class&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; : &lt;span class="type"&gt;EnumBase&lt;/span&gt;&amp;lt;&lt;span class="type"&gt;EnumType&lt;/span&gt;&amp;gt; {
    &lt;span class="comment"&gt;// ...&lt;/span&gt;
}&lt;/pre&gt;

&lt;p&gt;Before I say anything else, I&amp;rsquo;ll admit to borrowing part of this idea from Java: &amp;nbsp;in Java 5.0, java.lang.Enum is parameterized in the same way as my
EnumBase type. &amp;nbsp;The constraint on the type parameter is a very difficult one to get your head around, and thinking about it too hard is likely to cause
your head to spin into an infinite loop. &amp;nbsp;I think of it as &amp;ldquo;the type parameter to the EnumBase type has to be the enum type itself.&amp;rdquo; &amp;nbsp;If
it still doesn&amp;rsquo;t make sense to you, I wouldn&amp;rsquo;t worry about it too much; try the code and convince yourself that it does, in fact, compile. &amp;nbsp;However,
even though I got the idea from Java, this method couldn&amp;rsquo;t be written in Java in the same way because of how they implemented generics.&lt;/p&gt;

&lt;p&gt;I like this solution because now, you can simply call EnumType.GetValues, or if you make any other enum type, it will inherit the static GetValues
method, which will do the right thing for that enum. &amp;nbsp;Best of all, all enums can now be related in the class hierarchy, just as System.Enum is a
supertype of all built-in enum types. &amp;nbsp;Thus, if you come up with any more functionality for your enums to share, you can just add it to the base
type, instead of trying to change every enum class itself.&lt;/p&gt;

&lt;p&gt;One final note before I go: &amp;nbsp;because the GetValues method returns an array, it isn&amp;rsquo;t safe to return the same array to all consumers, since
any consumer could modify the array. &amp;nbsp;Ideally, it would return a read-only set, which could be computed once, then cached for future calls, which could
all return that instance. &amp;nbsp;However, with the arrays, you could cache a single copy of the array, then just copy that array upon each call to GetValues
to avoid the cost of performing the reflection on each call.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=507932" width="1" height="1"&gt;</description></item><item><title>Enumerated Types:  Dynamic Differentiation</title><link>http://blogs.msdn.com/nealho/archive/2005/12/19/505673.aspx</link><pubDate>Tue, 20 Dec 2005 02:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:505673</guid><dc:creator>nealho</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/nealho/comments/505673.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=505673</wfw:commentRss><description>&lt;P&gt;So class-based enumerated types allow you to define dynamic behaviors, where the consumer of the enum doesn't have to know specifically which instance of the enum he has; he need only invoke the appropriate method, and the type of the instance will determine the runtime behavior. &amp;nbsp;This use of dynamic dispatch is familiar to all of us object-oriented programming nerds, and imo, this is the cornerstone of OO's power.&lt;/P&gt;
&lt;P&gt;However, we're left with a problem: &amp;nbsp;what if, when designing the code, we don't know all the dynamic behaviors that someone might want to use on our enum? &amp;nbsp;Well, if we're writing code for any kind of public API, then this is obviously the case. &amp;nbsp;Using our model from before, we'd need code like:&lt;/P&gt;&lt;PRE style="FONT-SIZE: 14px"&gt;    &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; myEnumValue;

    &lt;SPAN class=comment&gt;// do something to get your value&lt;/SPAN&gt;

    &lt;SPAN class=keyword&gt;if&lt;/SPAN&gt; (myEnumValue == &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt;.One) {
    }
    &lt;SPAN class=keyword&gt;else if&lt;/SPAN&gt; (myEnumValue == &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt;.Two) {
    }
    &lt;SPAN class=comment&gt;// ... rest of cases&lt;/SPAN&gt;
    &lt;SPAN class=keyword&gt;else&lt;/SPAN&gt; {
        &lt;SPAN class=comment&gt;// this should not be possible, but it's still prudent to cover this case&lt;/SPAN&gt;
        &lt;SPAN class=keyword&gt;throw new&lt;/SPAN&gt; &lt;SPAN class=type&gt;UnreachableCodeException&lt;/SPAN&gt;();
    }&lt;/PRE&gt;
&lt;P&gt;While this is not horrible, it has two issues. &amp;nbsp;The first is its inefficiency—in order to get to any given element of the enum, a comparison must be performed with each element preceding it. &amp;nbsp;The second is that, just as with C#'s built-in enums, you are left with that final else block. &amp;nbsp;It should be unreachable, but the compiler can't tell you that for sure.&lt;/P&gt;
&lt;P&gt;The most obvious solution is to restore the functionality given by built-in enums by adding an integer index to each enum value. &amp;nbsp;Because case labels must be constant, this is a bit awkward and requires adding constants for each enum value so that these indices can be known at compile-time:&lt;/P&gt;&lt;PRE style="FONT-SIZE: 14px"&gt;&lt;SPAN class=keyword&gt;public abstract class&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; {
    &lt;SPAN class=keyword&gt;public static readonly&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; One = &lt;SPAN class=keyword&gt;new&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeOne&lt;/SPAN&gt;();
    &lt;SPAN class=keyword&gt;public const int&lt;/SPAN&gt; OneValue = 1;
    &lt;SPAN class=keyword&gt;public static readonly&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; Two = &lt;SPAN class=keyword&gt;new&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeTwo&lt;/SPAN&gt;();
    &lt;SPAN class=keyword&gt;public const int&lt;/SPAN&gt; TwoValue = 2;
    &lt;SPAN class=comment&gt;// ...&lt;/SPAN&gt;

    &lt;SPAN class=keyword&gt;private int&lt;/SPAN&gt; _value;
    &lt;SPAN class=keyword&gt;private string&lt;/SPAN&gt; _name;

    &lt;SPAN class=keyword&gt;private&lt;/SPAN&gt; EnumType(&lt;SPAN class=keyword&gt;int&lt;/SPAN&gt; value, &lt;SPAN class=keyword&gt;string&lt;/SPAN&gt; name) {
        _value = value;
        _name = name;
    }

    &lt;SPAN class=keyword&gt;public abstract void&lt;/SPAN&gt; DoSomething();

    &lt;SPAN class=keyword&gt;public int&lt;/SPAN&gt; Value {
        &lt;SPAN class=keyword&gt;get&lt;/SPAN&gt; {
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; _value;
        }
    }

    &lt;SPAN class=keyword&gt;public string&lt;/SPAN&gt; Name {
        &lt;SPAN class=keyword&gt;get&lt;/SPAN&gt; {
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; _name;
        }
    }

    &lt;SPAN class=keyword&gt;private class&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeOne&lt;/SPAN&gt; : &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; {
        &lt;SPAN class=keyword&gt;public&lt;/SPAN&gt; TypeOne() : &lt;SPAN class=keyword&gt;base&lt;/SPAN&gt;(OneValue, &lt;SPAN class=string&gt;"One"&lt;/SPAN&gt;) {
        }

        &lt;SPAN class=keyword&gt;public override void&lt;/SPAN&gt; DoSomething() {
            &lt;SPAN class=comment&gt;// Do whatever One does&lt;/SPAN&gt;
        }
    }
    
    &lt;SPAN class=keyword&gt;private class&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeTwo&lt;/SPAN&gt; : &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; {
        &lt;SPAN class=keyword&gt;public&lt;/SPAN&gt; TypeTwo() : &lt;SPAN class=keyword&gt;base&lt;/SPAN&gt;(TwoValue, &lt;SPAN class=string&gt;"Two"&lt;/SPAN&gt;) {
        }

        &lt;SPAN class=keyword&gt;public override void&lt;/SPAN&gt; DoSomething() {
            &lt;SPAN class=comment&gt;// Do whatever Two does&lt;/SPAN&gt;
        }
    }
    
    &lt;SPAN class=comment&gt;// ...&lt;/SPAN&gt;
}&lt;/PRE&gt;
&lt;P&gt;We are now able to use a switch statement on this enum:&lt;/P&gt;&lt;PRE style="FONT-SIZE: 14px"&gt;   &lt;SPAN class=keyword&gt;switch&lt;/SPAN&gt; (myEnumValue.Value) {
        &lt;SPAN class=keyword&gt;case&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt;.OneValue:
            &lt;SPAN class=comment&gt;// myEnumValue == EnumType.One&lt;/SPAN&gt;
            &lt;SPAN class=keyword&gt;break&lt;/SPAN&gt;;
        &lt;SPAN class=keyword&gt;case&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt;.TwoValue:
            &lt;SPAN class=comment&gt;// myEnumValue == EnumType.Two&lt;/SPAN&gt;
            &lt;SPAN class=keyword&gt;break&lt;/SPAN&gt;;
        &lt;SPAN class=comment&gt;// ...&lt;/SPAN&gt;
        &lt;SPAN class=keyword&gt;default&lt;/SPAN&gt;:
            &lt;SPAN class=keyword&gt;throw new&lt;/SPAN&gt; &lt;SPAN class=type&gt;UnreachableCodeException&lt;/SPAN&gt;();
            &lt;SPAN class=keyword&gt;break&lt;/SPAN&gt;;
    }&lt;/PRE&gt;
&lt;P&gt;Well, I think we've improved our perf and code readability, but we've introduced something pretty ugly: &amp;nbsp;every enum value now requires two declarations instead of just one. &amp;nbsp;This may not be too bad at first, but it could be a maintenance issue, and if the constants get out of sync with their corresponding enum instances, it could also be a source of some pretty insidious bugs. &amp;nbsp;Moreover, we still have that default block, because the compiler doesn't know for sure that we've covered all our enum cases.&lt;/P&gt;
&lt;P&gt;For serious OOP junkies like me, the solution is probably screaming at you: &amp;nbsp;use the visitor pattern. &amp;nbsp;For those of you not familiar with it, a “visitor” is a class that implements an algorithm on the type being visited. &amp;nbsp;The class then has a “visitor hook,” which is a method that calls the appropriate case in the visitor class. &amp;nbsp;So your new code would look like this:&lt;/P&gt;&lt;PRE style="FONT-SIZE: 14px"&gt;&lt;SPAN class=keyword&gt;public interface&lt;/SPAN&gt; &lt;SPAN class=type&gt;IEnumTypeVisitor&lt;/SPAN&gt;&amp;lt;T&amp;gt; {
    T ForOne();
    T ForTwo();
    &lt;SPAN class=comment&gt;// ...&lt;/SPAN&gt;
}

&lt;SPAN class=keyword&gt;public abstract class&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; {
    &lt;SPAN class=keyword&gt;public static readonly&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; One = &lt;SPAN class=keyword&gt;new&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeOne&lt;/SPAN&gt;();
    &lt;SPAN class=keyword&gt;public static readonly&lt;/SPAN&gt; &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; Two = &lt;SPAN class=keyword&gt;new&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeTwo&lt;/SPAN&gt;();
    &lt;SPAN class=comment&gt;// ...&lt;/SPAN&gt;

    &lt;SPAN class=keyword&gt;private string&lt;/SPAN&gt; _name;

    &lt;SPAN class=keyword&gt;private&lt;/SPAN&gt; EnumType(&lt;SPAN class=keyword&gt;&lt;/SPAN&gt;&lt;SPAN class=keyword&gt;string&lt;/SPAN&gt; name) {
        _name = name;
    }

    &lt;SPAN class=keyword&gt;public abstract&lt;/SPAN&gt; T Accept&amp;lt;T&amp;gt;(&lt;SPAN class=type&gt;IEnumTypeVisitor&lt;/SPAN&gt;&amp;lt;T&amp;gt; visitor);

    &lt;SPAN class=keyword&gt;public string&lt;/SPAN&gt; Name {
        &lt;SPAN class=keyword&gt;get&lt;/SPAN&gt; {
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; _name;
        }
    }

    &lt;SPAN class=keyword&gt;private class&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeOne&lt;/SPAN&gt; : &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; {
        &lt;SPAN class=keyword&gt;public&lt;/SPAN&gt; TypeOne() : &lt;SPAN class=keyword&gt;base&lt;/SPAN&gt;(&lt;SPAN class=string&gt;"One"&lt;/SPAN&gt;) {
        }

        &lt;SPAN class=keyword&gt;public override&lt;/SPAN&gt; T Accept&amp;lt;T&amp;gt;(&lt;SPAN class=type&gt;IEnumTypeVisitor&lt;/SPAN&gt;&amp;lt;T&amp;gt; visitor) {
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; visitor.ForOne();
        }
    }

    &lt;SPAN class=keyword&gt;private class&lt;/SPAN&gt; &lt;SPAN class=type&gt;TypeTwo&lt;/SPAN&gt; : &lt;SPAN class=type&gt;EnumType&lt;/SPAN&gt; {
        &lt;SPAN class=keyword&gt;public&lt;/SPAN&gt; TypeTwo() : &lt;SPAN class=keyword&gt;base&lt;/SPAN&gt;(&lt;SPAN class=string&gt;"Two"&lt;/SPAN&gt;) {
        }

        &lt;SPAN class=keyword&gt;public override&lt;/SPAN&gt; T Accept&amp;lt;T&amp;gt;(&lt;SPAN class=type&gt;IEnumTypeVisitor&lt;/SPAN&gt;&amp;lt;T&amp;gt; visitor) {
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; visitor.ForTwo();
        }
    }

    &lt;SPAN class=comment&gt;// ...&lt;/SPAN&gt;
}

&lt;SPAN class=keyword&gt;public class&lt;/SPAN&gt; &lt;SPAN class=type&gt;MyEnumVisitor&lt;/SPAN&gt; : &lt;SPAN class=type&gt;IEnumTypeVisitor&lt;/SPAN&gt;&amp;lt;&lt;SPAN class=keyword&gt;string&lt;/SPAN&gt;&amp;gt; {
    &lt;SPAN class=keyword&gt;public string&lt;/SPAN&gt; ForOne() {
        &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; &lt;SPAN class=string&gt;"This is MyEnumVisitor, and it was called on an EnumType.One!"&lt;/SPAN&gt;;
    }

    &lt;SPAN class=keyword&gt;public string&lt;/SPAN&gt; ForTwo() {
        &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; &lt;SPAN class=string&gt;"This is MyEnumVisitor, and it was called on an EnumType.Two!"&lt;/SPAN&gt;;
    }

    &lt;SPAN class=comment&gt;// ...&lt;/SPAN&gt;
}&lt;/PRE&gt;
&lt;P&gt;Now, any consumer can write their own IEnumTypeVisitor to define their own dynamic behaviors, and simply call the Accept method of their enum instance to have the correct case automatically determined by the runtime. &amp;nbsp;We've even removed the extra default case that we know we should never reach: &amp;nbsp;because the possible enum values are determined by the visitor interface, the compiler knows that we will never hit another case.&lt;/P&gt;
&lt;P&gt;I'm not going to claim this enum design is perfect, but I think it is much better than the integer-based alternative. &amp;nbsp;By using a truly object-oriented design, I think it’s a lot easier to write clean, elegant code.&lt;/P&gt;
&lt;P&gt;That pretty much covers the basics of how I think about enum design. &amp;nbsp;I’ll post some of my favorite tricks later on.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=505673" width="1" height="1"&gt;</description></item><item><title>Enumerated Types:  Using Classes</title><link>http://blogs.msdn.com/nealho/archive/2005/12/16/504850.aspx</link><pubDate>Sat, 17 Dec 2005 01:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:504850</guid><dc:creator>nealho</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/nealho/comments/504850.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=504850</wfw:commentRss><description>    &lt;p&gt; I said I’d blog about how exactly I use enums, but there are a lot of interesting
        issues here, so I think I’ll develop&amp;nbsp;my approach over the course of a few posts.&amp;nbsp;
        Here’s my simplest form of class-based enum:&lt;/p&gt;
&lt;pre style="font-size:14px"&gt;&lt;span class="keyword"&gt;public class&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; {
    &lt;span class="keyword"&gt;public static readonly&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; One = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt;(&lt;span class="string"&gt;"One"&lt;/span&gt;);
    &lt;span class="keyword"&gt;public static readonly&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; Two = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt;(&lt;span class="string"&gt;"Two"&lt;/span&gt;);
    &lt;span class="comment"&gt;// You can add as many instances as you want, but I’ll stick to two for the sake of brevity&lt;/span&gt;

    &lt;span class="keyword"&gt;private string&lt;/span&gt; _name;

    &lt;span class="keyword" &gt;private&lt;/span&gt; EnumType(&lt;span class="keyword"&gt;string&lt;/span&gt; name) {
        _name = name;
    }
    
    &lt;span class="keyword"&gt;public string&lt;/span&gt; Name {
        &lt;span class="keyword"&gt;get&lt;/span&gt; {
            &lt;span class="keyword"&gt;return &lt;/span&gt;_name;
        }
    }
}&lt;/pre&gt;
    &lt;p&gt; This approach solves one problem C#’s built-in enum types:&amp;nbsp; because the EnumType
        constructor is private, no one else can ever create another instance.&amp;nbsp; If EnumType.One
        and EnumType.Two are all you create in&amp;nbsp;the class,&amp;nbsp;then you know that there
        will be no other instances of EnumType.&lt;/p&gt;
    &lt;p&gt; This style of enum is good for simple enumerations where all you’re doing is differentiating
        between a several options and maybe displaying a value depending on which option
        you pick.&amp;nbsp; To check which value of the enum you have, you can just use reference
        equality and chained if/else blocks, which are admittedly less performant than a
        switch statement, but not notably more difficult to code.&lt;/p&gt;
    &lt;p&gt; The next step is a simple composite pattern, which I’m sure you can all guess, so
        I’ll go on with that right now:&lt;/p&gt;
&lt;pre style="font-size:14px"&gt;&lt;span class="keyword"&gt;public abstract class&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; {
    &lt;span class="keyword"&gt;public static readonly&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; One = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;TypeOne&lt;/span&gt;();
    &lt;span class="keyword"&gt;public static readonly&lt;/span&gt; &lt;span class="type"&gt;EnumType&lt;/span&gt; Two = &lt;span class="keyword"&gt;new&lt;/span&gt; &lt;span class="type"&gt;TypeTwo&lt;/span&gt;();
    &lt;span class="comment"&gt;// ...&lt;/span&gt;

    &lt;span class="keyword"&gt;private&lt;/span&gt; EnumType() {
    }

    &lt;span class="keyword"&gt;public abstract void&lt;/span&gt; DoSomething();

    &lt;span class="keyword"&gt;public abstract string&lt;/span&gt; Name { &lt;span class="keyword"&gt;get&lt;/span&gt;; }

    &lt;span class="keyword"&gt;private class&lt;/span&gt; &lt;span class="type"&gt;TypeOne&lt;/span&gt; : &lt;span class="type"&gt;EnumType&lt;/span&gt; {
        &lt;span class="keyword"&gt;public override void&lt;/span&gt; DoSomething() {
            &lt;span class="type"&gt;Console&lt;/span&gt;.WriteLine(&lt;span class="string"&gt;"I am #1!"&lt;/span&gt;);
        }
        
        &lt;span class="keyword"&gt;public override string&lt;/span&gt; Name {
            &lt;span class="keyword"&gt;get&lt;/span&gt; {
                &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="string"&gt;"One"&lt;/span&gt;;
            }
        }
    }
    
    &lt;span class="keyword"&gt;private class&lt;/span&gt; &lt;span class="type"&gt;TypeTwo&lt;/span&gt; : &lt;span class="type"&gt;EnumType&lt;/span&gt; {
        &lt;span class="keyword"&gt;public override void&lt;/span&gt; DoSomething() {
            &lt;span class="type"&gt;Console&lt;/span&gt;.WriteLine(&lt;span class="string"&gt;"I am #2!"&lt;/span&gt;);
        }
        
        &lt;span class="keyword"&gt;public override string&lt;/span&gt; Name {
            &lt;span class="keyword"&gt;get&lt;/span&gt; {
                &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="string"&gt;"Two"&lt;/span&gt;;
            }
        }
    }
    
    &lt;span class="comment"&gt;// ...&lt;/span&gt;
}&lt;/pre&gt;
    &lt;p&gt; Now, each instance of EnumType is actually a singleton instance of its own class.&amp;nbsp;
        Now, your enums are much more powerful:&amp;nbsp; in addition to fields, each value
        in the enum can have its own behaviors in the form of concrete implementations of
        the parent’s abstract method.&amp;nbsp; There is still no way for another user to add
        values to the enum&amp;mdash;because EnumType’s constructor is private, it can’t be subclassed
        outside of the class itself.&lt;/p&gt;
    &lt;p&gt; One minor implementation detail here&amp;mdash;the only reason I chose to change the Name
        property from referencing a field to being abstract and implemented by each subclass
        was to show that it was an option.&amp;nbsp; It could just as easily have remained a
        field of EnumType, and TypeOne and TypeTwo would have called the base construtor
        with the appropriate strings instead of overriding the Name property.&amp;nbsp; I am
        not convinced that one way is better than the other; it’s just a stylistic preference.&lt;/p&gt;
    &lt;p&gt; We’re making progress away from the C-style need to treat everything as an int,
        but there’s still a lot of distance to cover.&amp;nbsp; Notably, we still can’t switch
        on the enum types (long, chained if-else blocks are so inelegant!), and&amp;nbsp;we've
        lost the support of methods from System.Enum like &lt;a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemenumclassgetvaluestopic.asp"&gt;
            GetValues&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemenumclassparsetopic.asp"&gt;
                Parse&lt;/a&gt;.&amp;nbsp; I’ll explore how to resolve these issues and try to think
        about how C# or a language like it could provide better support for this sort of
        enum in upcoming posts.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=504850" width="1" height="1"&gt;</description></item><item><title>Enumerated Types</title><link>http://blogs.msdn.com/nealho/archive/2005/12/15/504538.aspx</link><pubDate>Fri, 16 Dec 2005 05:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:504538</guid><dc:creator>nealho</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/nealho/comments/504538.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=504538</wfw:commentRss><description>&lt;P&gt;I had an interesting discussion earlier regarding enums and how they work in .NET, and since I haven't I blogged in a while, I thought I'd post some thoughts.&lt;/P&gt;
&lt;P&gt;Conceptually, an enumerated type is a type that has a set of values that can be (surprise surprise) enumerated.&amp;nbsp; This means, most significantly, that there is a finite number of possible values you can assign an enum.&amp;nbsp; That's a fairly abstract and, frankly, not very useful definition in the context of a programming language.&lt;/P&gt;
&lt;P&gt;Let's move on to an (admittedly impractical) example.&amp;nbsp; The classic example of enum usage&amp;nbsp;is as a primitive type identifier, such as:&lt;/P&gt;&lt;PRE style="FONT-SIZE: 14px"&gt;&lt;SPAN class=keyword&gt;public enum&lt;/SPAN&gt; &lt;SPAN class=type&gt;FruitKind&lt;/SPAN&gt; {
    Banana, Cherry, Orange
}&lt;/PRE&gt;
&lt;P&gt;Then, you write some methods that deal with these fruits:&lt;/P&gt;&lt;PRE style="FONT-SIZE: 14px"&gt;&lt;SPAN class=keyword&gt;public class&lt;/SPAN&gt; &lt;SPAN class=type&gt;FruitConsumer&lt;/SPAN&gt; {
    ...
    &lt;SPAN class=keyword&gt;public string&lt;/SPAN&gt; GetColor(&lt;SPAN class=type&gt;FruitKind&lt;/SPAN&gt; kind) {

    &lt;SPAN class=keyword&gt;switch&lt;/SPAN&gt; (kind) {
        &lt;SPAN class=keyword&gt;case&lt;/SPAN&gt; &lt;SPAN class=type&gt;FruitKind&lt;/SPAN&gt;.Banana:
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; &lt;SPAN class=string&gt;"Yellow"&lt;/SPAN&gt;;
        &lt;SPAN class=keyword&gt;case&lt;/SPAN&gt; &lt;SPAN class=type&gt;FruitKind&lt;/SPAN&gt;.Cherry:
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; &lt;SPAN class=string&gt;"Red"&lt;/SPAN&gt;;
        &lt;SPAN class=keyword&gt;case&lt;/SPAN&gt; &lt;SPAN class=type&gt;FruitKind&lt;/SPAN&gt;.Orange:
            &lt;SPAN class=keyword&gt;return&lt;/SPAN&gt; &lt;SPAN class=string&gt;"Orange"&lt;/SPAN&gt;;
        }
    }
}&lt;/PRE&gt;
&lt;P&gt;But wait, the compiler is complaining that this code doesn't compile.&amp;nbsp; "'FruitConsumer.GetColor(FruitKind)': not all code paths return a value," it tells me.&amp;nbsp; But I covered every possible value of the enum, right?&amp;nbsp; What other code path is there?&amp;nbsp; The answer, of course, lies with something that is intimately familiar to those of us with C/C++ background:&amp;nbsp; enums are really integers under the hood.&amp;nbsp; FruitKinds are really ints under the hood, wrapped in a pretty box with a little language support for using the named values.&lt;/P&gt;
&lt;P&gt;This dichotomy has some advantages.&amp;nbsp; For example, there are a lot of flag-type enums, where you can have any number of a series of values.&amp;nbsp; For example, the System.Reflection.BindingFlags type allows you to specify any number of binding options with the | (bitwise or) operator, and then the consumers of these can use &amp;amp; (bitwise and) to see if your flags fit.&amp;nbsp; This has good perf without overly obfuscating the meaning of those operations.&lt;/P&gt;
&lt;P&gt;Additionally, you gain the convenience is the ability to switch on enums.&amp;nbsp; One other "advantage" many people will cite is the ability to do regular arithmetic on enums, though I have yet to see a convincing example of this being a good idea.&lt;/P&gt;
&lt;P&gt;There are some downsides, too:&amp;nbsp;&amp;nbsp;in the FruitKind example, you weren't certain that the code could never be called with a value other than those you defined.&amp;nbsp; Mostly, I am just bothered by the "weirdness" factor, that your enums are not guaranteed to be one of the values you set forth.&amp;nbsp; The OO solution is to use an abstract base class as the enum type with a private constructor, and have nested classes with singleton instances as your enum values (good &lt;a href="http://blogs.msdn.com/jaybaz_ms/archive/2004/05/31/144967.aspx"&gt;post&lt;/A&gt; on the subject).&amp;nbsp; I think that's by far the best solution:&amp;nbsp; in return for arithmetic operations, you get methods; in return for switch/case, you can add a visitor hook.&amp;nbsp; Overall, it gives much greater flexibility (which, like most things, comes at the cost of some complexity).&lt;/P&gt;
&lt;P&gt;One final note, which is completely off this train of thought, but it is the last thing that bugs me about enums in C#.&amp;nbsp; The literal zero is always &lt;EM&gt;implicitly&lt;/EM&gt; convertible to any enum type.&amp;nbsp; Any integral value can be converted to any enum type &lt;EM&gt;explicitly&lt;/EM&gt;, but only zero can be converted implicitly.&amp;nbsp; The reasoning, as I understand it,&amp;nbsp;has to do with the fact that the memory that your enum instances are occupying is going to start out as zero anyway, so saying "FruitKind kind = 0" is the same as saying "FruitKind kind = new FruitKind()"; this is logical enough from a computer organization point of view.&amp;nbsp; At a high level, though,&amp;nbsp;it doesn't mean I have to like it. ;)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=504538" width="1" height="1"&gt;</description></item><item><title>Breaking Changes</title><link>http://blogs.msdn.com/nealho/archive/2005/11/22/496101.aspx</link><pubDate>Wed, 23 Nov 2005 06:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:496101</guid><dc:creator>nealho</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/nealho/comments/496101.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=496101</wfw:commentRss><description>&lt;P&gt;Something we in the C# Compiler team have been discussing recently is the issue of including "breaking changes" in our code.&amp;nbsp; This is a big deal, especially for a compiler, which people expect to be pretty rock-solid.&amp;nbsp; There are a lot of issues here, most of which remain unresolved.&lt;/P&gt;
&lt;P&gt;The first issue is the question of what, exactly, a breaking change &lt;EM&gt;is&lt;/EM&gt;.&amp;nbsp; One could probably make the case that any code change, no matter how minor, could be a "breaking" change because someone could be relying on that particular functionality.&amp;nbsp; Thus, we're required to make a few assumptions about our users.&amp;nbsp;&amp;nbsp;A good starting point is to assume that no one relies on crashes of any kind.&amp;nbsp; Any time we see "Internal Compiler Error" we know that fixing the issue is the right thing to do—a code change that causes the compiler not to crash where it crashed before is not a breaking change.&amp;nbsp; All right, that one's a no-brainer, and while that's an important class of errors, it hardly covers the majority of issues users might encounter, so let's break down code changes further into two major categories, excluding crashing issues:&amp;nbsp; there is code that did not compile under the previous version, and code that did compile under the previous version.&lt;/P&gt;
&lt;P&gt;If the code didn't compile in the first place, there are two changes we can make.&amp;nbsp; One is to change the error messages that are printed out, probably with the intention of making them clearer or more relevant to the problem at hand.&amp;nbsp; It seems reasonable to assume that people wouldn't rely on code not compiling in a certain way—the code didn't compile before, and it still doesn't compile.&amp;nbsp; This class of errors does not contain breaking changes.&amp;nbsp; The other possibility is that the code that didn't compile is now accepted.&amp;nbsp; This also isn't likely to break anyone.&amp;nbsp; If we add a new feature that reasonably accepts code that would have been illegal before, people aren't likely to complain that we are accepting programs they were expecting to fail (I can't imagine hearing a complaint that class Foo&amp;lt;T&amp;gt; { ... } is accepted by the Whidbey compiler, since it's not valid code in Everett!).&amp;nbsp; We also don't consider these breaking changes.&lt;/P&gt;
&lt;P&gt;All right, so we've determined that it's okay to change things about code that doesn't compile anyway.&amp;nbsp; I'll take the category of code that did compile and further divide it into three subcategories:&amp;nbsp; first, changes in warnings generated; second, changes in actual code generated; and third, changes that raise new errors.&amp;nbsp; Changing the warnings is generally considered acceptable, and is often a prelude to more serious changes.&amp;nbsp; For example, it is common practice to add a warning on a newly-deprecated construct.&amp;nbsp; These are not breaking changes.&lt;/P&gt;
&lt;P&gt;The next two categories are a bit more serious, and the astute reader will probably think that this is the interesting part.&amp;nbsp; First, if the generated code is different, that is likely to be a problem for some people who depended on an old behavior.&amp;nbsp; For example, in Whidbey, we fixed an issue with enum types and integer math, where an enum whose underlying type was byte could be subtracted from another enum of the same type, assigned to an int,&amp;nbsp;and the result would be -1.&amp;nbsp; Now, if you were to do the same math with the underlying byte type, you would end up with 255, because the&amp;nbsp;byte arithmetic would overflow (byte is unsigned, and operations are unchecked by default).&amp;nbsp; We're giving the right answer here, but it's still a breaking change because we had real customers who relied on the old behavior, even though it was incorrect.&lt;/P&gt;
&lt;P&gt;Finally, let's look at the case where code that was accepted simply fails on a new version.&amp;nbsp; These are the clearest breaking changes; anyone who had the (previously valid) code would be broken by this sort of change.&amp;nbsp; Take, for example, our handling of [Conditional(DEBUG)] attribute.&amp;nbsp; In Everett, we allowed methods marked with [Conditional(DEBUG)] to have out parameters, but in Whidbey, it was disallowed because it cannot be guaranteed that the parameter will be assigned in a release build.&amp;nbsp; However, this is also a real customer scenario where we broke someone who had [Conditional(DEBUG)] methods that had out parameters.&amp;nbsp; What seemed to be a sensible design revision caused some customers a lot of trouble.&lt;/P&gt;
&lt;P&gt;All this thought has been part of a long discussion among some of us on the C# team on how, exactly, to deal with breaking changes.&amp;nbsp; This is a long process, starting&amp;nbsp;with devs considering if they might be adding breaking changes,&amp;nbsp;going through QA noticing breaking changes involved in bug fixes, and finally moving on to PMs and other customer-focused team members educating users about what changes have been made.&amp;nbsp; It's no single person's responsibility, which means that it has not always gotten as much attention as it should.&lt;/P&gt;
&lt;P&gt;What's the right solution?&amp;nbsp; I'll let you know when we come up with it, but for now, we'll be encouraging devs to think about users they may be breaking when they make checkins, asking QA to consider each bug they sign off on carefully, and getting everyone to keep their PMs informed about these changes as soon as they are made and discovered.&amp;nbsp; That way, we'll have an easier time knowing and documenting what changes exactly may adversely affect our users between versions, and help them transition more smoothly from each version to the next.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=496101" width="1" height="1"&gt;</description></item><item><title>Expressions vs. Statements</title><link>http://blogs.msdn.com/nealho/archive/2005/11/07/490211.aspx</link><pubDate>Tue, 08 Nov 2005 06:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:490211</guid><dc:creator>nealho</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/nealho/comments/490211.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=490211</wfw:commentRss><description>&lt;P&gt;This morning, I was discussing issues around my previous post with Mads Torgersen, the PM who owns the C# compiler, and it reminded me of the next issue I wanted to talk about.&amp;nbsp; Something I'm pushing for in the Orcas timeframe is to add not only the Func&amp;lt;&amp;gt; types, but Proc&amp;lt;&amp;gt; types as well, which specify delegates that return void.&amp;nbsp; This and related types complete the set of standard delegate types for various APIs.&amp;nbsp; However, the very need for these extra times brings me to another issue with C#, which is a holdover from being a C-like language (and so, of course, this is a problem in C++ and Java as well).&amp;nbsp; That is the difference between a statement and an expression.&lt;/P&gt;
&lt;P&gt;Now, this difference is probably a lot more intuitive for most programmers than the ones I was discussing last time, because nearly everyone who's ever done any kind of programming has done at least some coding in C, C++, Java, and C#.&amp;nbsp; All of these languages have these two constructs in their grammars, and the difference is woven into the very fabric of these languages.&amp;nbsp; We are taught to think of programs conceptually as a series of statements, where each statement modifies the state of the program's state&amp;nbsp;in some way before the next statement executes (I'll limit the discussion to single-threaded applications for simplicity).&amp;nbsp; The reasoning for this is clear—this is what happens when our programs run.&amp;nbsp; The (classical) processor executes an instruction, which changes some register or writes some value to memory, then executes the next instruction.&amp;nbsp; The C language, which is essentially portable assembly, should obviously be a series of statements.&lt;/P&gt;
&lt;P&gt;Unfortunately, even in C, we want to be able to use higher-level constructs than can be encoded in individual or even a small group of machine instructions.&amp;nbsp; This leads to the concept of an expression, which can be a part of a statement.&amp;nbsp; Now, subroutines can can "return" a value to be used by the caller instead of simply modifying explicitly declared state.&amp;nbsp; Obviously, this is a powerful concept, but it leads to an unfortunate inconsistency in the design of the language.&amp;nbsp; For exaple, compare (condition ?&amp;nbsp;trueCase : falseCase) with (if (condition) {&amp;nbsp;trueCase;&amp;nbsp;} else {&amp;nbsp;falseCase; }).&amp;nbsp; What is the difference here?&amp;nbsp; The expression "condition" is evaluated in both cases, exactly the same way.&amp;nbsp; If trueCase and falseCase are expressions, then there is no difference at all!&amp;nbsp; If condition is true, then trueCase is executed, else falseCase is executed.&amp;nbsp; Why are there two language constructs to express exactly the same thing?&amp;nbsp; I ask rhetorically, of course, because if-else expects "statements" while the ternary conditional operator expects "expressions."&amp;nbsp; Honestly, though,&amp;nbsp;it seems&amp;nbsp;silly that&amp;nbsp;? : can't contain blocks, and that if-else&amp;nbsp;can't return a value.&lt;/P&gt;
&lt;P&gt;Still, this leaves a problem.&amp;nbsp; Conceptually, a statement is simply an expression that returns no value.&amp;nbsp; Okay, but if every statement becomes an expression, how do you differentiate between a method with a return value and a method that returns void?&amp;nbsp; Even in this pretty, expression-based world, we may still want a method that simply modifies state and doesn't need to return a value, so why not make void an actual part of the type system?&amp;nbsp; A method can still return void, and you can still call it and ignore its return value.&amp;nbsp; Moreover, we wouldn't need separate Proc types;&amp;nbsp;one could simply say Func&amp;lt;T, void&amp;gt; to get that Proc&amp;lt;T&amp;gt; (or Action&amp;lt;T&amp;gt;, if we're looking at Whidbey APIs).&amp;nbsp; Of course, this would make some silly things possible, like declaring a variable of type void, or having a List&amp;lt;void&amp;gt;, but I can't imagine why anyone would want to do that. :)&lt;/P&gt;
&lt;P&gt;The very syntax of C#, Java, and other C-like languages makes this difficult, since statements are baked into the grammar, so we're stuck with void being its own special non-type.&amp;nbsp; So if you find yourself annoyed at the inconsistency of statements and expressions, take a break and write some code in Scheme or OCaml.&amp;nbsp; The exercise in functional thinking will do you good, and imho, it'll make you write better code in C#.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=490211" width="1" height="1"&gt;</description></item><item><title>Func&lt;&gt; and delegate types</title><link>http://blogs.msdn.com/nealho/archive/2005/11/03/488950.aspx</link><pubDate>Fri, 04 Nov 2005 07:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488950</guid><dc:creator>nealho</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/nealho/comments/488950.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nealho/commentrss.aspx?PostID=488950</wfw:commentRss><description>&lt;P&gt;I'm a bit new both to the C# team and to blogging, so bear with me as I muddle through my first post about something that interests me. :)&lt;BR&gt;&lt;BR&gt;Let me start at the beginning.&amp;nbsp; Since C# 1.0, one of the things that I liked about it (compared to Java, since I'm an OO nerd) was the fact that it had a way to pass functions from one place to another.&amp;nbsp; However, delegate types hardly provided the ease of code passing that my functional background would like, and with Java, anonymous inner classes at least provided a simple way of writing a bit of code in a method, including closing over some of the local state, and passing that to another method.&amp;nbsp; Of course, the downside was that I always kept an IFunction interface around, which specified a single method from object to object (and, once I started using the Sun's JSR-14 comiler, from T to U) that I could instantiate as needed for my code passing requirements.&lt;BR&gt;&lt;BR&gt;C# 2.0's anonymous methods made that IFunction type look clunky with all of Java's extra syntax required to create an anonymous inner class.&amp;nbsp; As with the IFunction type in Java, however, I needed a delegate type for the signature of any method that needed to accept code as an argument.&amp;nbsp; No big deal, just requires writing a line declaring that delegate type above the function, and thanks to implicit conversion from anonymous methods to an appropriate delegate type, I never needed to think about that type name again, unless it came up in error messages (usually due to the limitations of the type inference that goes on when generic delegate types are combined with polymorphic methods--but this is an issue for another blog entry).&amp;nbsp; The convenience of not having to write a new method every time I wanted to create a delegate combined with the intuitive power of closures (well, almost closures--but this is a &lt;a href="http://blogs.msdn.com/brada/archive/2004/08/03/207164.aspx"&gt;well-covered issue&lt;/A&gt; that I don't want to reiterate here) made me that much more productive when coding.&lt;BR&gt;&lt;BR&gt;Now to the main event.&amp;nbsp; Since the PDC, I've heard a lot of buzz surrounding all of the various "function" delegate types that are included in the LINQ preview.&amp;nbsp; I'm sure those of you with any&amp;nbsp;experience&amp;nbsp;with functional programming are breathing sighs of relief that such obvious types are finally being included in the .NET framework--I know I am!&amp;nbsp; Still, along with this relief is a healthy dose of caution.&amp;nbsp; I find myself wary of how these types will integrate into the existing framework, with all of its predefined delegate types.&amp;nbsp; For example, suppose I write a method using the LINQ framework that accepts Func&amp;lt;int, bool&amp;gt;, and&amp;nbsp;am interested in using this function to filter a List&amp;lt;int&amp;gt; that I already have.&amp;nbsp; Looking down the IntelliSense-provided options suggests that the FindAll method will perform the action that I want.&amp;nbsp; So I call FindAll on my list and pass it my Func&amp;lt;int, bool&amp;gt;.&amp;nbsp; But what's this?&amp;nbsp; The compiler is telling me that Func&amp;lt;int, bool&amp;gt; cannot be converted to Predicate&amp;lt;int&amp;gt;!&amp;nbsp; That's ridiculous: &amp;nbsp;I &lt;B&gt;have&lt;/B&gt; a predicate, at least conceptually.&amp;nbsp; Really, what's the difference between a Predicate&amp;lt;int&amp;gt; and a Func&amp;lt;int, bool&amp;gt;?&amp;nbsp; Both represent a function that takes an int and returns a bool, so why aren't they compatible?&lt;BR&gt;&lt;BR&gt;I had mistakenly convinced myself that methods really were just as powerful in C# as they are in Lisp.&amp;nbsp; All the ease of using delegates that came with anonymous methods and implicit conversions to delegate types had blinded me to their original flaw--that despite their ease of use, &lt;I&gt;methods&lt;/I&gt; are still not first-class entities.&amp;nbsp; Because the language doesn't consider a method an actual type, despite the fact that the same method could be converted to either a Func&amp;lt;int, bool&amp;gt; or a Predicate&amp;lt;int&amp;gt;, you can't simply convert one to the other.&amp;nbsp; Of course, thanks to anonymous methods, and more recently, lambda expressions, this isn't hard to work around.&amp;nbsp; Given a Func&amp;lt;int, bool&amp;gt; func, you can create a Predicate&amp;lt;int&amp;gt; simply by saying arg =&amp;gt; func(arg).&amp;nbsp; But having to write the identity function still feels awkward, and reduces the functional&amp;nbsp;appeal of your program.&lt;BR&gt;&lt;BR&gt;Where am I going with all this?&amp;nbsp; Well, even without fully first-class method support, it would have been nice if the Func&amp;lt;&amp;gt; types had been included in the framework since v1.0.&amp;nbsp; Of course, this wasn't possible before 2.0 (hooray for generics!), and I guess no one thought they'd be important enough to add for Whidbey.&amp;nbsp; Maybe they're right on that point; it does seem like very few C# programmers use delegates in their APIs.&lt;/P&gt;
&lt;P&gt;I suppose the real bottom line is that I&amp;nbsp;need to bear in mind the distinction between methods, which are the actual code that take and return arguments, and delegates, which merely encapsulate a method (or several methods--but once again, this is not an issue for this post).&amp;nbsp; This distinction is subtle, but anyone who programs in C# should understand it.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488950" width="1" height="1"&gt;</description></item></channel></rss>