Fun with Enum

Today’s fun snippet, which I likely already knew and forgot:

If you want to see if a value is contained in your defined enum, this will do the trick.

if(Enum.IsDefined(typeof(yourEnum), someValue))
{    
    // do stuff
}
else
{
    // oh no, probably log an error or throw an exception
    // or show some validation message
    // your use case will vary
}

msdn for reference

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.