Leggi in inglese

Condividi tramite


Type.Equals Metodo

Definizione

Determina se il tipo di sistema sottostante dell'oggetto Type corrente coincide con il tipo di sistema sottostante di Object o dell'oggetto Type specificato.

Overload

Equals(Type)

Determina se il tipo di sistema sottostante dell'oggetto Type corrente coincide con il tipo di sistema sottostante dell'oggetto Type specificato.

Equals(Object)

Determina se il tipo di sistema sottostante dell'oggetto Type corrente coincide con il tipo di sistema sottostante dell'oggetto Object specificato.

Equals(Type)

Origine:
Type.cs
Origine:
Type.cs
Origine:
Type.cs

Determina se il tipo di sistema sottostante dell'oggetto Type corrente coincide con il tipo di sistema sottostante dell'oggetto Type specificato.

C#
public bool Equals (Type o);
C#
public virtual bool Equals (Type? o);
C#
public virtual bool Equals (Type o);

Parametri

o
Type

Oggetto il cui tipo di sistema sottostante deve essere confrontato con il tipo di sistema sottostante dell'oggetto Type corrente.

Restituisce

true se il tipo di sistema sottostante di o coincide con il tipo di sistema sottostante dell'oggetto Type corrente; in caso contrario, false.

Implementazioni

Esempio

Nell'esempio seguente viene usato Equals per confrontare due tipi.

C#

using System;
using System.Reflection;

class Example
{
    public static void Main()
    {

        Type a = typeof(System.String);
        Type b = typeof(System.Int32);

        Console.WriteLine("{0} == {1}: {2}", a, b, a.Equals(b));

        // The Type objects in a and b are not equal,
        // because they represent different types.

        a = typeof(Example);
        b = new Example().GetType();

        Console.WriteLine("{0} is equal to {1}: {2}", a, b, a.Equals(b));

        // The Type objects in a and b are equal,
        // because they both represent type Example.

        b = typeof(Type);

        Console.WriteLine("typeof({0}).Equals(typeof({1})): {2}", a, b, a.Equals(b));

        // The Type objects in a and b are not equal,
        // because variable a represents type Example
        // and variable b represents type Type.

        //Console.ReadLine();
    }
}

//
/* This code example produces the following output:
    System.String == System.Int32: False
    Example is equal to Example: True
    typeof(Example).Equals(typeof(System.Type)): False
*/

Vedi anche

Si applica a

.NET 9 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
.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

Equals(Object)

Origine:
Type.cs
Origine:
Type.cs
Origine:
Type.cs

Determina se il tipo di sistema sottostante dell'oggetto Type corrente coincide con il tipo di sistema sottostante dell'oggetto Object specificato.

C#
public override bool Equals (object o);
C#
public override bool Equals (object? o);

Parametri

o
Object

Oggetto il cui tipo di sistema sottostante deve essere confrontato con il tipo di sistema sottostante dell'oggetto Type corrente. Affinché il confronto abbia esito positivo, o deve essere in grado di eseguire il cast o la conversione in un oggetto di tipo Type.

Restituisce

true se il tipo di sistema sottostante di o coincide con il tipo di sistema sottostante dell'oggetto Type corrente; in caso contrario, false. Questo metodo restituisce false anche quando:

  • o è null.

  • Impossibile eseguire il cast o la conversione di o in un oggetto Type.

Implementazioni

Esempio

Nell'esempio seguente viene usato Equals(Object) per confrontare diverse Type istanze di oggetti con diverse Object istanze.

C#
using System;
using System.Collections.Generic;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      Type t =typeof(int);
      Object obj1 = typeof(int).GetTypeInfo();
      IsEqualTo(t, obj1);

      Object obj2 = typeof(String);
      IsEqualTo(t, obj2);
      
      t = typeof(Object);
      Object obj3 = typeof(Object);
      IsEqualTo(t, obj3);
      
      t = typeof(List<>);
      Object obj4 = (new List<String>()).GetType();
      IsEqualTo(t, obj4);
      
      t = typeof(Type);
      Object obj5 = null;
      IsEqualTo(t, obj5);
   }
   
   private static void IsEqualTo(Type t, Object inst)
   {
      Type t2 = inst as Type;
      if (t2 != null)
         Console.WriteLine("{0} = {1}: {2}", t.Name, t2.Name,
                           t.Equals(t2));
      else
         Console.WriteLine("Cannot cast the argument to a type.");

      Console.WriteLine();                        
   }
}
// The example displays the following output:
//       Int32 = Int32: True
//       
//       Int32 = String: False
//       
//       Object = Object: True
//       
//       List`1 = List`1: False
//       
//       Cannot cast the argument to a type.

Due aspetti vale particolarmente la pena notare sull'esempio:

  • Confronto di un Type oggetto che rappresenta un numero intero con un oggetto che rappresenta un TypeInfo valore integer restituito true perché TypeInfo è derivato da Type.

  • Il confronto di un Type oggetto che rappresenta un IList<T> oggetto (un tipo generico aperto) con un List(Of String) oggetto (tipo generico chiuso) restituisce false.

Commenti

Questo metodo esegue l'override di Object.Equals. Esegue il cast o a un oggetto di tipo Type e chiama il Type.Equals(Type) metodo .

Vedi anche

Si applica a

.NET 9 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
.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