Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

How can I cast string to enum?

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
Comment

casting string to enum type

string str = "Dog";
Animal animal = (Animal)Enum.Parse(typeof(Animal), str);  // Animal.Dog
Animal animal = (Animal)Enum.Parse(typeof(Animal), str, true); // case insensitive
Comment

Convert string to Enum using C#

/// 09/14/2022 Mahesh Kumar Yadav. <br/>
/// <summary>
/// Convert string to enum
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T ToEnum<T>(this string value)
{
    return (T)Enum.Parse(typeof(T), value, true);
}
Comment

PREVIOUS NEXT
Code Example
::  
::  
Csharp :: c# get files of type in directory 
::  
Csharp :: regex replace all special characters 
Csharp :: c# string newline 
Csharp :: c# read next int Like Java 
Csharp :: unity set sprite transparency 
::  
Csharp :: c# transparent label 
Csharp :: c# display a variable to a text gameobject 
Csharp :: c# convert list to string 
:: unity read from text file 
::  
Csharp ::  
::  
::  
::  
::  
:: small modal popup bootstrap 
Csharp :: get name of project c# .net 
:: fluentassertions force exceptions 
:: c# append textbox 
Csharp :: get query string parameter from string value c# 
Csharp ::  
Csharp ::  
Csharp :: how to get the date of the first day and last day of the week c# 
Csharp ::  
Csharp ::  
:: how to redirect to extern page in .net core 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =