SByte.CompareTo() Method in C# with Examples


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

Syntax

The syntax is as follows −

public int CompareTo (sbyte val);
public int CompareTo (object ob);

Above, the parameter val is an 8-bit signed integer to compare, whereas ob for the 2nd syntax is an object to compare.

Example

Let us now see an example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      sbyte s1 = 55;
      sbyte s2 = 55;
      Console.WriteLine("Value of S1 = "+s1);
      Console.WriteLine("Value of S2 = "+s2);
      int res = s1.CompareTo(s2);
      if (res > 0)
         Console.WriteLine("s1 > s2");
      else if (res < 0)
         Console.WriteLine("s1 < s2");
      else
         Console.WriteLine("s1 = s2");
   }
}

Output

This will produce the following output −

Value of S1 = 55
Value of S2 = 55
s1 = s2

Example

Let us now see another example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      sbyte s1 = 55;
      object s2 = (sbyte)55;
      Console.WriteLine("Value of S1 = "+s1);
      Console.WriteLine("Value of S2 = "+s2);
      int res = s1.CompareTo(s2);
      if (res > 0)
         Console.WriteLine("s1 > s2");
      else if (res < 0)
         Console.WriteLine("s1 < s2");
      else
         Console.WriteLine("s1 = s2");
   }
}

Output

This will produce the following output −

Value of S1 = 55
Value of S2 = 55
s1 = s2
raja
Published on 04-Dec-2019 12:44:56
Advertisements