C#中的SByte.Equals()方法用于返回一个值,该值指示此实例是否等于指定的对象或SByte。
语法如下-
public bool Equals (sbyte ob); public override bool Equals (object ob);
上面,用于与该实例进行比较的SByte值的ob参数,而用于第二种语法的ob参数是与该实例进行比较的对象。
现在让我们看一个例子-
using System; public class Demo { public static void Main() { sbyte s1 = 10; sbyte s2 = 100; Console.WriteLine("Value of S1 = "+s1); Console.WriteLine("Value of S2 = "+s2); Console.WriteLine("Is s1 and s2 equal? = "+s1.Equals(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 = 10 Value of S2 = 100 Is s1 and s2 equal? = False s1 < s2
现在让我们来看另一个示例-
using System; public class Demo { public static void Main() { sbyte s1 = 10; object s2 = 10; Console.WriteLine("Value of S1 = "+s1); Console.WriteLine("Value of S2 = "+s2); Console.WriteLine("Is s1 and s2 equal? = "+s1.Equals(s2)); } }
输出结果
这将产生以下输出-
Value of S1 = 10 Value of S2 = 10 Is s1 and s2 equal? = False