Is enum value defined

Enum has a static method we can use to check if an enum value is defined.


using System;

public enum WeekDay{

   Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

}

class Test
{
    static void Main()
    {
        WeekDay day = (WeekDay)111;

        if (Enum.IsDefined(day.GetType(),day))
        {
            Console.WriteLine("defined");
        }
        else
        {
            Console.WriteLine("not defined");

        }

    }
}

The output:


not defined
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.