SByte.CompareTo() 方法在 C# 中的使用及示例


在 C# 中,SByte.CompareTo() 方法用於將此例項與指定的 SByte 或者物件進行比較,並返回其相對值指示。

語法

語法如下 −

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

以上,對於第二個語法,引數值為要比較的 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

更新時間: 04-Dec-2019

35 次瀏覽

開啟您的 職業生涯

透過完成課程獲取認證

開始學習
廣告
© . All rights reserved.