更好的方式来获取字符枚举 [英] Better Way To Get Char Enum
本文介绍了更好的方式来获取字符枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个更清洁的方式来获得在C#中的枚举的char值。
公共枚举DivisionStatus
{
无='N',
活性='A',
无效='我',
候补='W'
}
串状态= Enums.DivisionStatus.Active.ToString()[0]的ToString(); //A
解决方案
只投中值:
字符状态=(char)的Enums.DivisionStatus.Active;
请注意,这将使用值而不是标识符。在 Enums.DivisionStatus.Active
值的字符代码'A'
,因为这是你拥有的价值定义
直接使用的值大于仰视的值标识符更快
Is there a cleaner way to get the char value of an enum in C#.
public enum DivisionStatus
{
None = 'N',
Active = 'A',
Inactive = 'I',
Waitlist = 'W'
}
string status = Enums.DivisionStatus.Active.ToString()[0].ToString(); // "A"
解决方案
Just cast the value:
char status = (char)Enums.DivisionStatus.Active;
Note that this will use the value instead of the identifier. The Enums.DivisionStatus.Active
value is the character code of 'A'
, as that is the value that you have defined.
Using the value directly is faster than looking up the identifier for the value.
这篇关于更好的方式来获取字符枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文