How to convert a string to enum in C#

How to convert a string to enum in C#

In this post we will give you information about How to convert a string to enum in C#. Hear we will give you detail about How to convert a string to enum in C#And how to use it also give you demo for it if it is necessary.

In this tutorial, we are going to learn about how to convert a string to enum in C# with the help of examples

Using Enum.parse() method

To convert a string to enum, we can use the built-in Enum.parse() method in C#.

The Enum.parse() method takes the two arguments, the first one is enum type and second one is string then it converts to enum.

Here is an example:

using System;class ConvertEnum {     enum Vehicle { Bus = 14, Lorry = 2, Car = 34 };     static void Main() {      Vehicle name= (Vehicle)Enum.Parse(typeof(Vehicle), "Bus");     System.Console.WriteLine(name);  }}

Output:

Bus

Hope this code and post will helped you for implement How to convert a string to enum in C#. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US