Enum from string, int, etc

后端 未结 7 1034
伪装坚强ぢ
伪装坚强ぢ 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:06

    You can either make extension methods on int and string.

    Or make static method on some other static class. Maybe something like EnumHelper.FromInt(int).

    But I would pose one question : Why do you want to convert to string or int? Its not how you normaly work with enumerables, except maybe serialisation. But that should be handled by some kind of infrastructure, not your own code.

提交回复
热议问题