When we create variable of enumeration type and assign it an enumeration value
enum Members{HighlyQualified, Qualified, Ordinary}
class
{
static void Main()
{
Members developers = Members.HighlyQualified;
Console.WriteLine(developers);//write out HighlyQualified
}
}
Since enum is value type so the value of developers is stored on stack which is returned by Members.HighlyQualified.Here we are clear that the value of developers is string which reference to the memory location of characters.
Now,
1.If we cast Members.HighlyQualifed to an int then the value returned is 0. How it happens?
2.What value is really stored on stack for an enumeration type ?
Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.
An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them.
In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on.
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
Here we are clear that the value of developers is string which reference to the memory location of characters.
No, it's not. The value of developers
is of type Members
. It's converted to a string by the Console.WriteLine
method. You'll be calling the Console.WriteLine(object)
overload, which boxes the value - and then Console.WriteLine
will call ToString
on that boxed value, giving the appropriate enum value name.
If we cast Members.HighlyQualifed to an int then the value returned is 0. How it happens?
HighlyQualified
is the first member declared in Members
, and you haven't assigned any specific value. By default, the C# compiler assigns a value of 0 to the first declared value, then increments by 1 each time. If you cast Members.Qualified
to int
, you'd get 1.
What value is really stored on stack for an enumeration type ?
The value, which is effectively just a number. (In this case, an int
because that's the default underlying type. But the stack slot has the right type - the enum type.
The documentation explains the underlying type:
By default the underlying type of each element in the enum is int.
and how the values are generated when not explicitly specified:
When you do not specify values for the elements in the enumerator list, the values are automatically incremented by 1.
So, in your case, the declaration is equivalent to:
enum Members : int
{
HighlyQualified = 0,
Qualified = 1,
Ordinary = 2
}
What's on the stack is the enum
type itself (Members
in this case), and when you call Console.WriteLine
it will call ToString
on that, for which, per the docs for that, returns:
a string containing the name of the constant
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With