---
title: "How to get an enum value from a string value in C#?"  
description: "How to get an enum value from a string value in C#?"  
author: "Utpal Vishwas"  
published: 2023-08-21  
updated: 2023-08-23  
canonical: https://www.mindstick.com/forum/159623/how-to-get-an-enum-value-from-a-string-value-in-c-sharp  
category: "c#"  
tags: ["c#", "string", "enum"]  
reading_time: 2 minutes  

---

# How to get an enum value from a string value in C#?

How to get an [enum](https://www.mindstick.com/forum/159626/how-can-i-cast-a-string-to-an-enum) value from a [string value](https://www.mindstick.com/forum/159227/how-to-get-an-enum-value-from-a-string-value-in-java) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

Sure, here are two ways to get an enum [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) from a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) value in C#:

1. Using the `Enum.Parse()` method:

C#

```plaintext
enum MyEnum {
  Value1,
  Value2,
  Value3
}

string myString = "Value1";

// Get the enum value from the string using the Enum.Parse() method.
MyEnum myEnum = Enum.Parse(typeof(MyEnum), myString);
```

In this example, the `myString` variable is a string that has the value of `"Value1"`. The `MyEnum` enum has three values: `Value1`, `Value2`, and `Value3`. The `Enum.Parse(typeof(MyEnum), myString)` method casts the `myString` variable to an enum.

1. Using the `TryParse()` method:

C#

```plaintext
enum MyEnum {
  Value1,
  Value2,
  Value3
}

string myString = "Value1";

// Get the enum value from the string using the TryParse() method.
bool success = Enum.TryParse(myString, out MyEnum myEnum);

// Check if the conversion was successful.
if (success) {
  // The conversion was successful.
} else {
  // The conversion was not successful.
}
```

In this example, the `myString` variable is a string that has the value of `"Value1"`. The `MyEnum` enum has three values: `Value1`, `Value2`, and `Value3`. The `Enum.TryParse(myString, out MyEnum myEnum)` method tries to cast the `myString` variable to an enum. If the conversion is successful, then the `success` variable is set to `true` and the `myEnum` variable is set to the enum value. Otherwise, the `success` variable is set to `false` and the `myEnum` variable is not modified.

Which method you use depends on your specific needs. If you need to get the enum value and you don't need to do any further processing on the enum value, then the `Enum.Parse()` method is a good option. If you need to do further processing on the enum value, then the `TryParse()` method is a good option.


---

Original Source: https://www.mindstick.com/forum/159623/how-to-get-an-enum-value-from-a-string-value-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
