C# int 到枚举的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/502905/
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
C# int to enum conversion
提问by TK.
Possible Duplicate:
Cast int to enum in C#
可能的重复:
在 C# 中将 int 转换为枚举
If I have the following code:
如果我有以下代码:
enum foo : int
{
option1 = 1,
option2,
...
}
private foo convertIntToFoo(int value)
{
// Convert int to respective Foo value or throw exception
}
What would the conversion code look like?
转换代码是什么样的?
采纳答案by Jon B
It's fine just to cast your int to Foo:
只需将您的 int 转换为 Foo 就可以了:
int i = 1;
Foo f = (Foo)i;
If you try to cast a value that's not defined it will still work. The only harm that may come from this is in how you use the value later on.
如果您尝试转换一个未定义的值,它仍然可以工作。这可能带来的唯一伤害是您以后如何使用该值。
If you really want to make sure your value is defined in the enum, you can use Enum.IsDefined:
如果你真的想确保你的值是在枚举中定义的,你可以使用 Enum.IsDefined:
int i = 1;
if (Enum.IsDefined(typeof(Foo), i))
{
Foo f = (Foo)i;
}
else
{
// Throw exception, etc.
}
However, using IsDefined costs more than just casting. Which you use depends on your implemenation. You might consider restricting user input, or handling a default case when you use the enum.
但是,使用 IsDefined 的成本不仅仅是强制转换。你使用哪个取决于你的实现。您可能会考虑限制用户输入,或在使用枚举时处理默认情况。
Also note that you don't have to specify that your enum inherits from int; this is the default behavior.
另请注意,您不必指定您的枚举继承自 int;这是默认行为。
回答by Cameron MacFarland
I'm pretty sure you can do explicit casting here.
我很确定你可以在这里进行显式转换。
foo f = (foo)value;
So long as you say the enum inherits(?) from int, which you have.
只要您说枚举从您拥有的 int 继承(?)。
enum foo : int
EDITYes it turns out that by default, an enums underlying type is int. You can however use any integral type except char.
编辑是的,默认情况下,枚举基础类型是 int。但是,您可以使用除 char 之外的任何整数类型。
You can also cast from a value that's not in the enum, producing an invalid enum. I suspect this works by just changing the type of the reference and not actually changing the value in memory.
您还可以从不在枚举中的值进行转换,从而产生无效的枚举。我怀疑这只是通过更改引用的类型而不是实际更改内存中的值来起作用。
回答by Sergio
You don't need the inheritance. You can do:
你不需要继承。你可以做:
(Foo)1
it will work ;)
它会工作;)
回答by WestDiscGolf
if (Enum.IsDefined(typeof(foo), value))
{
return (Foo)Enum.Parse(typeof(foo), value);
}
Hope this helps
希望这可以帮助
EditThis answer got down voted as value in my example is a string, where as the question asked for an int. My applogies; the following should be a bit clearer :-)
编辑这个答案在我的例子中被投票为值是一个字符串,而问题要求一个整数。我的应用程序;以下应该更清楚一点:-)
Type fooType = typeof(foo);
if (Enum.IsDefined(fooType , value.ToString()))
{
return (Foo)Enum.Parse(fooType , value.ToString());
}
回答by weiran
Casting should be enough. If you're using C# 3.0 you can make a handy extension method to parse enum values:
铸造应该足够了。如果您使用的是 C# 3.0,您可以创建一个方便的扩展方法来解析枚举值:
public static TEnum ToEnum<TInput, TEnum>(this TInput value)
{
Type type = typeof(TEnum);
if (value == default(TInput))
{
throw new ArgumentException("Value is null or empty.", "value");
}
if (!type.IsEnum)
{
throw new ArgumentException("Enum expected.", "TEnum");
}
return (TEnum)Enum.Parse(type, value.ToString(), true);
}