C# Enum IsDefined Method


The IsDefined method returns true if a given integral value, or its name as a string, is present in a specified enum.

The following is our enum −

enum Subjects { Maths, Science, English, Economics };

The above is initialized by default i.e.

Maths = 0, Science = 1, English = 2, Economics = 3

Therefore, when we will find 3 using IsDefined(), then it will return True as shown below −

Example

 Live Demo

using System;
public class Demo {
   enum Subjects { Maths, Science, English, Economics };
   public static void Main() {
      object ob;
      ob = 3;
      Console.WriteLine("{0} = {1}", ob, Enum.IsDefined(typeof(Subjects), ob));
   }
}

Output

3 = True

Updated on: 23-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements