Why can't switch be used for objects



Why can't switch used for objects, at least for static readonly fields it
wold be nice.
The Jit could certainly optimize this case because the addresses of static
readonly variables are known at jit time.

The thing is that is often used my own enum classes because I usually want
to assign a userfriendly (and localizable) name to an enum member which is
automatically displayed if I add a enum member to a combobox for example.
Additionally I have helper methods for returning me a list of all or a
specific subsets of the member of the enums:

public class Color
{
public static reaonly Color Red = new Color(255,0,0);
public static reaonly Color Green = new Color(0,255,0);
// and so on

private Color(){}

public Color[] GetColors() ..
public Color[] GetGreenLikeColors() ..
public Color[] GetDarkColors() ..
public Color[] GetSystemColors() ..

Color GetByName(string name){}
Color GetByID(string name){}
}

The getXX methods work mainly using hashtables and the great thing is that
if using generics I could derive all enums from some base enum class without
having to add new getXX methods which return Color[] instead of object[].

The stupid thing is if I change some enum into my enum class I always have
to rewrite all switches into huge ugly if's.

I see no technical reason why switch shouldn' be allowed for objects.
Certainly is won't be as fast as with constant int values but anyway they
allowed strings in switch statements.

In a modern language like C# the purpose of a switch statement shouldn't be
to make a program faster but more readable.

Am I just plain stupid or is there something huge I overlooked?


.



Relevant Pages

  • Re: Reflection and variable selection? Late binding?
    ... If your variables are all integers (or any of the supported base Types for Enums) then use an Enum as in my second example. ... If your variables are other Types such as strings or complex Types then you should use the switch statement. ... when each case is doing something very similar: going from string "x" to ...
    (microsoft.public.dotnet.framework)
  • Re: Long switch statements (and Swing application structural design)
    ... I can't help feeling that I ought to be able to find a way to eliminate the switch statement. ... I don't know off-hand whether you can make an `enum' abstract, ... override it with a concrete method in each constant." ... Any time you find yourself switching or iffing on a type in order to determine which of several versions of the same method you need, you have subverted polymorphism. ...
    (comp.lang.java.programmer)
  • Re: Long switch statements (and Swing application structural design)
    ... case Country: ... case Currency: ... I can't help feeling that I ought to be able to find a way to eliminate the switch statement. ... I don't know off-hand whether you can make an `enum' abstract, ...
    (comp.lang.java.programmer)
  • Re: Using enums to avoid using switch/if
    ... My point was that in the given example, replacing if ladders with switch would buy little in terms of bloat, and that taking advantage of enums is more likely to. ... Looking at run-time, switch statements typically use much faster low-level instructions than a series of if statements, with less impact on branch prediction. ... Each enum has an execute method that returns an enum for the "next" state. ...
    (comp.lang.java.programmer)
  • Re: Magic Numbers dangerous?
    ... The crucial problem with type-safe enum kludges is getting a decently ... The Java enum implementors went to considerable ... shenanigans to implement the switch with a plain old jump table using ... // use an ordinary int switch, using pre-mapped ordinals to sort the ...
    (comp.lang.java.programmer)