Type.Equals Method (Object)
Determines if the underlying system type of the current Type object is the same as the underlying system type of the specified Object.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- o
-
Type:
System.Object
The object whose underlying system type is to be compared with the underlying system type of the current Type. For the comparison to succeed, o must be able to be cast or converted to an object of type Type.
Return Value
Type: System.Booleantrue if the underlying system type of o is the same as the underlying system type of the current Type; otherwise, false. This method also returns false if: .
o is null.
o cannot be cast or converted to a Type object.
This method overrides Object.Equals. It casts o to an object of type Type and calls the Type.Equals(Type) method.
The following example uses Equals(Object) to compare various Type object instances with various Object instances.
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.
Two things are particularly worth noting about the example:
The comparison of a Type object that represents an integer with a TypeInfo object that represents an integer return true because TypeInfo is derived from Type.
The comparison of a Type object that represents a IList<T> object (an open generic type) with a List(Of String) object (a closed generic type) returns false.
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1