10

How can i have a c# enum that if i chose to string it returns a different string, like in java it can be done by

public enum sample{
    some, other, things;

    public string toString(){
        switch(this){
          case some: return "you choose some";
          default: break;
        }
    }
}

Console.writeln(sample.some) will output:

you choose some

i just want my enums to return a different string when i try to call them.

DACrosby
  • 11,116
  • 3
  • 39
  • 51
sclang
  • 229
  • 2
  • 15

4 Answers4

12

To my knowledge this is not possible. However, you can write an extension method that gets some other string:

public static class EnumExtensions
{
    public static string ToSampleString(this SampleEnum enum)
    {
         switch(enum)
         {
             case SampleEnum.Value1 : return "Foo";
             etc.
         }
    }
}

Now, just call this new ToSampleString on instances of SampleEnum:

mySampleEnum.ToSampleString();

If you are unfamiliar with extension methods in C#, read more here.

Another option is to use an Description attribute above each enum value, as described here.

Community
  • 1
  • 1
Ben Reich
  • 16,222
  • 2
  • 38
  • 59
  • You could use a combination of the description on the enum class as well as the enum value to make a generic message thats defined once, with the extension method to compose it together. Would make for a more reusable extension method. +1 – Phill Jun 21 '13 at 04:31
  • 1
    Why is it so complicated to retrieve the Description attribute? – CodeCamper Jun 21 '13 at 08:27
  • 1
    Getting the Description attribute is only complicated once. You add it as an extension method and is available for all Enums and don't look at it anymore. Using this above answer, you'll have to create a method like this for each Enum. You can look at a nice example here: http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx – Nelson Reis Jun 21 '13 at 08:46
  • This really, really, really shouldn't be the accepted answer. Using an attribute is a much more elegant, object-oriented, encapsulated way to solve this. – Bob Horn Jun 21 '13 at 13:50
4

I would do it decoratively by creating an attribute e.g. Description and decorating the enum values with it.

e.g.

public enum Rate
{
   [Description("Flat Rate")]
   Flat,
   [Description("Time and Materials")]
   TM
}

Then use GetCustomAttributes to read/display the values. http://msdn.microsoft.com/en-us/library/system.attribute.getcustomattributes.aspx

@CodeCamper Sorry about the late response but here is some example code to read the DescriptionAttribute:

Extension method:

public static class EnumExtensions
{
    public static string Description<T>(this T t)
    {
        var descriptionAttribute = (DescriptionAttribute) typeof (T).GetMember(t.ToString())
                                   .First().GetCustomAttribute(typeof (DescriptionAttribute));

        return descriptionAttribute == null ? "" : descriptionAttribute.Description;
    }
}

Usage:

Rate currentRate = Rate.TM;
Console.WriteLine(currentRate.Description());
lahsrah
  • 9,013
  • 5
  • 37
  • 67
  • I have been reading articles on how to do this and it seems so overly complicated for some reason. Can you please show the simplest and least lines of code to retrieve the Description? – CodeCamper Jun 21 '13 at 08:28
3

You want a dictionary. An enumerator enumerates (gives a number) for its values. You want a string value to be returned when you provide a string key. Try something like:

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("some", "you choose some");
dictionary.Add("other", "you choose other");
dictionary.Add("things", "you choose things");

Then this code:

string value = dictionary["some"];
Console.writeln(value);

will return "you choose some"

mcalex
  • 6,628
  • 5
  • 50
  • 80
0

If you just want to get Enum as string you can use this method:

Enum.GetName(typeof(sample), value);

This method will return the name of Enum instead of int.

Yemol
  • 29
  • 3