How to define an enum with string value?

后端 未结 18 1392
一生所求
一生所求 2020-12-12 14:53

I am trying to define an Enum and add valid common separators which used in CSV or similar files. Then I am going to bind it to a ComboBox as a dat

18条回答
  •  旧巷少年郎
    2020-12-12 15:10

    You can't, because enum can only be based on a primitive numeric type. You could try using a Dictionary instead:

    Dictionary separators = new Dictionary
    {
        {"Comma", ','}, 
        {"Tab",  '\t'}, 
        {"Space", ' '},
    };
    

    Alternatively, you could use a Dictionary or Dictionary where Separator is a normal enum:

    enum Separator
    {
        Comma,
        Tab,
        Space
    }
    

    which would be a bit more pleasant than handling the strings directly.

提交回复
热议问题