Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Double.Equals() Method in C# with Examples
The Double.Equals() method in C# is used to determine whether two instances of Double represent the same value. This method provides a reliable way to compare double values and handles special cases like NaN (Not a Number) values correctly.
Syntax
The Double.Equals() method has two overloads −
public bool Equals(double obj); public override bool Equals(object obj);
Parameters
obj (first overload) − A
Doublevalue to compare to the current instance.obj (second overload) − An
objectto compare with the current instance.
Return Value
Returns true if the specified value is equal to the current instance; otherwise, false. For the object overload, it returns true only if the object is a Double and represents the same value.
Using Double.Equals() with Double Values
Example
using System;
public class Demo {
public static void Main() {
double d1 = 150.0;
double d2 = 150.0;
double d3 = 150.1;
Console.WriteLine("Double1 Value = " + d1);
Console.WriteLine("Double2 Value = " + d2);
Console.WriteLine("Double3 Value = " + d3);
Console.WriteLine("d1.Equals(d2) = " + d1.Equals(d2));
Console.WriteLine("d1.Equals(d3) = " + d1.Equals(d3));
}
}
The output of the above code is −
Double1 Value = 150 Double2 Value = 150 Double3 Value = 150.1 d1.Equals(d2) = True d1.Equals(d3) = False
Using Double.Equals() with Object Parameter
Example
using System;
public class Demo {
public static void Main() {
double d1 = 150.0;
object obj1 = 150.0; // Boxing a double
object obj2 = 150; // Boxing an int
object obj3 = "150"; // String object
Console.WriteLine("Double Value = " + d1);
Console.WriteLine("Object1 (double) = " + obj1);
Console.WriteLine("Object2 (int) = " + obj2);
Console.WriteLine("Object3 (string) = " + obj3);
Console.WriteLine("d1.Equals(obj1) = " + d1.Equals(obj1));
Console.WriteLine("d1.Equals(obj2) = " + d1.Equals(obj2));
Console.WriteLine("d1.Equals(obj3) = " + d1.Equals(obj3));
}
}
The output of the above code is −
Double Value = 150 Object1 (double) = 150 Object2 (int) = 150 Object3 (string) = 150 d1.Equals(obj1) = True d1.Equals(obj2) = False d1.Equals(obj3) = False
Handling Special Values
The Double.Equals() method correctly handles special double values like NaN, PositiveInfinity, and NegativeInfinity −
Example
using System;
public class Demo {
public static void Main() {
double nan1 = Double.NaN;
double nan2 = Double.NaN;
double posInf = Double.PositiveInfinity;
double negInf = Double.NegativeInfinity;
Console.WriteLine("NaN equals NaN: " + nan1.Equals(nan2));
Console.WriteLine("PositiveInfinity equals PositiveInfinity: " +
Double.PositiveInfinity.Equals(posInf));
Console.WriteLine("PositiveInfinity equals NegativeInfinity: " +
posInf.Equals(negInf));
}
}
The output of the above code is −
NaN equals NaN: True PositiveInfinity equals PositiveInfinity: True PositiveInfinity equals NegativeInfinity: False
Double.Equals() vs == Operator
| Double.Equals() | == Operator |
|---|---|
NaN.Equals(NaN) returns true
|
NaN == NaN returns false
|
Can compare with object parameter |
Only compares numeric types |
| Method call with type checking | Direct operator comparison |
Conclusion
The Double.Equals() method provides a robust way to compare double values, especially when dealing with special values like NaN. It offers type-safe comparison and handles object comparisons by checking the type before comparing values, making it more reliable than the equality operator in certain scenarios.
