Here we will learn what is enum or enumeration in c#, how to use enum in c#, how to cast or convert int to enum in c#, enum to int in c#, and how to convert string values to enum type in c# with examples.
In c#, enum is a datatype and it’s a short form of enumeration. The enum will allow us to define the set of named constants that can be logically grouped together.
An enum is particularly useful to represent the set of related constants, such as the days of the week, colors, states, types, etc. The enum will provide a structured and organized approach to handle such data.
The following is an example of defining the enumeration using the enum
keyword in c#.
In this example, we defined VehicleTypes enumeration using the enum
keyword and it contains seven named constants. Each constant will represent a unique value within the enum.
By default, when defining an enum in c#, the first named constant in the enumerator will have a value of 0, and each successive item in the enumerator will have a value that increases by 1. In the given example, the value of Monday is 0, the value of Tuesday is 1, and so on.
To learn more about enum type in c#, refer to the Enumeration (Enum) in C# with Examples.
Now we will learn how to convert or cast a string or integer value to an enum in c# with examples.
In c#, we can convert or cast int to enum using explicit type casting and Enum.ToObject() method.
Now we will learn how to convert an integer value to an enum using explicit type casting in c# with an example.
In this example, we defined the VehicleTypes enum with seven constants. By default, the first constant (Sedan) will have a value of 0, and each successive constant will increment by 1.
Here, we are casting the defined integer values to VehicleTypes enum and our enum contains 7 constants. If you observe the integer z has a value of 50 which is not explicitly defined as a constant in the enum so it will display the value of z for v3.
When you execute this example, you will get the following result.
The value 50 is displayed for v3 because it is not explicitly defined as constant in the enum so it will consider using the value of 50.
By using Enum.ToObject method, we can convert integer values to enum. The following is an example of converting int to enum using Enum.ToObject() method in c#.
In this example, we are performing conversion of integer values to VehileTypes enum type using Enum.ToObject method.
When you execute this example, you will get the following result.
In c#, you can also convert enum values to integer values by using explicit type casting in c#. The following is an example of converting an enum to int values in c#.
When you execute this example, you will get the following result.
This is how we can convert int to enum and enum to int in c# based on our requirements.
In c#, we can convert the string value to an enum using Enum.Parse and Enum.TryParse methods.
The following is an example of converting a string to an enum using Enum.Parse method in c#.
In this example, we defined the VehicleTypes enum with seven constants and cast the defined string values to the VehicleTypes enum using Enum.Parse method.
When you execute this example, you will get the following result.
In c#, you can also convert a string to an enum using Enum.TryParse method. The Enum.TryParse method will help us to convert a string to an enum and identify whether the given string exists in the enum or not. The Enum.TryParse method will return the boolean value which will indicate whether the string to enum conversion was successful or not.
The following is an example of converting string to enum using Enum.TryParse method in c#.
In this example, we used Enum.TryParse method to cast string to enum and checking whether the string to enum conversion is a success or not.
When you execute this example, you will get the following result.
This is how we can convert integer to enum, string to enum, and enum to integer in c# based on our requirements.