how to check if string value is in the Enum list?

qinking126 picture qinking126 · May 29, 2012 · Viewed 97.1k times · Source

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.

Answer

AaronS picture AaronS · May 29, 2012

You can use:

 Enum.IsDefined(typeof(Age), youragevariable)