How to define an enum with string value?

后端 未结 18 1376
一生所求
一生所求 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:11

    Maybe it's too late, but here it goes.

    We can use the attribute EnumMember to manage Enum values.

    public enum EUnitOfMeasure
    {
        [EnumMember(Value = "KM")]
        Kilometer,
        [EnumMember(Value = "MI")]
        Miles
    }
    

    This way the result value for EUnitOfMeasure will be KM or MI. This also can be seen in Andrew Whitaker answer.

提交回复
热议问题