C# | Boolean.CompareTo(Boolean) Method
Last Updated :
06 Jan, 2022
Boolean.CompareTo(Boolean) Method is used to compare the current instance to a specified Boolean object and returns an indication of their relative values.
Syntax:
public int CompareTo (bool value);
Here, the value is a Boolean object to compare to the current instance.
Return Value: This method returns a 32-bit signed integer that indicates the relative order of this instance and value.
- Less than zero: If this instance is false and value is true.
- Zero: If this instance and value are equal (either both are true or both are false).
- Greater than zero: If this instance is true and value is false.
Example:
C#
using System;
class GFG {
public static void Main()
{
bool val1, val2;
val1 = true ;
val2 = false ;
int i = val2.CompareTo(val1);
if (i > 0)
Console.Write("val2 is greater than val1");
else if (i < 0)
Console.Write("val2 is less than val1");
else
Console.Write("val1 is equal to val1");
}
}
|
Output:
val2 is less than val1
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...