0

So I am working with Enumerations for my first time in C# but I don't think it'll last as I just realised the lack of flexibility you have with them as you do in Java.

For example, the equivalent of what I want to do in Java would look something like this:

public enum Days {
    Mon("Monday"), Tue("Tuesday"), Wed("Wednesday"), Thu("Thursday", 93), Fri, Sat("Saturday", 44), Sun;

    private String longName;
    private int other;

    Days() {}
    Days(String longName) {
        this.longName = longName;
    }
    Day(String longName, int somethingElse) {
        this(longName);
        this.other = somethingElse;
    }

    @Override
    public String toString() {
        return this.longName + ", " + this.other;
    }
}

Is there anyway I can do this in C#? As far as I can tell, with enums you can only do something like the following:

public enum Days { Mon=1, Tue=2, Wed=3 }

It doesn't have to strictly be an enum, just act like the Java enum.

Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • You can use custom attributes. – crashmstr Oct 14 '15 at 15:28
  • So you want the ability to give each enum value a long name? – Dave Zych Oct 14 '15 at 15:28
  • 1
    Java's and C#'s enumerations look similar, but they are quite different: Java's enumerations are essentially classes, where each enum constant is an instance. C's enumerations, in contrast, are constants of an ordinal type such as `int`. – O. R. Mapper Oct 14 '15 at 15:32
  • 1
    Unlike Java, C# enums, like C and C++ only contain constants. You can create a class that acts like an extension of days and call your functions through that. You can convert an enum's name and/or value to string and a string to an enum's name and/or value, if that is all you are looking for – Ross Bush Oct 14 '15 at 15:33
  • You could create a [TypeConverter](https://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter(v=vs.110).aspx) class and use that for your formatting purposes. – test Oct 14 '15 at 15:38

2 Answers2

2

Well you can add a display name to an enum:

public enum Days {
    [Display(Name = "Monday")]
    Mon=1, 

    [Display(Name = "Tuesday")]
    Tue=2, 

    [Display(Name = "Wednesday")]
    Wed=3 
}

but it is just metadata. You can't add implementation to them like you (apparently) can in Java.

You could use a class with static read-only properties:

public class Days {

    private String longName;
    private int other;

    Days() {}
    Days(String longName) 
    {
        this.longName = longName;
    }
    Days(String longName, int somethingElse) 
        : this(longName) 
    {
        this.other = somethingElse;
    }

    public override String ToString() 
    {
        return this.longName + ", " + this.other;
    }

    // just an example - in reality you'd probably want to keep one instance for each property in a static dictionary.
    public static Days Mon {get{return new Days("Monday",1);}}
    public static Days Tue {get{return new Days("Tuesday",2);}}
    // etc.

}

But there are significant differences between it and a "true" enum.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • That works great up until the point when someone asks you to internationalise it ;) – Matthew Watson Oct 14 '15 at 15:31
  • So I can't have a bunch of fields on each of different values and classes... Great :/ – Spedwards Oct 14 '15 at 15:32
  • 1
    @MatthewWatson you always can use `[Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))]` overload of this Attribute. – teo van kot Oct 14 '15 at 15:33
  • @Spedwards Not in a true `enum`. See my edit for an example that's closer to what you want. You can also look at the implementation of [`System.Color`](http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Color.cs) for another real-world example – D Stanley Oct 14 '15 at 15:35
2

Using an Extension Method you can add additional funtions to an enum.

public enum Days {
    MON =1,
    TUE=2,
    WED=3,
    THUR=4,
    FRI=5,
    SAT=6,
    SUN=7
}

public static class EnumTypeExtensions {
    public static string FriendlyString(this Days day) {
        switch (day) {
            case Days.MON:
                return "Monday";
            case Days.TUE:
                return "Tuesday";
            ...
        }
    }
}


//Usage-Example:
public void test() {
    Days tuesday = Days.TUE;
    string s = tuesday .FriendlyString();
    //s == "Tuesday"
}
Tim Schmidt
  • 1,297
  • 1
  • 15
  • 30
mp7
  • 151
  • 1
  • 4