How to use Enum in C#
Enums are strongly typed constants . If you have a number of constants that are logically related to each other, then you can group together these constants in an enumeration . The main benefit of this is that constants can be referred to in a consistent, expressive and type safe way.
Creating an enumeration type
Enums are strongly typed constants which makes the code more readable and less prone to errors. It is useful when you have a set of values that are functionally significant and unchanged. The main advantage of Enum is make it easy to change values in the future, also you can reduces errors caused by transposing or mistyping numbers.
How to retrieve enum values
By default the underlying type of each element in the enum is int. If you try with above example to convert to integer then you can see the result:
Enum underlying type
By default the underlying type of each element in the enum is int. This means that, an enum declaration that does not explicitly declare an underlying type has an underlying type of int. You can specify another integral numeric type by using a colon . You can explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long or ulong.
The following Enum declare as byte, you can verify the underlying numeric values by casting to the underlying type .
You can retrieve the value like the following:
You can assign any values to the elements in the enumerator list of an enumeration type. By default, the first member of an enum takes the value of zero. If this value doesn't make sense for your enum, you can change it to one or some other number. If you declare a different value in the first member of Enum then it assign the next value greater by one than that of the immediately preceding member .
How to cast an Enum directly to int?
How to cast int to enum?
Following is the easiest method to cast an int to enum.
Converts int to enum values
How to check If a Enum contain a number?
The Enum.IsDefined() method tests if a particular value or name is defined within an enumeration's list of constants.
This method requires two parameters. The first one is the type of the enumeration to be checked. This type is usually obtained using a typeof expression . The second one is defined as a basic object. It is used to specify either the integer value or a string containing the name of the constant to find. The return value is a Boolean that is true if the value exists and false if it does not.
Enum.Parse() converts the C# string representation of the name or integer value of one or more enumerated constants to an equivalent Enum object .
Convert a string to an enum
How to Loop through all enum values
The Enum.GetValues() method returns an array that contains a value for each member of the enum Type . If more than one members have the same value, it returned array includes duplicate values
Iterating through an enum
The following example will show how do enumerate an enum .
Enum in Switch Case
The following program shows how to use Enum in Switch..Case statement.
Enum Class Methods
|Equals(Object)||Returns a boolean value indicating whether this instance is equal to a specified object.|
|GetNames(Type)||Retrieves string array of the names of the constants in in enumType.|
|HasFlag(Enum)||Returns true one or more bit fields are set in the current instance.|
|Parse(Type, String)||Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.|
|ToString()||Returns the string representation of the value of this instance.|
Enum Flags Attribute
The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. More about.... Enum Flags Explained
Convert an enum to a List
The following program shows how to convert an enum to List Datastructure . More about.... Enum to List