Opinion: Why Java Enums are better than those in C#


News: Opinion: Why Java Enums are better than those in C#

  1. Chris Stevenson has discussed the differences between the enum constructs in Java and C#. What doesn't he like about the C# way? "C# and .NET will let you create a “strongly typed” enum with an underlying int value of 666, or anything you like."

    Chris Stevenson's on More reasons never to use C# enums

    Ara Abrahamian also chimes in, and relates it all to the Memento movie (which is great by the way :) )

    Read Ara's thoughts in Memento

    Threaded Messages (4)

  2. Note: Some early manuscripts read "616" ...
  3. Before indulging into hate[ Go to top ]

    Maybe one should consider why they did it. Unfortunately it's in a human nature of many to find negative explanation more believable (they are stupid, they are evil) than positive one (they DO have really smart guys working on it, there must be some reason).

    While I don't have insider insights, one explanation of this can be versioning. If managed environment is to become core kernel API (as in WinFX), it's natural to assume that they want everything more loose and versionable. As in this blog:
  4. Before indulging into hate[ Go to top ]

    Someone from MS does explain why (see comments on the same page of the article)...

    "There are two reasons we do this:

    The first is that, as another poster noted, enums function both as enums and bit sets. One could conceive of a language where they were two discrete items, though it would be less C-like (one of our targets), and would require a bit more syntax. And also, runtime support, and support in all the other .NET languages, so we elected not to go there.

    We did elect to change the C behavior of enums being the same as ints - they are separate types, and while you can convert between an int and an enum with an explicit cast, the use of an explicit cast is generally a "I know what I'm doing here" indication.

    The second issue is around the values you can put into an enum. We don't validate that an enum value is equal to one of the predefined values, both because they are used for bit-sets, and because it would impose a performance penalty. You can easily add the check yourself, but it is somewhat unfortunate that you have to do so.

    Every language design has its tradeoffs.

    Posted by: Eric Gunnerson on January 23, 2004 12:28 AM "
  5. The movie, hehe[ Go to top ]

    Doh! It's the first time a movie title is also included in a TSS thread!

    Earlier today we interviewed James Gosling and he said he's sure Sammy Jenkins, one of the evil characters of the movie, is a Microsoft agent. He said Microsoft tries to manipulate people's memory to forget all those DLL hell and Outlook viruses, and tries to tattoo the false fact '.Net is better than Java' on developers' minds :-) Hehe