Cast/Convert String, Int to Enum in C# with Examples

  By : Suresh Dasari
  Posted On : 19-Jun-2023

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.

What is Enum (Enumeration) in C#?

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#.

 

enum VehicleTypes
{
   Sedan,
   Hatchback,
   SUV,
   CrossOver,
   Roadster,
   Pickup,
   Van
}

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. 

C# Convert Int to Enum using Explicit Type Casting

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.

 

using System;

namespace Tutlane
{
   enum VehicleTypes
   {
      Sedan,
      Hatchback,
      SUV,
      CrossOver,
      Roadster,
      Pickup,
      Van
   }
   class Program
   {
      static void Main(string[] args)
      {
         int x = 3, y = 5, z = 50;
         VehicleTypes v1, v2, v3;
         v1 = (VehicleTypes)x;
         Console.WriteLine(v1); // CrossOver
         v2 = (VehicleTypes)y;
         Console.WriteLine(v2); // Pickup
         v3 = (VehicleTypes)z;
         Console.WriteLine(v3); // 50
         Console.ReadLine();
      }
   }
}

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.

 

CrossOver
Pickup
50

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.

C# Convert Int to Enum using Enum.ToObject() Method

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#.

 

using System;

namespace Tutlane
{
   enum VehicleTypes
   {
      Sedan,
      Hatchback,
      SUV,
      CrossOver,
      Roadster,
      Pickup,
      Van
   }
   class Program
   {
      static void Main(string[] args)
      {
         int x = 3, y = 5, z = 50;
         VehicleTypes v1, v2, v3;
         v1 = (VehicleTypes)Enum.ToObject(typeof(VehicleTypes), x);
         Console.WriteLine(v1);
         v2 = (VehicleTypes)Enum.ToObject(typeof(VehicleTypes), y);
         Console.WriteLine(v2);
         v3 = (VehicleTypes)Enum.ToObject(typeof(VehicleTypes), z);
         Console.WriteLine(v3);
         Console.ReadLine();
      }
   }
}

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.

 

CrossOver
Pickup
50 

Convert Enum to Int in C#

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#.

 

using System;

namespace Tutlane
{
   enum VehicleTypes
   {
      Sedan,
      Hatchback,
      SUV,
      CrossOver,
      Roadster,
      Pickup,
      Van
   }
   class Program
   {
      static void Main(string[] args)
      {
         int x = (int)VehicleTypes.CrossOver;
         Console.WriteLine(x); // 3
         int y = (int)VehicleTypes.SUV;
         Console.WriteLine(y); // 2
         int z = (int)VehicleTypes.Hatchback;
         Console.WriteLine(z); // 1
         Console.ReadLine();
      }
   }
}

When you execute this example, you will get the following result.

 

3
2
1

This is how we can convert int to enum and enum to int in c# based on our requirements.

C# Convert String to Enum using Enum.Parse Method

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#.

 

using System;

namespace Tutlane
{
   enum VehicleTypes
   {
      Sedan,
      Hatchback,
      SUV,
      CrossOver,
      Roadster,
      Pickup,
      Van
   }
   class Program
   {
      static void Main(string[] args)
      {
         // Convert string to enum
         string x = "Roadster", y = "Van", z = "SUV";
         VehicleTypes v1, v2, v3;
         v1 = (VehicleTypes)Enum.Parse(typeof(VehicleTypes), x);
         Console.WriteLine(v1);
         v2 = (VehicleTypes)Enum.Parse(typeof(VehicleTypes), y);
         Console.WriteLine(v2);
         v3 = (VehicleTypes)Enum.Parse(typeof(VehicleTypes), z);
         Console.WriteLine(v3);
         Console.ReadLine();
      }
   }
}

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.

 

Roadster
Van
SUV

C# Convert String to Enum using Enum.TryParse Method

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#.

 

using System;
using System.Drawing;

namespace Tutlane
{
   enum VehicleTypes
   {
      Sedan,
      Hatchback,
      SUV,
      CrossOver,
      Roadster,
      Pickup,
      Van
   }
   class Program
   {
      static void Main(string[] args)
      {
         // Convert string to the enum
         string x = "Roadster";
         VehicleTypes v1;
         bool r1 = Enum.TryParse(x, out v1);
         // Check if the conversion was successful or not
         if (r1)
         {
            Console.WriteLine(v1); // Roadster
         }
         else
         {
            Console.WriteLine("Invalid vehicletype string");
         }
         Console.ReadLine();
      }
   }
}

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.

 

Roadster

This is how we can convert integer to enum, string to enum, and enum to integer in c# based on our requirements.