Boolean.CompareTo(Boolean) Method in C#


The Boolean.CompareTo(Boolean) method in C# is used to compare this instance to a specified Boolean object and returns an integer that indicates their relationship to one another.

Syntax

Following is the syntax −

public int CompareTo (bool val);

Above, Val is a Boolean object to compare to the current instance.

Example

Let us now see an example to implement the Boolean.CompareTo(Boolean) method −

using System;
public class Demo {
   public static void Main(){
      bool b1, b2;
      b1 = false;
      b2 = false;
      int res = b2.CompareTo(b1);
      if (res > 0)
         Console.Write("b1 > b2");
      else if (res < 0)
         Console.Write("b1 < b2");
      else
         Console.Write("b1 = b2");
   }
}

Output

This will produce the following output −

b1 = b2

Updated on: 08-Nov-2019

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements