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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var