Check if a constant exists in a specified enum in CSharp

Description

The following code shows how to check if a constant exists in a specified enum.

Example


using System;//  w  ww.  ja va  2  s .  c  om

[Flags]
public enum Pets
{
    None = 0, Dog = 1, Cat = 2, Bird = 4,
    Rodent = 8, Other = 16
};

public class Example
{
    public static void Main()
   {
      Pets value = Pets.Dog | Pets.Cat;
      Console.WriteLine(Pets.IsDefined(typeof(Pets), 1));
      string name = value.ToString();
      Console.WriteLine(Pets.IsDefined(typeof(Pets),2));
   }
}

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