英語で読む

次の方法で共有


Type.Equals メソッド

定義

現在の Type の基になるシステム型が、指定した Object または Type の基になるシステム型と同じかどうかを判断します。

オーバーロード

Equals(Type)

現在の Type の基になるシステム型が、指定した Type の基になるシステム型と同じかどうかを判断します。

Equals(Object)

現在の Type オブジェクトの基になるシステム型が、指定した Object の基になるシステム型と同じかどうかを判断します。

Equals(Type)

現在の Type の基になるシステム型が、指定した Type の基になるシステム型と同じかどうかを判断します。

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

パラメーター

o
Type

基になるシステム型が、現在の Type の基になるシステム型との比較対象になるオブジェクト。

戻り値

Boolean

o の基になるシステム型が現在の Type の基になるシステム型と同じである場合は true。それ以外の場合は false

実装

次の例では、 を使用 Equals して 2 つの型を比較します。

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

こちらもご覧ください

適用対象

.NET 7 およびその他のバージョン
製品 バージョン
.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
.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
.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)

現在の Type オブジェクトの基になるシステム型が、指定した Object の基になるシステム型と同じかどうかを判断します。

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

パラメーター

o
Object

基になるシステム型が、現在の Type の基になるシステム型との比較対象になるオブジェクト。 比較を成功させるには、o をキャストまたは型 Type のオブジェクトに変換できる必要があります。

戻り値

Boolean

o の基になるシステム型が現在の Type の基になるシステム型と同じである場合は true。それ以外の場合は false。 次の場合にも、このメソッドは false を返します。

  • onull です。

  • o をキャストまたは Type オブジェクトに変換できない。

実装

次の例では、 を Equals(Object) 使用して、さまざまなオブジェクト インスタンス Type とさまざまなインスタンス Object を比較します。

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.

この例では、特に次の 2 つの点に注意してください。

  • が から派生したため、整数を表す オブジェクトと整数の戻り値を表す オブジェクト Type TypeInfo true TypeInfo の比較 Type

  • オブジェクト (オープン ジェネリック型) を表す オブジェクトとオブジェクト (閉じたジェネリック型) の比較 Type IList<T> List(Of String) では、 が返されます false

注釈

このメソッドは、Object.Equals をオーバーライドします。 型のオブジェクト o にキャストし、 Type メソッドを呼び出 Type.Equals(Type) します。

こちらもご覧ください

適用対象

.NET 7 およびその他のバージョン
製品 バージョン
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0