C# Enum IsDefined Method

CsharpProgrammingServer Side Programming

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
raja
Published on 11-Sep-2018 12:44:14
Advertisements