C# | Boolean.CompareTo(Boolean) Method
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# program to demonstrate // Boolean.CompareTo(Boolean) // Method using System; class GFG { // Main Method public static void Main() { // Declaring val1 and val2 bool val1, val2; // intializing the val1, // and val2 val1 = true; val2 = false; // using CompareTo method int i = val2.CompareTo(val1); // checking the condition 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"); } } |
chevron_right
filter_none
Output:
val2 is less than val1
Reference:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Clone() Method
- C# | Uri.IsBaseOf(Uri) Method
- C# | Random.Next() Method
- C# | Uri.GetHashCode() Method
- C# | IndexOfAny() Method
- C# | Uri.IsHexDigit() Method
- C# | Uri.FromHex() Method
- C# | SortedDictionary.Add() Method
- C# | Dictionary.Add() Method
- C# | StartsWith() Method
- C# | CopyTo() Method
- C# | Replace() Method
- C# | Remove() Method
- C# | Math.Max() Method
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.



