C# Enum IsDefined

Description

Enum IsDefined returns an indication whether a constant with a specified value exists in a specified enumeration.

Syntax

Enum.IsDefined has the following syntax.


[ComVisibleAttribute(true)]/*w  w w. j av a2s .  c  o  m*/
public static bool IsDefined(
  Type enumType,
  Object value
)

Parameters

Enum.IsDefined has the following parameters.

  • enumType - An enumeration type.
  • value - The value or name of a constant in enumType.

Returns

Enum.IsDefined method returns true if a constant in enumType has a value equal to value; otherwise, false.

Example

The IsDefined method returns false when you pass it an enumeration value that has two bit fields set.


/*ww  w  .ja  v a 2s.  c om*/
using System;

[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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version