Ler en inglés

Compartir por


Type.Equals Método

Definición

Determina si el tipo del sistema subyacente del objeto Type actual coincide con el tipo del sistema subyacente del objeto Object o Type especificado.

Sobrecargas

Equals(Type)

Determina si el tipo de sistema subyacente del objeto Type actual es igual que el tipo de sistema subyacente del objeto Type especificado.

Equals(Object)

Determina si el tipo del sistema subyacente del objeto Type actual es el mismo que el tipo del sistema subyacente del objeto Object especificado.

Equals(Type)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Determina si el tipo de sistema subyacente del objeto Type actual es igual que el tipo de sistema subyacente del objeto Type especificado.

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

Parámetros

o
Type

Objeto cuyo tipo de sistema subyacente se va a comparar con el tipo de sistema subyacente del objeto Type actual.

Devoluciones

Es true si el tipo del sistema subyacente de o coincide con el tipo del sistema subyacente del objeto Type actual; de lo contrario, es false.

Implementaciones

Ejemplos

En el ejemplo siguiente se usa Equals para comparar dos tipos.

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
*/

Consulte también

Se aplica a

.NET 9 e outras versións
Produto Versións
.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)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Determina si el tipo del sistema subyacente del objeto Type actual es el mismo que el tipo del sistema subyacente del objeto Object especificado.

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

Parámetros

o
Object

Objeto cuyo tipo de sistema subyacente se va a comparar con el tipo de sistema subyacente del objeto Type actual. Para que la comparación se realice correctamente, o debe ser capaz de convertir o convertir en un objeto de tipo Type.

Devoluciones

Es true si el tipo del sistema subyacente de o coincide con el tipo del sistema subyacente del objeto Type actual; de lo contrario, es false. Este método también devuelve false si:

  • o es null.

  • o no se puede convertir en un objeto Type.

Implementaciones

Ejemplos

En el ejemplo siguiente se usa Equals(Object) para comparar varias Type instancias de objeto con varias Object instancias.

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.

Cabe destacar dos cosas especialmente importantes sobre el ejemplo:

  • Comparación de un Type objeto que representa un entero con un TypeInfo objeto que representa un valor devuelto true entero porque TypeInfo se deriva de Type.

  • La comparación de un Type objeto que representa un objeto (un IList<T> tipo genérico abierto) con un List(Of String) objeto (un tipo genérico cerrado) devuelve false.

Comentarios

Este método invalida Object.Equals. Se convierte o en un objeto de tipo Type y llama al Type.Equals(Type) método .

Consulte también

Se aplica a

.NET 9 e outras versións
Produto Versións
.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