C# | Type.IsEnumDefined() Method

Type.IsEnumDefined(Object) Method is used to return a value which indicates whether the specified value exists in the current enumeration type.

Syntax: public virtual bool IsEnumDefined (object value);
Here, it takes the value to be tested.

Return Value: This method returns true if the specified value is a member of the current enumeration type otherwise, false.



Exceptions:

  • ArgumentException: If the current type is not an enumeration.
  • ArgumentNullException: If the value is null.
  • InvalidOperationException: If the value is of a type that cannot be the underlying type of an enumeration.

Below programs illustrate the use of Type.IsEnumDefined(Object) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.IsEnumDefined(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // defining enum enu
    public enum enu {A, B, C};
  
    // Main Method
    public static void Main()
    {
  
        // try-catch block for handling Exception
        try {
  
            // creating and initializing object Type
            object value = enu.A;
  
            // checking for the current enumeration type
            // by using IsEnumDefined() Method
            bool status = typeof(enu).IsEnumDefined(value);
  
            // display the result
            if (status)
                Console.WriteLine("specified value is a member"+
                            " of the current enumeration type");
            else
                Console.WriteLine("specified value is not"+
                " present in the current enumeration type");
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) {
  
            Console.WriteLine("Object is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        // catch ArgumentNullException here
        catch (InvalidOperationException e) {
  
            Console.WriteLine("Object is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        // catch ArgumentException here
        catch (ArgumentException e) {
  
            Console.WriteLine("Object is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

specified value is a member of the current enumeration type

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.IsEnumDefined(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // defining enum enu
    public enum enu {A, B, C};
  
    // Main Method
    public static void Main()
    {
  
        // try-catch block for handling Exception
        try {
  
            // creating and initializing object Type
            object value = enu.B;
  
            // checking for the current enumeration type
            // by using IsEnumDefined() Method
            bool status = typeof(int).IsEnumDefined(value);
  
            // display the result
            if (status)
                Console.WriteLine("specified value is a member"+
                            " of the current enumeration type");
            else
                Console.WriteLine("specified value is not"+
               " present in the current enumeration type");
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) {
  
            Console.WriteLine("value is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        // catch ArgumentException here
        catch (ArgumentException e) {
  
            Console.WriteLine("The current type is not an enumeration.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

The current type is not an enumeration.
Exception Thrown: System.ArgumentException

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.IsEnumDefined(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // defining enum enu
    public enum enu {A, B, C};
  
    // Main Method
    public static void Main()
    {
  
        // try-catch block for handling Exception
        try {
  
            // creating and initializing object Type
            object value = null;
  
            // checking for the current enumeration type
            // by using IsEnumDefined() Method
            bool status = typeof(enu).IsEnumDefined(value);
  
            // display the result
            if (status)
                Console.WriteLine("specified value is a member"+
                            " of the current enumeration type");
            else
                Console.WriteLine("specified value is not"
                " present in the current enumeration type");
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) {
  
            Console.WriteLine("value is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        // catch ArgumentException here
        catch (ArgumentException e) {
  
            Console.WriteLine("The current type is not an enumeration.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

value is null.
Exception Thrown: System.ArgumentNullException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.