Isn’t it odd that…


From the files of things that make you go “hmmmm”.


 


System.Exception (the base class for all exceptions) is indeed an exception: throw new Exception ()


 


But…


System.Enum (the base class for all enums) is not an enum: typeof (Enum).IsEnum == false


System.ValueType (the base class for all ValueTypes) is not a ValueType: typeof (ValueType).IsValueType == false


System.Attribute (the base class for all custom attributes) is not a custom attribute [Attribute] -> compile error


From the files of things that make you go “hmmmm”.


 


System.Exception (the base class for all exceptions) is indeed an exception: throw new Exception ()


 


But…


System.Enum (the base class for all enums) is not an enum: typeof (Enum).IsEnum == false


System.ValueType (the base class for all ValueTypes) is not a ValueType: typeof (ValueType).IsValueType == false


System.Attribute (the base class for all custom attributes) is not a custom attribute [Attribute] -> compile error.


 


 


There is, of course, good reason for all of this, but still a bit odd when you try to explain to someone the first time.


Comments (10)

  1. Martin Mueller says:

    Even odder (pun intended) is that <b>System.Reflection</b> thinks of poor <b>System.Enum</b> as the only true orphan in the whole type system:

    – typeof (System.Enum).IsEnum returns false : OK, since its no enum

    – typeof (System.Enum).IsValueType returns false : OK, since its no struct

    – typeof (System.Enum).IsInterface returns false : OK, since its no interface

    – typeof (System.Enum).IsClass returns false : Error ??

    According to section 14.4 of the C# spec, <b>System.Enum</b> is the abstract base <b>class</b> of all enums, so i think <b>IsClass</b> should return true, but it doesnt. For every other type, at least one of the four IsXYZ properties listed above returns true.

    Is that a bug in Net 1.1 ?

    If not, why not ?

    If yes, will it be fixed in Net 1.x / 2.x ?

  2. Miguel de Icaza says:

    Heh, we ran into that in our C# compiler, as the first pass

    blindly assumed the intuitive behavior, but you must generate

    code differently as explained above.

    Miguel.

  3. Ken Brubaker says:

    I guess that’s why they made Type.IsSubclassOf rather than IsTypeOrSubclass. 🙂

  4. Om says:

    It’ something we can compare to the Russell paradox

  5. Joel Pobar says:

    typeof(System.Enum).IsXXXX does behave strangely, and is more that likely a bug. We’ll take a look at that soon, and hopefully have a fix if it’s deemed a problem.

  6. Brad Abrams says:

    Oh, and I just noticed that System.Array is not an Array:

    typeof (Array).IsArray = false…

  7. Corneliu says:

    panel1 is a child of Form1

    label1 is a child of panel1.

    All are visible.

    panel1.Visible = false;

    label1.Visible = label1.Visible;

    panel1.Visible = true;

    What will now be visible?

    panel1 only!!. label1 will be hidden ..

    Right .. cose Visible returns the visiblity of it’s parents also.

    Why?

    Corneliu.

Skip to main content