Enum from string, int, etc

后端 未结 7 948
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 07:23

Using extension method we can create methods to convert an enum to other datatype like string, int by creating extension methods ToInt(), ToString()

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-17 08:04

    Do you really need those extension methods?

    MyEnum fromInt = (MyEnum)someIntValue;
    MyEnum fromString = (MyEnum)Enum.Parse(typeof(MyEnum), someStringValue, true);
    
    int intFromEnum = (int)MyEnum.SomeValue;
    string stringFromEnum = MyEnum.SomeValue.ToString();
    

提交回复
热议问题