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.


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.


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");
         Console.Write("b1 = b2");


This will produce the following output −

b1 = b2

Updated on: 11-Nov-2019


Kickstart Your Career

Get certified by completing the course

Get Started