C# | Byte.CompareTo(Byte) Method

This method is used to compare this instance to a specified 8-bit unsigned integer and returns an indication of their relative values.

Syntax:

public int CompareTo (byte value);

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

Return Value: This method returns a signed integer that indicates the relative order of this instance and value.

  • Less than zero: This instance is less than value.
  • Zero: This instance is equal to value.
  • Greater than zero: This instance is greater than value.

Below programs illustrate the use of Byte.CompareTo(Byte) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Byte.CompareTo(byte)
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring val1 and val2
        byte val1, val2;
  
        // intializing the val1, 
        // val2 and val3
        val1 = 12;
        val2 = 13;
  
        // getting compared constant
        // using CompareTo method
        int i = val2.CompareTo(val1);
  
        // checking the condition
        if (i > 0)
            Console.Write("val2 is greater than val1");
  
        else if (i < 0)
            Console.Write("val2 is less than val1");
  
        else
            Console.Write("val1 is equal to val1");
    }
}

chevron_right


Output:

val2 is greater than val1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Byte.CompareTo(byte)
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // checking the condition
        // calling check() method
        check((byte)10, (byte)20);
        check((byte)30, (byte)20);
        check((byte)10, (byte)10);
        check((byte)5, (byte)7);
        check((byte)40, (byte)50);
        check((byte)1, (byte)2);
    }
  
    // Defining the check method
    public static void check(byte v1, byte v2)
    {
  
        // getting compared constant
        // using CompareTo() method
        int i = v1.CompareTo(v2);
  
        // checking the condition
        if (i > 0)
            Console.WriteLine(v1 + " is greater than " + v2);
  
        else if (i < 0)
            Console.WriteLine(v1 + " is less than " + v2);
  
        else
            Console.WriteLine(v1 + " is equal to " + v2);
    }
}

chevron_right


Output:

10 is less than 20
30 is greater than 20
10 is equal to 10
5 is less than 7
40 is less than 50
1 is less than 2

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.