Check if a value is defined as enum number in CSharp
Description
The following code shows how to check if a value is defined as enum number.
Example
using System;// ww w.j av a2s . c o m
enum Color
{
red,
green,
yellow
}
public class ClassMain
{
public static void Main()
{
Color c = Color.red;
// see if a specific value is a defined enum member
bool defined = Enum.IsDefined(typeof(Color), 5);
Console.WriteLine("5 is a defined value for Color: {0}", defined);
}
}
The code above generates the following result.