7

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

 11 months ago
source link: https://www.geeksforgeeks.org/c-sharp-program-to-check-a-specified-type-is-an-enum-or-not/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

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# 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 whether the given type is a enum or not
// Using IsEnum property
Console.WriteLine(typeof(Subject).IsEnum);
}
}

Output:

True

Example 2: 

// 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 whether 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




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK