Skip to content
Related Articles
Open in App
Not now

Related Articles

C# Program to Check a Specified Type is an Enum or Not

Improve Article
Save Article
  • Last Updated : 28 Nov, 2021
Improve Article
Save Article

Enum or also known as Enumeration is used to store user define data. It is used to assign the string value to an integral constant which makes programs easy to read and manage. We can create enum data using the enum keyword followed by the enum name. In C#, we can check the specific type is enum or not by using the IsEnum property of the Type class. It will return true if the type is enum. Otherwise, this property will return false. It is a read-only property.

Syntax:

public bool IsEnum { get; }

Return Type: The return type of this property is boolean. It will return either true or false.

Example 1:

C#




// C# program to check whether the
// given type is enum or not
using System;
using System.Reflection;
  
// Declare a enum type with subjects
enum Subject
{
    Java, Python, Php, Html
}
  
class GFG{
  
// Driver code  
public static void Main(string[] args)
{
      
    // Check the given type is a enum or not
    // Using IsEnum property
    Console.WriteLine(typeof(Subject).IsEnum);
}
}

Output:

True

Example 2: 

C#




// C# program to check whether the
// given type is enum or not
using System;
using System.Reflection;
  
enum courses
{
    DSA, ReactJS, OperatingSystem, DBMS
}
  
class Branch
{
    void display()
    {
        Console.WriteLine("Name of the branch");
    }
}
  
struct subject
{
    string name;
    int marks;
}
  
class GFG{
  
// Driver code  
public static void Main(string[] args)
{
      
    // Check the given type is a enum or not
    // Using IsEnum property
    bool res1 = typeof(courses).IsEnum;
    bool res2 = typeof(Branch).IsEnum;
    bool res3 = typeof(subject).IsEnum;
  
    Console.WriteLine("Is courses is enum?: " + res1);
    Console.WriteLine("Is Branch is enum?: " + res2);
    Console.WriteLine("Is subject is enum?: " + res3);
}
}

Output:

Is courses is enum?: True
Is Branch is enum?: False
Is subject is enum?: False

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!