how to check if string value is in the Enum list?
In my query string, I have an age variable ?age=New_Born
.
Is there a way I can check if this string value New_Born
is in my Enum list
[Flags]
public enum Age
{
New_Born = 1,
Toddler = 2,
Preschool = 4,
Kindergarten = 8
}
I could use if statement for right now, but if my Enum list gets bigger. I want to find a better way to do it. I am thinking about to use Linq, just not sure how to do it.
You can use:
Enum.IsDefined(typeof(Age), youragevariable)
You can use the Enum.TryParse method:
Age age;
if (Enum.TryParse<Age>("New_Born", out age))
{
// You now have the value in age
}