Double.Equals() Method in C# with Examples


The Double.Equals() method in C# is used to return a value indicating whether two instances of Double represent the same value.

Syntax

The syntax is as follows −

public bool Equals (double obj);
public override bool Equals (object ob);

The obj parameter of the first syntax is a Double object to compare to this instance, whereas the obj of the second parameter is an object to compare with this instance.

Example

Let us now see an example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      double d1 = 150d;
      double d2 = 150d;
      Console.WriteLine("Double1 Value = "+d1);
      Console.WriteLine("Double2 Value = "+d2);
      Console.WriteLine("Are both the double values equal? = "+d1.Equals(d2));
   }
}

Output

This will produce the following output −

Double1 Value = 150
Double2 Value = 150
Are both the double values equal? = True

Example

Let us now see another example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      double d1 = 150d;
      object ob1 = 1/2;
      Console.WriteLine("Double1 Value = "+d1);
      Console.WriteLine("Object Value = "+ob1);
      Console.WriteLine("Are both the values equal? = "+d1.Equals(ob1));
   }
}

Output

This will produce the following output −

Double1 Value = 150
Object Value = 0
Are both the values equal? = False

Updated on: 04-Dec-2019

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements