Edita

Comparteix a través de


Enum.IsDefined Method

Definition

Overloads

IsDefined(Type, Object)

Returns a Boolean telling whether a given integral value, or its name as a string, exists in a specified enumeration.

IsDefined<TEnum>(TEnum)

Returns a boolean telling whether a given integral value, or its name as a string, exists in a specified enumeration.

IsDefined(Type, Object)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Returns a Boolean telling whether a given integral value, or its name as a string, exists in a specified enumeration.

public:
 static bool IsDefined(Type ^ enumType, System::Object ^ value);
public static bool IsDefined (Type enumType, object value);
[System.Runtime.InteropServices.ComVisible(true)]
public static bool IsDefined (Type enumType, object value);
static member IsDefined : Type * obj -> bool
[<System.Runtime.InteropServices.ComVisible(true)>]
static member IsDefined : Type * obj -> bool
Public Shared Function IsDefined (enumType As Type, value As Object) As Boolean

Parameters

enumType
Type

An enumeration type.

value
Object

The value or name of a constant in enumType.

Returns

true if a constant in enumType has a value equal to value; otherwise, false.

Attributes

Exceptions

enumType or value is null.

enumType is not an Enum.

-or-

The type of value is an enumeration, but it is not an enumeration of type enumType.

-or-

The type of value is not an underlying type of enumType.

Examples

The following example defines an enumeration named PetType that consists of individual bit fields. It then calls the IsDefined method with possible underlying enumeration values, string names, and composite values that result from setting multiple bit fields.

using System;

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

public class Example
{
   public static void Main()
   {
      object value;

      // Call IsDefined with underlying integral value of member.
      value = 1;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with invalid underlying integral value.
      value = 64;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with string containing member name.
      value = "Rodent";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with a variable of type PetType.
      value = PetType.Dog;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = PetType.Dog | PetType.Cat;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with uppercase member name.
      value = "None";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = "NONE";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with combined value
      value = PetType.Dog | PetType.Bird;
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = value.ToString();
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
   }
}
// The example displays the following output:
//       1: True
//       64: False
//       Rodent: True
//       Dog: True
//       Dog, Cat: False
//       None: True
//       NONE: False
//       9: False
//       Dog, Bird: False
open System

[<Flags>]
type PetType =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Rodent = 4
    | Bird = 8
    | Reptile = 16
    | Other = 32

[<EntryPoint>]
let main _ =
    // Call IsDefined with underlying integral value of member.
    let value = 1
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with invalid underlying integral value.
    let value = 64
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with string containing member name.
    let value = "Rodent"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with a variable of type PetType.
    let value = PetType.Dog
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = PetType.Dog ||| PetType.Cat
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with uppercase member name.
    let value = "None"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = "NONE"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with combined value
    let value = PetType.Dog ||| PetType.Bird
    printfn $"{value:D}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = value.ToString()
    printfn $"{value:D}: {Enum.IsDefined(typeof<PetType>, value)}"
    0
// The example displays the following output:
//       1: True
//       64: False
//       Rodent: True
//       Dog: True
//       Dog, Cat: False
//       None: True
//       NONE: False
//       9: False
//       Dog, Bird: False
<Flags> Public Enum PetType As Integer
   None = 0
   Dog = 1
   Cat = 2
   Rodent = 4
   Bird = 8
   Reptile = 16
   Other = 32
End Enum

Module Example
   Public Sub Main()
      Dim value As Object
      
      ' Call IsDefined with underlying integral value of member.
      value = 1
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with invalid underlying integral value.
      value = 64
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with string containing member name.
      value = "Rodent"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with a variable of type PetType.
      value = PetType.Dog
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = PetType.Dog Or PetType.Cat
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with uppercase member name.      
      value = "None"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = "NONE"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with combined value
      value = PetType.Dog Or PetType.Bird
      Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = value.ToString()
      Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
   End Sub
End Module
' The example displays the following output:
'       1: True
'       64: False 
'       Rodent: True
'       Dog: True
'       Dog, Cat: False
'       None: True
'       NONE: False
'       9: False
'       Dog, Bird: False

Remarks

The value parameter can be any of the following:

  • Any member of type enumType.

  • A variable whose value is an enumeration member of type enumType.

  • The string representation of the name of an enumeration member. The characters in the string must have the same case as the enumeration member name.

  • A value of the underlying type of enumType.

If the constants in enumType define a set of bit fields and value contains the values, names, or underlying values of multiple bit fields, the IsDefined method returns false. In other words, for enumerations that define a set of bit fields, the method determines only whether a single bit field belongs to the enumeration. To determine whether multiple bit fields are set in an enumeration type that is tagged with the FlagsAttribute attribute, you can call the HasFlag method.

Notes to Callers

If enumType is an enumeration that is defined by using the FlagsAttribute attribute, the method returns false if multiple bit fields in value are set but value does not correspond to a composite enumeration value, or if value is a string concatenation of the names of multiple bit flags. In the following example, a Pets enumeration is defined with the FlagsAttribute attribute. The IsDefined(Type, Object) method returns false when you pass it an enumeration value that has two bit fields (Pets.Dog and Pets.Cat) set, and when you pass it the string representation of that enumeration value ("Dog, Cat").

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("{0:D} Exists: {1}",
                        value, Pets.IsDefined(typeof(Pets), value));
      string name = value.ToString();
      Console.WriteLine("{0} Exists: {1}",
                        name, Pets.IsDefined(typeof(Pets), name));
   }
}
// The example displays the following output:
//       3 Exists: False
//       Dog, Cat Exists: False
open System

[<Flags>]
type Pets =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Bird = 4
    | Rodent = 8
    | Other = 16

let value = Pets.Dog ||| Pets.Cat
printfn $"{value:D} Exists: {Pets.IsDefined(typeof<Pets>, value)}"
let name = string value
printfn $"{name} Exists: {Pets.IsDefined(typeof<Pets>, name)}"
// The example displays the following output:
//       3 Exists: False
//       Dog, Cat Exists: False
<Flags> Public Enum Pets As Integer
   None = 0
   Dog = 1
   Cat = 2
   Bird = 4
   Rodent = 8
   Other = 16
End Enum

Module Example
   Public Sub Main()
      Dim value As Pets = Pets.Dog Or Pets.Cat
      Console.WriteLine("{0:D} Exists: {1}", 
                        value, Pets.IsDefined(GetType(Pets), value))
      Dim name As String = value.ToString()
      Console.WriteLine("{0} Exists: {1}", 
                        name, Pets.IsDefined(GetType(Pets), name))
   End Sub
End Module
' The example displays the following output:
'       3 Exists: False
'       Dog, Cat Exists: False

You can determine whether multiple bit fields are set by calling the HasFlag(Enum) method.

See also

Applies to

IsDefined<TEnum>(TEnum)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Returns a boolean telling whether a given integral value, or its name as a string, exists in a specified enumeration.

public:
generic <typename TEnum>
 where TEnum : value class static bool IsDefined(TEnum value);
public static bool IsDefined<TEnum> (TEnum value) where TEnum : struct;
static member IsDefined : 'Enum -> bool (requires 'Enum : struct)
Public Shared Function IsDefined(Of TEnum As Structure) (value As TEnum) As Boolean

Type Parameters

TEnum

The type of the enumeration.

Parameters

value
TEnum

The value or name of a constant in TEnum.

Returns

true if a given integral value, or its name as a string, exists in a specified enumeration; false otherwise.

Applies to