Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeof(System.Enum).IsClass == false

Founded that:

typeof(System.Enum).IsClass == false

It's become strange that System.Enum has also .IsValueType == false, but Reflector shows that it is really just an abstract class.

System.Enum is a reference type like a System.ValueType and casting enumeration values to/from System.Enum reference caused boxing/unboxing. No surprises here.

But what is a reason for Type class not to tell truth about System.Enum nature? There is no anything extraordinary with the System.Enum type's reflection behavior to make it looks like not a reference type.

like image 899
controlflow Avatar asked Jan 23 '23 01:01

controlflow


1 Answers

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.

like image 124
Anthony M. Powers Avatar answered Feb 04 '23 22:02

Anthony M. Powers