Byte.CompareTo(Byte) Method in C#

The Byte.CompareTo(Byte) method in C# is used to compare the current byte instance to a specified 8-bit unsigned integer and returns an indication of their relative values. This method is useful for sorting operations and determining the relative ordering of byte values.

Syntax

Following is the syntax −

public int CompareTo(byte value);

Parameters

value: An 8-bit unsigned integer to compare with the current instance.

Return Value

The method returns an integer that indicates the relative values of the compared instances:

  • Less than zero: The current instance is less than value.

  • Zero: The current instance is equal to value.

  • Greater than zero: The current instance is greater than value.

Example

Let us see an example demonstrating all three comparison scenarios −

using System;

public class Demo {
    public static void Main() {
        byte b1 = 5;
        byte b2 = 7;
        byte b3 = 5;
        
        // Compare b2 (7) with b1 (5)
        int result1 = b2.CompareTo(b1);
        Console.WriteLine("b2.CompareTo(b1): " + result1);
        
        // Compare b1 (5) with b2 (7)
        int result2 = b1.CompareTo(b2);
        Console.WriteLine("b1.CompareTo(b2): " + result2);
        
        // Compare b1 (5) with b3 (5)
        int result3 = b1.CompareTo(b3);
        Console.WriteLine("b1.CompareTo(b3): " + result3);
        
        // Practical usage in conditional statements
        if (result1 > 0)
            Console.WriteLine("b2 > b1");
        else if (result1 < 0)
            Console.WriteLine("b2 < b1");
        else
            Console.WriteLine("b2 = b1");
    }
}

The output of the above code is −

b2.CompareTo(b1): 1
b1.CompareTo(b2): -1
b1.CompareTo(b3): 0
b2 > b1

Using CompareTo for Sorting

The CompareTo method is commonly used in sorting algorithms. Here's an example showing how to sort an array of bytes −

using System;

public class SortingExample {
    public static void Main() {
        byte[] bytes = { 15, 3, 8, 12, 1, 20 };
        
        Console.WriteLine("Original array:");
        foreach (byte b in bytes) {
            Console.Write(b + " ");
        }
        Console.WriteLine();
        
        // Sort using Array.Sort (which uses CompareTo internally)
        Array.Sort(bytes);
        
        Console.WriteLine("Sorted array:");
        foreach (byte b in bytes) {
            Console.Write(b + " ");
        }
        Console.WriteLine();
    }
}

The output of the above code is −

Original array:
15 3 8 12 1 20 
Sorted array:
1 3 8 12 15 20 

Conclusion

The Byte.CompareTo(Byte) method provides a standardized way to compare byte values, returning negative, zero, or positive integers based on the comparison result. This method is essential for sorting operations and implementing custom comparison logic in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements