为什么将int转换为无效的枚举值而不抛出异常?

jcv*_*dan 114 c#

如果我有这样的枚举:

enum Beer
{
    Bud = 10,
    Stella = 20,
    Unknown
}
Run Code Online (Sandbox Code Playgroud)

为什么在将int超出这些值的值转换为某种类型时,它不会抛出异常Beer

例如,以下代码不会抛出异常,它会向控制台输出"50":

int i = 50;
var b = (Beer) i;

Console.WriteLine(b.ToString());
Run Code Online (Sandbox Code Playgroud)

我觉得这很奇怪......任何人都可以澄清吗?

Str*_*ior 75

取自混淆解析枚举

这是创建.NET的人的决定.枚举被另一个值类型(备份int,short,byte,等),所以它实际上可以有一个有效期为那些值类型的任意值.

我个人不喜欢它的工作方式,所以我做了一系列实用方法:

/// <summary>
/// Utility methods for enum values. This static type will fail to initialize 
/// (throwing a <see cref="TypeInitializationException"/>) if
/// you try to provide a value that is not an enum.
/// </summary>
/// <typeparam name="T">An enum type. </typeparam>
public static class EnumUtil<T>
    where T : struct, IConvertible // Try to get as much of a static check as we can.
{
    // The .NET framework doesn't provide a compile-checked
    // way to ensure that a type is an enum, so we have to check when the type
    // is statically invoked.
    static EnumUtil()
    {
        // Throw Exception on static initialization if the given type isn't an enum.
        Require.That(typeof (T).IsEnum, () => typeof(T).FullName + " is not an enum type.");
    }

    /// <summary>
    /// In the .NET Framework, objects can be cast to enum values which are not
    /// defined for their type. This method provides a simple fail-fast check
    /// that the enum value is defined, and creates a cast at the same time.
    /// Cast the given value as the given enum type.
    /// Throw an exception if the value is not defined for the given enum type.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="enumValue"></param>
    /// <exception cref="InvalidCastException">
    /// If the given value is not a defined value of the enum type.
    /// </exception>
    /// <returns></returns>
    public static T DefinedCast(object enumValue)

    {
        if (!System.Enum.IsDefined(typeof(T), enumValue))
            throw new InvalidCastException(enumValue + " is not a defined value for enum type " +
                                           typeof (T).FullName);
        return (T) enumValue;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="enumValue"></param>
    /// <returns></returns>
    public static T Parse(string enumValue)
    {
        var parsedValue = (T)System.Enum.Parse(typeof (T), enumValue);
        //Require that the parsed value is defined
        Require.That(parsedValue.IsDefined(), 
            () => new ArgumentException(string.Format("{0} is not a defined value for enum type {1}", 
                enumValue, typeof(T).FullName)));
        return parsedValue;
    }

    public static bool IsDefined(T enumValue)
    {
        return System.Enum.IsDefined(typeof (T), enumValue);
    }

}


public static class EnumExtensions
{
    public static bool IsDefined<T>(this T enumValue)
        where T : struct, IConvertible
    {
        return EnumUtil<T>.IsDefined(enumValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,我可以说:

if(!sEnum.IsDefined()) throw new Exception(...);
Run Code Online (Sandbox Code Playgroud)

... 要么:

EnumUtil<Stooge>.Parse(s); // throws an exception if s is not a defined value.
Run Code Online (Sandbox Code Playgroud)

编辑

除了上面给出的解释之外,你必须意识到,Enum的.NET版本遵循的是比C语言更具C语言的模式.这使得可以具有"位标志"枚举,其可以使用二进制模式来确定特定"标志"在枚举值中是否有效.如果你必须定义每个可能的标志组合(即MondayAndTuesday,MondayAndWednesdayAndThursday),这将是非常繁琐的.因此,具有使用未定义的枚举值的能力可以非常方便.当你想要在不利用这些技巧的枚举类型上实现快速失败时,它只需要一些额外的工作.

  • @dormisher:"'这是疯狂的,但有方法可以." 看我的编辑. (3认同)
  • @StriplingWarrior,请关注。Java等效于EnumSet.of(星期一,星期三,星期四)。EnumSet内只有一个long。因此提供了一个不错的API,而没有太多效率损失。 (2认同)
  • "Require.That()" =&gt; http://stackoverflow.com/questions/4892548/confusion-with-parsing-an-enum/4892571#comment5443975_4892571 (2认同)

Eri*_*ert 51

枚举通常用作标志:

[Flags]
enum Permission
{
    None = 0x00,
    Read = 0x01,
    Write = 0x02,
}
...

Permission p = Permission.Read | Permission.Write;
Run Code Online (Sandbox Code Playgroud)

p的值是整数3,它不是枚举的值,但显然是有效值.

我个人宁愿看到不同的解决方案; 我宁愿有能力将"位数组"整数类型 "一组不同值"类型作为两种不同的语言特征,而不是将它们都混合成"枚举".但这就是原始语言和框架设计者想出的东西; 因此,我们必须允许枚举的非声明值为合法值.

  • 但3"显然是一个有效的价值"有点加强了OP的观点.[Flags]属性可以告诉编译器查找已定义的枚举或有效值.只是在说'. (18认同)

Ree*_*sey 14

简短的回答:语言设计者决定以这种方式设计语言.

答案很长:Section 6.2.2: Explicit enumeration conversionsC#语言规范说:

通过将任何参与的枚举类型视为该枚举类型的基础类型,然后在结果类型之间执行隐式或显式数字转换,处理两种类型之间的显式枚举转换.例如,给定枚举类型E with和基础类型int,从E到byte的转换作为显式数字转换(第6.2.1节)从int到byte处理,从byte到E的转换处理为从byte到int的隐式数值转换(第6.1.2节).

基本上,在进行转换操作时,枚举被视为基础类型.默认情况下,枚举的基础类型是Int32,这意味着转换的处理方式与转换完全相同Int32.这意味着任何有效值int都是允许的.

我怀疑这主要是出于性能原因.通过制作enum简单的整数类型并允许任何整数类型转换,CLR不需要进行所有额外检查.这意味着enum与使用整数相比,使用a确实没有任何性能损失,这反过来又有助于鼓励使用它.

  • 你觉得这很奇怪吗?我认为枚举的要点之一是安全地对一系列整数进行分组,我们也可以赋予它们各自的含义。对我来说,允许枚举类型保存任何整数值违背了它的目的。 (3认同)

Mar*_*tin 9

文档:

类型为Days的变量可以在基础类型的范围内分配任何值; 值不限于命名常量.