C#中的SByte.CompareTo()方法用于将此实例与指定的对象或SByte进行比较,并返回其相对值的指示。
语法如下-
public int CompareTo (sbyte val); public int CompareTo (object ob);
上面的参数val是要比较的8位有符号整数,而第二种语法的ob是要比较的对象。
现在让我们看一个例子-
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"); } }
输出结果
这将产生以下输出-
Value of S1 = 55 Value of S2 = 55 s1 = s2
现在让我们来看另一个示例-
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"); } }
输出结果
这将产生以下输出-
Value of S1 = 55 Value of S2 = 55 s1 = s2