C# | Type.Equals() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.Equals(Type) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        Type value1 = typeof(System.String);
  
        // Declaring and initializing value2
        Type value2 = typeof(System.Int32);
  
        // using Equals(Type) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}

chevron_right


Output:

System.String is not equal to System.Int32

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.Equals(Type) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling get() method
        get(typeof(System.String), typeof(System.String));
        get(typeof(System.String), typeof(System.Int32));
        get(typeof(System.Decimal), typeof(System.Double));
    }
  
    // defining get() method
    public static void get(Type value1,
                           Type value2)
    {
  
        // using Equals(Type) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.Equals(Object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        Type value1 = typeof(int);
  
        // Declaring and initializing value2
        object value2 = typeof(int);
  
        // using Equals(Object) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}

chevron_right


Output:

System.Int32 is equal to System.Int32

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.Equals(Object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(typeof(int), new Object());
        get(typeof(System.String), (object)5.5);
        get(typeof(System.String), null);
    }
  
    // defining get() method
    public static void get(Type value1,
                           object value2)
    {
  
        // using Equals(Object) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}

chevron_right


Output:

System.Int32 is not equal to System.Object
System.String is not equal to 5.5
System.String is not equal to

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.