C# 枚举反向索引
有没有办法使用整数索引从枚举返回适当的值?例如,如果有枚举 Color {Red,Green,Blue) 是否有一个函数,对于值 0 将返回红色,1 将返回绿色,2 将返回蓝色?
Is there a way to use an integer index to return the appropriate value from an enum? For example, if there is the enum Color {Red, Green, Blue) is there a function that for the value 0 will return Red, 1 will return Green, and 2 will return Blue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Enum.GetName 方法: http://msdn.microsoft.com /en-us/library/system.enum.getname.aspx
使用您的示例,
打印“绿色”
The Enum.GetName method: http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx
Using your example,
prints "Green"
您可以将整数值转换为枚举。
You can cast your integer value to an enum.
使用 Enum.ToString() 方法。
http://msdn.microsoft.com/en-us/library/16c1xs4z.aspx
Use the Enum.ToString() method.
http://msdn.microsoft.com/en-us/library/16c1xs4z.aspx
这很笨拙,但是...
It's klunky but...