Leggi in inglese

Condividi tramite


Enum.IsDefined Metodo

Definizione

Overload

IsDefined(Type, Object)

Restituisce un valore booleano se in un'enumerazione specificata esiste un determinato valore integrale o il relativo nome sotto forma di stringa.

IsDefined<TEnum>(TEnum)

Restituisce un valore booleano se in un'enumerazione specificata esiste un determinato valore integrale o il relativo nome sotto forma di stringa.

IsDefined(Type, Object)

Origine:
Enum.cs
Origine:
Enum.cs
Origine:
Enum.cs

Restituisce un valore booleano se in un'enumerazione specificata esiste un determinato valore integrale o il relativo nome sotto forma di stringa.

C#
public static bool IsDefined(Type enumType, object value);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static bool IsDefined(Type enumType, object value);

Parametri

enumType
Type

Tipo di enumerazione.

value
Object

Valore o nome di una costante in enumType.

Restituisce

true se una costante in enumType ha un valore uguale a value; in caso contrario, false.

Attributi

Eccezioni

enumType o value è null.

enumType non è un elemento Enum.

-oppure-

Il tipo di value è un'enumerazione, ma non è un'enumerazione di tipo enumType.

-oppure-

Il tipo di value non è un tipo sottostante di enumType.

Esempio

Nell'esempio seguente viene definita un'enumerazione denominata PetType costituita da singoli campi bit. Chiama quindi il metodo con possibili valori di enumerazione sottostanti, nomi di stringa e valori compositi che derivano dall'impostazione IsDefined di più campi bit.

C#
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

Commenti

Il value parametro può essere uno dei seguenti:

  • Qualsiasi membro di tipo enumType.

  • Variabile il cui valore è un membro di enumerazione di tipo enumType.

  • Rappresentazione stringa del nome di un membro di enumerazione. I caratteri della stringa devono avere lo stesso caso del nome del membro di enumerazione.

  • Valore del tipo sottostante di enumType.

Se le costanti in enumType definiscono un set di campi bit e value contengono i valori, i nomi o i valori sottostanti di più campi bit, il IsDefined metodo restituisce false. In altre parole, per le enumerazioni che definiscono un set di campi di bit, il metodo determina solo se un singolo campo bit appartiene all'enumerazione. Per determinare se più campi bit sono impostati in un tipo di enumerazione contrassegnato con l'attributo, è possibile chiamare il FlagsAttributeHasFlag metodo.

Note per i chiamanti

Se enumType è un'enumerazione definita usando l'attributo FlagsAttribute , il metodo restituisce false se sono impostati più campi value bit, ma value non corrispondono a un valore di enumerazione composita o se value è una concatenazione stringa dei nomi di più flag di bit. Nell'esempio seguente viene definita un'enumerazione Pets con l'attributo FlagsAttribute . Il IsDefined(Type, Object) metodo restituisce false quando si passa un valore di enumerazione con due campi bit (e Pets.Cat) impostati e quando si passa la rappresentazione stringa del valore di enumerazione ("Pets.DogDog, Cat").

C#
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

È possibile determinare se sono impostati più campi bit chiamando il HasFlag(Enum) metodo .

Vedi anche

Si applica a

.NET 10 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

IsDefined<TEnum>(TEnum)

Origine:
Enum.cs
Origine:
Enum.cs
Origine:
Enum.cs

Restituisce un valore booleano se in un'enumerazione specificata esiste un determinato valore integrale o il relativo nome sotto forma di stringa.

C#
public static bool IsDefined<TEnum>(TEnum value) where TEnum : struct;

Parametri di tipo

TEnum

Tipo dell'enumerazione.

Parametri

value
TEnum

Valore o nome di una costante in TEnum.

Restituisce

true se in un'enumerazione specificata esiste un determinato valore integrale o il relativo nome sotto forma di stringa. In caso contrario, false.

Si applica a

.NET 10 e altre versioni
Prodotto Versioni
.NET 5, 6, 7, 8, 9, 10