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#
using System;
using System.Reflection;
enum Subject
{
Java, Python, Php, Html
}
class GFG{
public static void Main( string [] args)
{
Console.WriteLine( typeof (Subject).IsEnum);
}
}
|
Output:
True
Example 2:
C#
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{
public static void Main( string [] args)
{
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