How to use Enum in C#
Enums in C# are a way to define a set of strongly typed constants. They are useful when you have a collection of constants that are logically related to each other. By grouping these constants together in an enumeration, you can refer to them in a consistent, expressive, and type-safe manner. This provides several benefits, including improved code readability, better code organization, and enhanced type safety during compilation.Syntax
Enums in C# serve as strongly typed constants that contribute to improved code readability and reduced error susceptibility. They are particularly useful when you have a fixed set of functionally significant values that are unlikely to change. The main advantage of using enums is their ability to facilitate future value modifications with ease, ensuring consistency and minimizing errors caused by numerical transposition or typographical mistakes. By providing a clear and intuitive representation of these values, enums enhance code maintainability and robustness.
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 an enum is int in C#. This implies that if an enum declaration does not explicitly specify an underlying type, it will automatically default to int. However, you have the flexibility to specify a different integral numeric type by utilizing a colon and explicitly declaring the desired underlying type. C# allows you to explicitly assign an underlying type of byte, sbyte, short, ushort, int, uint, long, or ulong to an enum, depending on the specific requirements of your application.
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 have the flexibility to assign custom values to the elements within an enum. By default, the first member of an enum is assigned a value of zero. However, if this value is not appropriate for your specific enum, you have the option to assign it a different value, such as one or any other desired number. When a different value is declared for the first member, the subsequent members of the enum are automatically assigned values that increment by one from the value of the preceding member. This allows you to define a meaningful and sequential set of values for your enum elements.
How to cast an Enum directly to int?
How to cast int to enum?Method 1:
Following is the easiest method to cast an int to enum.
Converts int to enum valuesMethod 2:
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.|
C# enum is a feature that allows you to define a set of named constants, providing more readability and type safety to your code. By default, enums have an underlying type of int, but you can specify a different integral type if needed.