Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Enum IsDefined Method
The Enum.IsDefined method in C# determines whether a specified value exists within a given enumeration. It returns true if the value is found, either as an integral value or its corresponding string name, and false otherwise.
Syntax
Following is the syntax for the Enum.IsDefined method −
public static bool IsDefined(Type enumType, object value)
Parameters
enumType − The type of the enumeration to check against.
value − The value to look for in the enumeration (can be an integer or string).
Return Value
Returns true if the specified value exists in the enumeration, otherwise false.
Using IsDefined with Integer Values
When you create an enum without explicit values, C# assigns integer values starting from 0. For example −
enum Subjects { Maths, Science, English, Economics }
// Equivalent to: Maths = 0, Science = 1, English = 2, Economics = 3
Example
using System;
public class Demo {
enum Subjects { Maths, Science, English, Economics }
public static void Main() {
Console.WriteLine("Checking integer values:");
Console.WriteLine("Value 0: " + Enum.IsDefined(typeof(Subjects), 0));
Console.WriteLine("Value 3: " + Enum.IsDefined(typeof(Subjects), 3));
Console.WriteLine("Value 5: " + Enum.IsDefined(typeof(Subjects), 5));
}
}
The output of the above code is −
Checking integer values: Value 0: True Value 3: True Value 5: False
Using IsDefined with String Names
You can also check if a string name exists in the enumeration −
Example
using System;
public class Demo {
enum Subjects { Maths, Science, English, Economics }
public static void Main() {
Console.WriteLine("Checking string names:");
Console.WriteLine("'Maths': " + Enum.IsDefined(typeof(Subjects), "Maths"));
Console.WriteLine("'Science': " + Enum.IsDefined(typeof(Subjects), "Science"));
Console.WriteLine("'History': " + Enum.IsDefined(typeof(Subjects), "History"));
}
}
The output of the above code is −
Checking string names: 'Maths': True 'Science': True 'History': False
Using IsDefined with Custom Enum Values
When an enum has custom integer values, IsDefined checks against those specific values −
Example
using System;
public class Demo {
enum Priority { Low = 10, Medium = 20, High = 30 }
public static void Main() {
Console.WriteLine("Checking custom enum values:");
Console.WriteLine("Value 10: " + Enum.IsDefined(typeof(Priority), 10));
Console.WriteLine("Value 15: " + Enum.IsDefined(typeof(Priority), 15));
Console.WriteLine("Value 20: " + Enum.IsDefined(typeof(Priority), 20));
Console.WriteLine("'Medium': " + Enum.IsDefined(typeof(Priority), "Medium"));
}
}
The output of the above code is −
Checking custom enum values: Value 10: True Value 15: False Value 20: True 'Medium': True
Common Use Cases
The IsDefined method is commonly used for input validation to ensure that a value is valid before using it with enum operations −
Example
using System;
public class Demo {
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday }
public static void ProcessDay(int dayValue) {
if (Enum.IsDefined(typeof(Day), dayValue)) {
Day selectedDay = (Day)dayValue;
Console.WriteLine("Processing day: " + selectedDay);
} else {
Console.WriteLine("Invalid day value: " + dayValue);
}
}
public static void Main() {
ProcessDay(2); // Valid
ProcessDay(7); // Invalid
}
}
The output of the above code is −
Processing day: Wednesday Invalid day value: 7
Conclusion
The Enum.IsDefined method provides a safe way to validate whether a given value or name exists within an enumeration before performing operations. It helps prevent runtime errors when converting integers to enum values or when validating user input against predefined enum constants.
