Enums

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

Simple question. Is there any reason an enum cannot inherit from a string or
guid?

Cheers,
Michael
 
Simple question. Is there any reason an enum cannot inherit from a string or

As far as inheriting from a string, there are a few reasons why this wont
work. 1) enums are value types; strings are implemented as classes. 2) The
System.String class is sealed meaning it can't be inherited from. 3) strings
are immutable.

As far as inheriting from a GUID goes, consider the following: 1) Even
though a GUID is a value type, it takes up 128 bits (16 bytes). This is
overkill as far as most enums go. 2) Part of the beauty of enums is that we
can represent a state or type in a very small space. When defined, we can
tell the compiler the size of the enum.

I guess the overriding question I have, though, is why would you want to
inherit from either?

..ARN.
 
first, there is not an inheritance in place: enums have underlying base
types.

maybe this is what u want to do :
public enum MyEnum
{
first = 1,
second = 2
}
MyEnum enm = (MyEnum)Enum.Parse(typeof(MyEnum), "FiRSt", true);

Console.Writeline(enm.ToString());



output is "first"
 
Back
Top