typeof(System.Enum).IsClass == false

后端 未结 2 1499
借酒劲吻你
借酒劲吻你 2020-12-19 15:49

Founded that:

typeof(System.Enum).IsClass == false

It\'s become strange that System.Enum has also .IsValueType == false<

相关标签:
2条回答
  • 2020-12-19 16:24

    I happened to recently revisit this problem under CLR4 and guess what, it is fixed now. The following definitions:

    public struct SomeValueType{}
    
    public enum SomeEnum
    {
        FirstElement
    }
    

    with this program

    Console.WriteLine( typeof( Enum ).IsClass );
    Console.WriteLine( typeof( SomeEnum ).IsClass );
    
    Console.WriteLine( typeof( ValueType).IsClass );
    Console.WriteLine( typeof( SomeValueType).IsClass );
    

    Yields the following results:

    CLR2: False, False, True, False 
    CLR4: True, False, True, False
    
    0 讨论(0)
  • 2020-12-19 16:42

    It's an issue with .Net 1.1 and 2.0. I haven't checked it in 3.0

    From MSDN User David Bernstein

    The IsClass property of the System.Enum type returns "false", even though "System.Enum" inherits from "System.ValueType" and "typeof(System.ValueType).IsClass" return "true" (as expected). At the same time, typeof(System.Enum).IsValueType returns "false" as expected. This observed behavior seems to contradict the explicit documentation above which stipulates: "This property returns true for Type instances representing Enum and ValueType." I found this to be the case in both frameworks 1.1 and 2.0.

    0 讨论(0)
提交回复
热议问题