Implementing some method with parameter of type Enum
as base class for all enums I was curios whether i need to check the meth's parameter of type Enum
is null
.
From experience I know, that enum can't be null, that is also confirmed by compiler: CS0037.
In the documentation for System.Enum there are sentences like:
"Enum is the base class for all enumerations in the .NET Framework"
and
"Note that in all cases, the enumeration does not explicitly inherit from Enum; the inheritance relationship is handled implicitly by the compiler"
What does mean "inheritance relationship is handled implicitly by the compiler" and if there is an inheritance why I can't assign null
to the derived class, but I can do it for the base class? Is it something like object
and int
?
enum SomeEnum { a,b,c }
void SomeMethWantsEnum(Enum someEnum)
{
if (someEnum == null) return;
var x = SomeEnum.a;
someEnum = x; //OK
someEnum = null; //OK
x = null; //Not OK, CS0037
}