Double.Equals() Method in C# with Examples

Double.Equals() Method is used to get a value which indicates whether the two instances of Double represents the same value or not. There are total two methods in the overload list of this method as follows:

  • Equals(Double) Method
  • Equals(Object) Method

Double.Equals(Double)

This method is used to return a value indicating whether this instance and a specified Double object represent the same value.

Syntax: public bool Equals (double obj);
Here, it takes a Double object to compare to this instance.

Return Value: This method returns true if obj is equal to this instance; otherwise, false.

Below programs illustrate the use of Double.Equals() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Double.Equals(Double)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        double value1 = 10d;
  
        // Declaring and initializing value2
        double value2 = 20d;
  
        // compare both double value
        // using Equals(Double) 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:

10 is not equal to 20

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Double.Equals(Double)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling get() method
        get(5d, 5d);
        get(5.5d, 4.5d);
        get(10d, 20d);
        get(7.5d, 19.5d);
    }
  
    // defining get() method
    public static void get(double value1, double value2)
    {
  
        // compare both double value
        // using Equals(Double) method
        bool status = value1.Equals(value2);
  
        // cheking 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:

5 is equal to 5
5.5 is not equal to 4.5
10 is not equal to 20
7.5 is not equal to 19.5

Double.Equals(Object) Method

This method is used to returns a value indicating whether this instance is equal to a specified object or not.

Syntax: public override bool Equals (object obj);
Here, it takes an object to compare with this instance.

Return Value: This method returns true if obj is an instance of Double and equals the value of this instance; otherwise, false.

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
// Double.Equals(Object)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        double value1 = 10d;
  
        // Declaring and initializing value2
        object value2 = 1 / 36;
  
        // compare both double value
        // 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:

10 is not equal to 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Double.Equals(object)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(5d, 1 / 3);
        get(5.5d, 1 / 3);
        get(10d, 1 / 24);
        get(7.5d, 2 / 5);
    }
  
    // defining get() method
    public static void get(double value1, object value2)
    {
  
        // compare both double value
        // 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:

5 is not equal to 0
5.5 is not equal to 0
10 is not equal to 0
7.5 is not equal to 0

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.