16

How can I declare an enum that has strings for values?

private enum breakout {            
    page = "String1",
    column = "String2",
    pagenames = "String3",
    row = "String4"
}
Geek
  • 195
  • 1
  • 1
  • 9

6 Answers6

20

No they cannot. They are limited to numeric values of the underlying enum type.

You can however get similar behavior via a helper method

public static string GetStringVersion(breakout value) {
  switch (value) {
    case breakout.page: return "String1";
    case breakout.column: return "String2";
    case breakout.pagenames: return "String3";
    case breakout.row: return "String4";
    default: return "Bad enum value";
  }
}
IAbstract
  • 19,551
  • 15
  • 98
  • 146
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 3
    +1 Good one with the helper method! – Rob Jun 09 '11 at 19:18
  • 1
    `ToString("F")` is useful for formatting to the name of the enum value, if they don't want custom strings. – Grant Thomas Jun 09 '11 at 19:20
  • A note with the switch: if you leave out the default case, then if you update the enum with new values the compiler will warn you the switch needs to be updated too. This can be handy. – Chris Walsh Jun 09 '11 at 19:24
  • @Chris I don't believe that is the case. Without the `default` label the C# compiler will only warn / error about the lack of return on all code execution paths – JaredPar Jun 09 '11 at 19:27
  • Yup I was in my own little world there. Just tried what I was mentioning and now I can't remember what exactly I was thinking of. – Chris Walsh Jun 09 '11 at 19:30
  • 1
    @Chris I believe certain tools like R# or FxCop may provide this behavior. Can't remember for sure though – JaredPar Jun 09 '11 at 19:32
  • Thanks @Jared Par let me resort to your solution.. – Geek Jun 09 '11 at 19:57
  • The default case should throw an exception. – Jouke van der Maas Jun 09 '11 at 20:03
  • @JaredPar Neat! A dynamic + generic version would be nice. Take in a `dictionary` and csharp or IL-Emit this ultraperformant version. Perhaps someone has already implemented something like that? – Lorenz Lo Sauer Dec 08 '13 at 20:12
13

As others have said, no you cannot.

You can do static classes like so:

internal static class Breakout {
    public static readonly string page="String1";
    public static readonly string column="String2";
    public static readonly string pagenames="String3";
    public static readonly string row="String4";

    // Or you could initialize in static constructor
    static Breakout() {
        //row = string.Format("String{0}", 4);
    }
}

Or

internal static class Breakout {
    public const string page="String1";
    public const string column="String2";
    public const string pagenames="String3";
    public const string row="String4";
}

Using readonly, you can actually assign the value in a static constructor. When using const, it must be a fixed string.

Or assign a DescriptionAttribute to enum values, like here.

Community
  • 1
  • 1
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
8

No, but you can get the enum's value as a string:

Enum.ToString Method

private enum Breakout {     
  page,
  column,
  pagenames,
  row
}

Breakout b = Breakout.page;
String s = b.ToString(); // "page"
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
2

An enum has integer as underlying type by default, which is also stated here on msdn.

Rob
  • 6,731
  • 12
  • 52
  • 90
2

Maybe Enum.GetName()/Enum.GetNames() can be helpful for you.

illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84
  • These methods work via reflection. At that point one might as well use assembly metadata, which is slow to access. One of the reasons for Enums are their performance. – Lorenz Lo Sauer Dec 08 '13 at 20:07
1

You can create a dictionary of enums.

public enum OrderType
{
    ASC,
    DESC
}

public class MyClass
{
    private Dictionary<OrderType, string> MyDictionary= new Dictionary<OrderType, string>()
                                                     {
                                                         {OrderType.ASC, ""},
                                                         {OrderType.DESC, ""},
                                                     };
}
John
  • 107
  • 5