Click here to Skip to main content

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy.
The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct and please don't post ads.

Technical discussions are welcome, but if you need specific help please use the programming forums.


 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberjschell21-Mar-13 10:04 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0021-Mar-13 10:27 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberCésar de Souza22-Mar-13 0:43 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 10:58 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberenglebart22-Mar-13 2:26 
GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 10:41 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberRafagaX22-Mar-13 6:40 
GeneralRe: Does this language feature seem strange to anyone else? PinmemberChadwick Posey22-Mar-13 7:48 
I always thought this part of C# was kind of hokey, but after reporting it to Microsoft as a bug (derp), and it getting escalated to the framework team, it was revealed, it is part of the spec:
 
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf[^] (The C# Specification), page 136 states:
 
"An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type."
 
So the short answer is, it's in the spec.
 
The long answer is (from Habib, here: http://stackoverflow.com/questions/14950750/why-switch-for-enum-accepts-implicit-conversion-to-0-but-no-for-any-other-intege[^])
 
At compile time 0 is known as the default value for an enum. No other value is explicitly known, and that is why no other value is allowed to be substituted.
 
In fact, you can try some sample code out for yourself:
 
    class Program
    {
        enum Direction { left = 1, right = 2 }
 
        static void Main(string[] args)
        {
            Direction d = 0;
 
            Console.WriteLine("Direction: " + Enum.GetName(typeof(Direction),d));
 
            Console.ReadLine();
        }
    }
 
Note that even though we've explicitly excluded 0 from our example, the code still compiles and works happily returning "" for the label corresponding to 0. It's always going to be there, and that's why 0 is allowed an implicit cast.
=============================
I'm a developer, he's a developer, she's a developer, Wouldn't ya like to be a developer too?

GeneralRe: Does this language feature seem strange to anyone else? Pinmemberlewax0022-Mar-13 10:45 
GeneralReading Code PinmemberKyudos20-Mar-13 11:22 
GeneralRe: Reading Code [modified] Pinmemberwizardzz20-Mar-13 11:29 
GeneralRe: Reading Code PinprotectorAspDotNetDev20-Mar-13 11:40 
JokeRe: Reading Code PinmemberKyudos20-Mar-13 11:47 
GeneralRe: Reading Code Pinmemberlewax0020-Mar-13 11:42 
GeneralRe: Reading Code PinmemberCDP180220-Mar-13 11:39 
GeneralRe: Reading Code Pinmemberdusty_dex20-Mar-13 11:45 
JokeRe: Reading Code PinmemberKyudos20-Mar-13 11:48 
GeneralRe: Reading Code PinmemberPIEBALDconsult20-Mar-13 12:50 
GeneralRe: Reading Code PinmemberRob Grainger22-Mar-13 3:00 
GeneralRe: Reading Code PinmvpEddy Vluggen20-Mar-13 12:45 
GeneralRe: Reading Code Pinmember_Josh_20-Mar-13 13:31 
GeneralRe: Reading Code PinmemberPIEBALDconsult20-Mar-13 13:35 
GeneralRe: Reading Code PinmemberKyudos20-Mar-13 17:24 
JokeRe: Reading Code PinmemberVivic20-Mar-13 17:44 
GeneralRe: Reading Code PinmemberNagy Vilmos20-Mar-13 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 23 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid