當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Int32.Equals用法及代碼示例


Int32.Equals()方法用於獲取一個值,該值指示當前實例是否等於指定的對象或Int32。此方法的重載列表中有兩種方法,如下所示:

    • Equals(Int32)
    • Equals(Object)

Int32.Equals(Int32)

此方法用於返回一個值,該值指示當前實例是否等於指定的Int32值。


用法: public bool Equals (int obj);
Here, it takes a Int32 value to compare to this instance.

返回值:如果obj與該實例具有相同的值,則此方法返回true,否則返回false。

以下示例程序旨在說明Int32.Equals(Int32)方法的用法:

範例1:

// C# program to demonstrate the 
// Int32.Equals(Int32) Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing value1 
        int value1 = 4562; 
  
        // Declaring and initializing value2 
        int value2 = 2563; 
  
        // using Equals(Int32) 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); 
    } 
}
輸出:
4562 is not equal to 2563

範例2:

// C# program to demonstrate the 
// Int32.Equals(Int32) Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // calling get() method 
        get(455, 578); 
        get(445, 445); 
        get(10, 20); 
        get(Int32.MaxValue, Int32.MinValue); 
    } 
  
    // defining get() method 
    public static void get(int value1, int value2) 
    { 
  
        // using Equals(Int32) 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); 
    } 
}
輸出:
455 is not equal to 578
445 is equal to 445
10 is not equal to 20
2147483647 is not equal to -2147483648

Int32.Equals(Object) Method

此方法用於返回一個值,該值指示當前實例是否等於指定的對象。

用法: public override bool Equals (object obj);
Here, it takes an object to compare with this instance.

返回值:如果obj是Int32的一個實例,並且此實例的值等於False,則此方法返回true。


以下示例程序旨在說明上述方法的用法:

範例1:

// C# program to demonstrate the 
// Int32.Equals(Object) Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing value1 
        int value1 = 10; 
  
        // Declaring and initializing value2 
        object value2 = 1 / 45; 
  
        // 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 
// Int32.Equals(Object) Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // calling get() method 
        get(547, 585); 
        get(555, 489); 
        get(100, 100); 
        get(745, 725); 
    } 
  
    // defining get() method 
    public static void get(int value1,  
                        object value2) 
    { 
  
        // compare both Int32 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); 
    } 
}
輸出:
547 is not equal to 585
555 is not equal to 489
100 is equal to 100
745 is not equal to 725

參考:



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Int32.Equals Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。