public enum Foo { Bar, Baz }; // Has to throw the exception public void doSomething(Foo f) { switch(f) { case Bar: System.out.println("!! Bar"); break; case Baz: System.out.println("Baz !!"); break; default: throw new IllegalArgumentException("Unsupported enum type!"); } }
In Java, you can give the enum behaviour.
public enum Foo { Bar("!! Bar"), Baz("Baz !!); private string stringRepresentation; private Foo(string s) { stringRepresentation = s; } public string getString() { return stringRepresentation; } } public void doSomething(Foo f) { return f.getString(); }
This is cool because the only way you can add a new enum value and get it to compile is to give it a valid string representation.
In C#, enum's can't have behaviour so I think they should be avoided like the plague. Changes are if you are switching on an enum value then the behaviour you are executing belongs on the class anyway!
You can simulate enums with a class, a private constructor and some static instances of the class representating the enum values.
public sealed class Foo { public static readonly Bar = new Foo("!! Bar"); public static reaodnly Baz = new Foo("Baz !!"); private string m_StringRepresentation; private Foo(string stringRepresentation) { m_StringRepresentation = stringRepresentation; } public string StringRepresentation { get { return m_StringRepresentation; } } }