C# .net core convert string to enum
var foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
if (Enum.IsDefined(typeof(YourEnum), foo))
{
return foo;
}
C# .net core convert string to enum
var foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
if (Enum.IsDefined(typeof(YourEnum), foo))
{
return foo;
}
c# string enum
public static class Status
{
public const string Awesome = "Awesome";
public const string Cool = "Cool";
}
//Not an enum but has a similar effect without needing to convert ints
how to pass string value to enum in c#
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), myString);
enum value to string
class Program.
static void Main(string[] args)
{
Enum wkday = Weekday.Friday;
Console.WriteLine("Enum string is '{0}'", wkday.ToString());
Console.ReadKey();
}
Converting Enum to String and Strint to Enum
private enum Status
{
Awesome = 1,
Cool = 2
};
public string ConvertEnumToString(Status status)
{
return status.ToString();
}
public Status ConvertStringToEnum(string status)
{
return (Status) Enum.Parse(typeof (Status), status);
}
//serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
//Fast enum
public enum HumanStates
{
Idle,
Working,
Sleeping,
Eating,
Dead
}
public string FastToString(HumanStates state)
{
switch(state)
{
case HumanStates.Idle:
return nameof(HumanStates.Idle);
case HumanStates.Working:
return nameof(HumanStates.Working);
case HumanStates.Sleeping:
return nameof(HumanStates.Sleeping);
case HumanStates.Eating:
return nameof(HumanStates.Eating);
case HumanStates.Dead:
return nameof(HumanStates.Dead);
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
java enum to string
public enum MyEnum{
SOMETHING,SOMETHING_ELSE
}
MyEnum enumObject=MyEnum.SOMETHING;
System.out.println(enumObject.toString());
System.out.println(enumObject.name());
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us