C# 如何从枚举中获取数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1540779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the numeric value from the Enum?
提问by kurozakura
For example System.Net.HttpStatusCode Enum, I would like to get the HTTP Status Codes instead of the HTTP Status Text.
System.Net.HttpStatusCode.Forbidden
should return 403 instead of "Forbidden".
例如 System.Net.HttpStatusCode 枚举,我想获取 HTTP 状态代码而不是 HTTP 状态文本。
System.Net.HttpStatusCode.Forbidden
应该返回 403 而不是“禁止”。
How can I extract the value?
我怎样才能提取价值?
采纳答案by JaredPar
For the majority of Enum's simply cast to the base type which is int32.
对于大多数 Enum 只是简单地转换为 int32 的基本类型。
int value = (int)System.Net.HttpStatusCode.Forbidden;
回答by Erich
You can just cast it to an integer!
您可以将其强制转换为整数!
int code = (int)enumVariable
回答by Dieter G
System.Convert.ToInt32(response.StatusCode) returns the statusCode number
System.Convert.ToInt32(response.StatusCode) 返回 statusCode 编号
回答by reza akhlaghi
you can use a Principled way using .NET CLR functions..
您可以使用使用 .NET CLR 函数的 Principled 方式。
int value = System.Net.HttpStatusCode.Forbidden.GetHashCode();