enum in c#

Enum in c# programming language-: An enumeration is a special kind of value type consisting of a list of named constants. To create one,we can use the enum keyword followed by a name and a code block containing a comma-separated list of constant elements.

enum State { Run, Wait, Stop };

This enumeration type can be used to create variables that can hold these constants. To assign a value to the enum variable, the elements are accessed from the enum as if they were static members of a class.
State s = State.Run;

Enum declaration -: An enum declaration declares a new enum type. An enum declaration begins with the keyword enum, and defines the name, accessibility, underlying type, and members of the enum.

Each enum type has a corresponding integral type called the underlying type of the enum type. This underlying type must be able to represent all the enumerator values defined in the enumeration.

An enum declaration may explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long, or ulong.  char cannot be used as an underlying type. An enum declaration that does not explicitly declare an underlying type has an underlying type .

 

Enum constant value-: There is usually no need to know the actual constant values that the enum constants represent, but sometimes it can be useful. By default, the first element has the value 0, and each successive element has one value higher.
enum State
{
Run, // 0
Wait, // 1
Stop // 2
};
These default values can be overridden by assigning values to the constants. The values can be computed from an expression and they do not have to be unique.
enum State
{
Run = 0, Wait = 3, Stop = Wait + 1
};

Enum Constant Type-:The underlying type of the constant elements is implicitly specified as int, but this can be changed by using a colon after the enumeration’s name followed by the desired integer type.
enum MyEnum : byte {};

Enum Access Levels and Scope-: The access levels for enumerations are the same as for classes. They are internal by default, but can also be declared as public. Although enumerations are usually defined at the top-level, they may be contained within a class. In a class they have private access by default, and can be set to any one of the access levels.

Enum Methods-: An enumeration constant can be cast to an int and the ToString method can be used to obtain its name.
static void Main()
{
State s = State.Run;
int i = (int)s; // 0
string t = s.ToString(); // Run
}
Several enumeration methods are available in the System.Enum class, such as GetNames() to obtain an array containing the names of the enum constants. Note that this method takes a type object (System.Type) as its argument, which is retrieved using the typeof operator.
enum Colors { Red, Green };
static void Main()
{
         foreach (string s in System.Enum.GetNames(typeof(Colors)))
{
        System.Console.Write(s); // “RedGreen”
}
}

Enum value and operation -:Each enum type defines a distinct type, an explicit enumeration conversion is required to convert between an enum type and an integral type, or between two enum types. The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type, and is a distinct valid value of that enum type.

Enum members have the type of their containing enum type (except within other enum member initializers. The value of an enum member declared in enum type E with associated value v is (E)v.

The following operators can be used on values of enum types: ==, !=, <, >, <=, >=  binary +  binary – ^, &, |  ~  and ++ and –.

 

Leave a Reply

Your email address will not be published. Required fields are marked *