Home »
.Net »
C# Programs
C# - Check a Specified Type is an Enum or Not?
Learn, how to check a specified type is an enum or not in C#?
Submitted by Nidhi, on October 28, 2020 [Last updated : March 22, 2023]
Checking a type is an enum or not
Here, we will check a specified type is an enum or not using IsEnum of Type class.
C# program to check a specified type is an enum or not
The source code to check a specified type is an enum or not is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to check a specified type
//is an enum or not.
using System;
using System.Reflection;
enum Colors
{ RED,GREEN,BLUE,WHITE,YELLOW,BLACK}
class Program
{
static void Main()
{
Type type = typeof(Colors);
if (type.IsEnum == true)
{
Console.WriteLine("Colors is an enum");
}
else
{
Console.WriteLine("Colors is not an enum");
}
}
}
Output
Colors is an enum
Press any key to continue . . .
Explanation
In the above program, we created an enum Colors and a class Program. The Colors enum contains the constants for colors.
The Program class is a non-abstract class that contains the Main() method. The Main() method is the entry point for the program. Here we check the specified type is an enum or not using the IsEnum property of Type class and printed the appropriate message on the console screen.
C# Basic Programs »