Byte.CompareTo(Byte) Method in C#

The Byte.CompareTo(Byte) method in C# is used to compare this instance to a specified 8-bit unsigned integer and returns an indication of their relative values.


Following is the syntax −

public int CompareTo (byte val);

Above, the parameter value is an 8-bit unsigned integer to compare.

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(Byte) method −

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


This will produce the following output −

b2 > b1

Updated on: 11-Nov-2019


Kickstart Your Career

Get certified by completing the course

Get Started