Type.Equals() Method is used to check whether the underlying system type of the current Type is the same as the underlying system type of the specified Object or Type. There are 2 methods in the overload list of this method as follows:
- Equals(Type) Method
- Equals(Object) Method
Type.Equals(Type) Method
This method is used to check whether the underlying system type of the current Type is the same as the underlying system type of the specified Type.
Syntax: public virtual bool Equals (Type o);
Here, it takes the object whose underlying system type is to be compared with the underlying system type of the current Type.
Return Value: This method returns true if the underlying system type of o is the same as the underlying system type of the current Type otherwise, it returns false.
Below programs illustrate the use of
Type.Equals() Method:
Example 1:
Output:
System.String is not equal to System.Int32
Example 2:
Output:
System.String is equal to System.String
System.String is not equal to System.Int32
System.Decimal is not equal to System.Double
Type.Equals(Object) Method
This method is used to check whether the underlying system type of the current defined
Type object is exactly same as the underlying system type of the specified Object.
Syntax: public override bool Equals (object obj);
Here, it takes the object whose underlying system type is to be compared with the underlying system type of the current Type. For the comparison to succeed, obj must be able to be cast or converted to an object of type Type.
Return Value: This method returns true if the underlying system type of obj is the same as the underlying system type of the current Type otherwise, it returns false. This method also returns false if obj is null or cannot be cast or converted to a Type object.
Below programs illustrate the use of the above-discussed method:
Example 1:
Output:
System.Int32 is equal to System.Int32
Example 2: