<?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>More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx</link><description>The well known (or moderately known fact): C# enums can contain any value supported by its base type and not just the ones specified in the enum . Lets consider the following enum. enum WeekDay { Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#1438695</link><pubDate>Tue, 09 Jan 2007 12:56:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1438695</guid><dc:creator>zproxy</dc:creator><description>&lt;p&gt;An enum value of 0 should be always reserved for None, Empty, Default or alike, thus assigning 0 allows to initialize the enum. It should have the same effect as default(MyEnumType).&lt;/p&gt;
&lt;p&gt;&amp;quot;Do name the zero value of flags enumerations None. For a flags enumeration, the value must always mean all flags are cleared.&lt;/p&gt;
&lt;p&gt;Note&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://msdn2.microsoft.com/en-us/library/ms229062.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms229062.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#1438706</link><pubDate>Tue, 09 Jan 2007 13:01:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1438706</guid><dc:creator>abhinaba</dc:creator><description>&lt;p&gt;You have pointed to Flags enumeration where it makes perfect sense to have a zero.&lt;/p&gt;
&lt;p&gt;But in our case this is not a bit flag and there is no default or empty weekday, nor can you have monday and tuesday at the same time (with an OR).&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#1438883</link><pubDate>Tue, 09 Jan 2007 14:21:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1438883</guid><dc:creator>Sam Judson</dc:creator><description>&lt;p&gt;Enumerations are a CLR value type, stored in the method stack as an Int32, which defaults to 0 anyway - the following two code segments have exactly the same end result:&lt;/p&gt;
&lt;p&gt;WeekDay day;&lt;/p&gt;
&lt;p&gt;WeekDay day = 0;&lt;/p&gt;
&lt;p&gt;The C# compiler will however consider the first variable 'unitialised'.&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#1439220</link><pubDate>Tue, 09 Jan 2007 16:36:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1439220</guid><dc:creator>Peter Ritchie</dc:creator><description>&lt;p&gt;I think it's less to do with the guideline of always defining a enum member with the value 0 than to do with the fact that an enum will always initialize to 0 before the first assignment. &amp;nbsp;There's really no point in throwing an error for assigning a value (0) to a enum type when that same value was used to initialize it, without error.&lt;/p&gt;
&lt;p&gt;An in fact, in your example &amp;quot;Weekday day = 0;&amp;quot;, the assignment of zero (0) is redundant; day will have been initialized with 0 already. &amp;nbsp;It could be that this is &amp;quot;supported&amp;quot; because it is optimized out (I'm not sure).&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#1480329</link><pubDate>Wed, 17 Jan 2007 01:33:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1480329</guid><dc:creator>Mark Steward</dc:creator><description>&lt;p&gt;I'm pretty sure it would have to be because (as you mentioned) Enums are used for flags as well as for lists.&lt;/p&gt;
&lt;p&gt;But of course, a lot of enums come from protocol definitions, so having to mentally add a zero case to every enum would be onerous, for very little extra protection (since enums are initialised to 0, it's not risky like having a zero pointer).&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#1501212</link><pubDate>Sun, 21 Jan 2007 07:15:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1501212</guid><dc:creator>Shahar</dc:creator><description>&lt;p&gt;Peter Ritchie:&lt;/p&gt;
&lt;p&gt;C# requires that you initialize any and all local variables.&lt;/p&gt;
&lt;p&gt;You are confusing this with member variables that do not have to be initialized.&lt;/p&gt;
&lt;p&gt;(This is a C# thing, not a CLR/MSIL thing - the CLR, as somebody mentioned, will automatically initialize it if the code does not do that..)&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#6488303</link><pubDate>Fri, 23 Nov 2007 21:17:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6488303</guid><dc:creator>Michael Buen</dc:creator><description>&lt;p&gt;can you request to Visual Studio 2008 team, to straight out C# 3.0 treatment of enum? &amp;nbsp;because it is very confusing.&lt;/p&gt;
&lt;p&gt;it is not clean to have 0 moonlighting as enum.&lt;/p&gt;
&lt;p&gt;an example:&lt;/p&gt;
&lt;p&gt;enum PgType { Varchar, Date, Integer };&lt;/p&gt;
&lt;p&gt;static void Main()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;AddField(&amp;quot;Name&amp;quot;, &amp;quot;Michael Buen&amp;quot;);&lt;/p&gt;
&lt;p&gt;AddField(&amp;quot;JobToSeek&amp;quot;, PgType.Varchar);&lt;/p&gt;
&lt;p&gt;AddField(&amp;quot;Age&amp;quot;, 20);&lt;/p&gt;
&lt;p&gt;AddField(&amp;quot;WorkExperience&amp;quot;, 0);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;static void AddField(string name, PgType t)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;Console.WriteLine(name + &amp;quot;: just add field&amp;quot;);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;static void AddField(string name, object value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;Console.WriteLine(name + &amp;quot;: add field and assign value at the same time, also get the type&amp;quot;);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;output is:&lt;/p&gt;
&lt;p&gt;Name: add field and assign value at the same time, also get the type&lt;/p&gt;
&lt;p&gt;JobToSeek: just add field&lt;/p&gt;
&lt;p&gt;Age: add field and assign value at the same time, also get the type&lt;/p&gt;
&lt;p&gt;WorkExperience: just add field&lt;/p&gt;
&lt;p&gt;Note: the WorkExperience should be: add field and assign a value at the same time.&lt;/p&gt;
&lt;p&gt;to illustrate again the problem:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://plus.kaist.ac.kr/~shoh/postgresql/Npgsql/apidocs/Npgsql.NpgsqlParameterCollection.Add_overload_3.html"&gt;http://plus.kaist.ac.kr/~shoh/postgresql/Npgsql/apidocs/Npgsql.NpgsqlParameterCollection.Add_overload_3.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://cleveralias.blogs.com/thought_spearmints/2004/01/more_c_enum_wac.html"&gt;http://cleveralias.blogs.com/thought_spearmints/2004/01/more_c_enum_wac.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;eric gunnerson's reasoning:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if ((myVar &amp;amp; MyEnumName.ColorRed) != (MyEnumName) 0)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;which we thought was difficult to read. One alernative was to define a zero entry:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if ((myVar &amp;amp; MyEnumName.ColorRed) != MyEnumName.NoBitsSet)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;which was also ugly.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;We therefore decided to relax our rules a bit, and permit an implicit conversion from the literal zero to any enum type, which allows you to write:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if ((myVar &amp;amp; MyEnumName.ColorRed) != 0)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;which is why PlayingCard(0, 0) works. &lt;/p&gt;
&lt;p&gt;that relaxing rule was favored because of bitwise elegance, but the C# team didn't see the drastic effect on programmer's strong faith on C#'s strong-typing. &amp;nbsp;0 should resolve to object if there is no integer overload function. &amp;nbsp;0 should never become an enum type in anyways, just like the value 20 should resolve to object, not enum. &amp;nbsp;i hope they will fix this in C# 3.0. &amp;nbsp;C# enum zero looks like a gotcha, because it always needs an explanation&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#6710254</link><pubDate>Sun, 09 Dec 2007 07:38:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6710254</guid><dc:creator>Michael Buen</dc:creator><description>&lt;p&gt;they should have make @0 to indicate enum zero. &amp;nbsp; 0 shouldn't be implicitly cast to in any ways, just like any number.&lt;/p&gt;
&lt;p&gt;VS team should have make this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if ((myVar &amp;amp; MyEnumName.ColorRed) != 0)&lt;/p&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if ((myVar &amp;amp; MyEnumName.ColorRed) != @0)&lt;/p&gt;
&lt;p&gt;so there will be no more implicitly casted zero to enum gotcha :-)&lt;/p&gt;
</description></item><item><title>re: More peculiarites of enum</title><link>http://blogs.msdn.com/abhinaba/archive/2007/01/09/more-peculiarites-of-enum.aspx#6710266</link><pubDate>Sun, 09 Dec 2007 07:41:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6710266</guid><dc:creator>Michael Buen</dc:creator><description>&lt;p&gt;they should have make @0 to indicate enum zero. &amp;nbsp; 0 shouldn't be implicitly cast to in any ways, just like any number.&lt;/p&gt;
&lt;p&gt;VS team should have make this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if ((myVar &amp;amp; MyEnumName.ColorRed) != 0)&lt;/p&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if ((myVar &amp;amp; MyEnumName.ColorRed) != @0)&lt;/p&gt;
&lt;p&gt;so there will be no more implicitly casted zero to enum gotcha :-)&lt;/p&gt;
</description></item></channel></rss>