📜  C#中的SByte.Equals方法与示例

📅  最后修改于: 2021-05-29 18:52:14             🧑  作者: Mango

SByte.Equals方法用于获取一个值,该值指示当前实例是否等于指定的对象或SByte。此方法的重载列表中有2种方法,如下所示:

  • 等于(SByte)方法
  • 等于(对象)方法

SByte.Equals(SByte)方法

此方法用于返回一个值,该值指示当前实例是否等于指定的SByte值。

下面的程序说明了SByte.Equals(SByte)方法的用法:

范例1:

// C# program to demonstrate the
// SByte.Equals(SByte) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        sbyte value1 = 42;
  
        // Declaring and initializing value2
        sbyte value2 = 23;
  
        // using Equals(SByte) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}
输出:
42 is not equal to 23

范例2:

// C# program to demonstrate the
// SByte.Equals(SByte) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling get() method
        get(78, 58);
        get(45, 45);
        get(10, 20);
        get(SByte.MaxValue, SByte.MinValue);
    }
  
    // defining get() method
    public static void get(sbyte value1,
                           sbyte value2)
    {
  
        // using Equals(SByte) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}
输出:
78 is not equal to 58
45 is equal to 45
10 is not equal to 20
127 is not equal to -128

SByte.Equals(Object)方法

此方法用于返回一个值,该值指示当前实例是否等于指定的对象。

下面的程序说明了上述方法的用法:

范例1:

// C# program to demonstrate the
// SByte.Equals(Object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing value1
        sbyte value1 = 10;
  
        // Declaring and initializing value2
        object value2 = 1 / 47;
  
        // using Equals(object) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}
输出:
10 is not equal to 0

范例2:

// C# program to demonstrate the
// SByte.Equals(Object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(112, 85);
        get(124, 154);
        get(87, 87);
        get(54, 76);
    }
  
    // defining get() method
    public static void get(sbyte value1,
                        object value2)
    {
  
        // compare both SByte value
        // using Equals(object) method
        bool status = value1.Equals(value2);
  
        // checking the status
        if (status)
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        value1, value2);
    }
}
输出:
112 is not equal to 85
124 is not equal to 154
87 is not equal to 87
54 is not equal to 76

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.sbyte.equals?view=netcore-2.1