Type.Equals() Method in C#

The Type.Equals() method in C# determines if the underlying system type of the current Type is the same as the underlying system type of the specified Object or Type. This method is essential for type comparison operations in reflection scenarios.

Syntax

The Type.Equals() method has two overloads −

public virtual bool Equals(Type o);
public override bool Equals(object o);

Parameters

  • o − The object or Type whose underlying system type is to be compared with the underlying system type of the current Type.

Return Value

Returns true if the underlying system type of the specified object is the same as the underlying system type of the current Type; otherwise, false.

Using Type.Equals() with Different Types

Example

using System;

public class Demo {
   public static void Main(string[] args) {
      Type val1 = typeof(System.UInt16);
      Type val2 = typeof(System.Int32);
      Type val3 = typeof(System.UInt16);
      
      Console.WriteLine("UInt16 equals Int32? " + val1.Equals(val2));
      Console.WriteLine("UInt16 equals UInt16? " + val1.Equals(val3));
      Console.WriteLine("Using == operator: " + (val1 == val3));
   }
}

The output of the above code is −

UInt16 equals Int32? False
UInt16 equals UInt16? True
Using == operator: True

Using Type.Equals() with TypeInfo

Example

using System;
using System.Reflection;

public class Demo {
   public static void Main(string[] args) {
      Type type = typeof(String);
      Object obj = typeof(String).GetTypeInfo();
      Type type2 = obj as Type;
      
      if (type2 != null)
         Console.WriteLine("Both types are equal? " + type.Equals(type2));
      else
         Console.WriteLine("Cannot cast!");
         
      // Additional comparison
      Console.WriteLine("Direct comparison: " + type.Equals(typeof(string)));
   }
}

The output of the above code is −

Both types are equal? True
Direct comparison: True

Comparing with Object.Equals()

Example

using System;

public class Demo {
   public static void Main(string[] args) {
      Type stringType = typeof(string);
      object stringTypeAsObject = typeof(string);
      string nonTypeObject = "Hello";
      
      Console.WriteLine("Type.Equals(Type): " + stringType.Equals(stringTypeAsObject));
      Console.WriteLine("Type.Equals(string): " + stringType.Equals(nonTypeObject));
      Console.WriteLine("Type.Equals(null): " + stringType.Equals(null));
   }
}

The output of the above code is −

Type.Equals(Type): True
Type.Equals(string): False
Type.Equals(null): False

How It Works

The Type.Equals() method performs the following checks −

  • If the parameter is null, it returns false.

  • If the parameter is not a Type object, it returns false.

  • If both Type objects represent the same underlying system type, it returns true.

  • The comparison is based on the underlying system type, not reference equality.

Conclusion

The Type.Equals() method provides a reliable way to compare Type objects for equality based on their underlying system types. It handles null checks and type validation automatically, making it safer than direct reference comparisons for type comparison operations in reflection scenarios.

Updated on: 2026-03-17T07:04:35+05:30

940 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements