Byte.CompareTo(Object) Method in C#


The Byte.CompareTo(Object) method in C# is used to compare this instance to a specified object and returns an indication of their relative values.

Syntax

Following is the syntax −

public int CompareTo (object val);

Above, Val is an object to compare or null.

The return value is less than zero if the current instance is less than the value. It’s zero, if the current instance is equal to value, whereas return value is more than zero if the current instance is more than value.

Example

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

using System;
public class Demo {
   public static void Main(){
      byte b1;
      b1 = 5;
      object b2 = (byte)5;
      int res = b1.CompareTo(b2);
      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: 11-Nov-2019

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements