Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumeration type in c#

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 ?

like image 678
mdadil2019 Avatar asked May 28 '16 09:05

mdadil2019


People also ask

What is enumeration data type in C?

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.

Which type of data type is enumeration?

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.

What are enumerated data types in C give an example?

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.

Why enumeration is used in C?

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.


2 Answers

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.

like image 200
Jon Skeet Avatar answered Nov 14 '22 20:11

Jon Skeet


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

like image 39
Charles Mager Avatar answered Nov 14 '22 22:11

Charles Mager