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.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Overrides Function Equals (
	o As Object
) As Boolean

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.Boolean

true 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.

Imports System.Collections.Generic
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim t As Type = GetType(Integer)
      Dim obj1 As Object = GetType(Integer).GetTypeInfo()
      IsEqualTo(t, obj1)

      Dim obj2 As Object = GetType(String)
      IsEqualTo(t, obj2)

      t = GetType(Object)
      Dim obj3 As Object = GetType(Object)
      IsEqualTo(t, obj3)

      t = GetType(List(Of ))
      Dim obj4 As Object = (New List(Of String())).GetType()
      IsEqualTo(t, obj4)

      t = GetType(Type)
      Dim obj5 As Object = Nothing
      IsEqualTo(t, obj5)
   End Sub

   Private Sub IsEqualTo(t As Type, inst As Object)
      Dim t2 As Type = TryCast(inst, Type)
      If t2 IsNot Nothing Then
         Console.WriteLine("{0} = {1}: {2}", t.Name, t2.Name,
                           t.Equals(t2))
      Else
         Console.WriteLine("Cannot cast the argument to a type.")
      End If
      Console.WriteLine()                        
   End Sub
End Module
' 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(Of T) object (an open generic type) with a List(Of String) object (a closed generic type) returns false.

Universal Windows Platform
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
Return to top
Show: