Are you jealously looking over the fence to Java Enums? Enums in Java are much more than the barely disguised integer constants in C#. In Java the Enum is a special form of a class with a set of fixed instances always inheriting the Enum baseclass They can have local variables and methods.

In C# (or VB so far) enums are much more like strongly typed and named integer subsets in the tradition of C #define. It is possible to give a different base but only different sizes of integer. No way to put any logic onto an enum. No way?


Extension methods to the rescue. I often have Enums coming from a WSDL proxy. As I don’t want the dependency to the webservice to leak into the domain model I convert from and to the internal version. Conversion could be done by a service but I prefer Extension Methods.

Assume the following definition coming from the webservice:

public enum ExternalEnum

As I don’t care for the specific reason the service is unavailable I define:

public enum InternalEnum

With this extension method defined:

public static class EnumExtensions
    public static InternalEnum ToInternalEnum(this ExternalEnum value)
        switch (value)
            case ExternalEnum.Ready:
                return InternalEnum.DatabaseReady;
            case ExternalEnum.HardwareUpdate:
            case ExternalEnum.SoftwareUpdate:
                return InternalEnum.Maintenance;
            case ExternalEnum.TimeOut:
            case ExternalEnum.Error:
                return InternalEnum.ServerError;
                throw new InvalidCastException("Unknown value for ExternalEnum value " + value);

I can convert (and get even Intellisense):

ExternalEnum wsResult = ...;
return wsResult.ToInternalEnum();