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
Boolean.Equals(Boolean) Method in C#
The Boolean.Equals(Boolean) method in C# returns a value indicating whether this instance is equal to a specified Boolean object. This method compares the current Boolean instance with another Boolean value and returns true if they are equal, otherwise false.
Syntax
Following is the syntax −
public bool Equals(bool obj);
Parameters
obj − A Boolean value to compare to this instance.
Return Value
This method returns true if obj has the same value as this instance; otherwise, false.
Example
Let us see an example to implement the Boolean.Equals(Boolean) method −
using System;
public class Demo {
public static void Main() {
bool b1 = false;
bool b2 = true;
bool res = b1.Equals(b2);
if (res == true)
Console.WriteLine("b1 equal to b2");
else
Console.WriteLine("b1 not equal to b2");
}
}
The output of the above code is −
b1 not equal to b2
Using Equals() with Same Values
Here's another example showing the method when comparing equal Boolean values −
using System;
public class Demo {
public static void Main() {
bool b1 = true;
bool b2 = true;
bool b3 = false;
bool b4 = false;
Console.WriteLine("b1.Equals(b2): " + b1.Equals(b2));
Console.WriteLine("b3.Equals(b4): " + b3.Equals(b4));
Console.WriteLine("b1.Equals(b3): " + b1.Equals(b3));
}
}
The output of the above code is −
b1.Equals(b2): True b3.Equals(b4): True b1.Equals(b3): False
Comparison with == Operator
The Equals() method produces the same result as the equality operator (==) for Boolean values −
using System;
public class Demo {
public static void Main() {
bool x = true;
bool y = false;
Console.WriteLine("Using Equals(): " + x.Equals(y));
Console.WriteLine("Using == operator: " + (x == y));
Console.WriteLine("Using Equals(): " + x.Equals(true));
Console.WriteLine("Using == operator: " + (x == true));
}
}
The output of the above code is −
Using Equals(): False Using == operator: False Using Equals(): True Using == operator: True
Conclusion
The Boolean.Equals(Boolean) method provides a straightforward way to compare Boolean values. While it functions identically to the == operator for Boolean comparisons, it follows the standard Equals() pattern used across .NET types for consistency in object-oriented programming.
