Int32.Equals 方法

定義

傳回表示這個執行個體是否等於指定物件或 Int32 的值。Returns a value indicating whether this instance is equal to a specified object or Int32.

多載

Equals(Int32)

傳回值,表示這個執行個體是否等於指定的 Int32 值。Returns a value indicating whether this instance is equal to a specified Int32 value.

Equals(Object)

傳回值,指出此執行個體是否等於指定的物件。Returns a value indicating whether this instance is equal to a specified object.

Equals(Int32)

傳回值,表示這個執行個體是否等於指定的 Int32 值。Returns a value indicating whether this instance is equal to a specified Int32 value.

public:
 virtual bool Equals(int obj);
public bool Equals (int obj);
override this.Equals : int -> bool
Public Function Equals (obj As Integer) As Boolean

參數

obj
Int32

要與此執行個體相比較的 Int32 值。An Int32 value to compare to this instance.

傳回

如果 true 與這個執行個體具有相同的值,則為 obj,否則為 falsetrue if obj has the same value as this instance; otherwise, false.

實作

備註

這個方法會實作用 System.IEquatable<T> 介面,而且執行效果稍微優於 Int32.Equals(Object),因為它不需要將 obj 參數轉換成物件。This method implements the System.IEquatable<T> interface, and performs slightly better than Int32.Equals(Object) because it does not have to convert the obj parameter to an object.

給呼叫者的注意事項

編譯器多載解析可能會考慮兩個 Equals(Int32) 方法多載的行為明顯差異。Compiler overload resolution may account for an apparent difference in the behavior of the two Equals(Int32) method overloads. 如果 obj 引數與 Int32 之間的隱含轉換已定義,而引數未輸入為 Object,則編譯器會執行隱含轉換並呼叫 Equals(Int32) 方法。If an implicit conversion between the obj argument and an Int32 is defined and the argument is not typed as an Object, compilers perform an implicit conversion and call the Equals(Int32) method. 否則,它們會呼叫 Equals(Object) 方法,如果其 obj 引數不是 Int32 值,則一律會傳回 falseOtherwise, they call the Equals(Object) method, which always returns false if its obj argument is not an Int32 value. 下列範例說明兩個方法多載之間的行為差異。The following example illustrates the difference in behavior between the two method overloads. ByteInt16SByteUInt16 值的情況下,第一個比較會傳回 true,因為編譯器會自動執行擴輾轉換並呼叫 Equals(Int32) 方法,而第二個比較會傳回 false,因為編譯器會呼叫 Equals(Object) 方法。In the case of the Byte, Int16, SByte, and UInt16 values, the first comparison returns true because the compiler automatically performs a widening conversion and calls the Equals(Int32) method, whereas the second comparison returns false because the compiler calls the Equals(Object) method.

[! code-csharpsystem.object # 1][! code-vb system.string. Equals # 1][!code-csharpSystem.Int32.Equals#1] [!code-vbSystem.Int32.Equals#1]

Equals(Object)

傳回值,指出此執行個體是否等於指定的物件。Returns a value indicating whether this instance is equal to a specified object.

public:
 override bool Equals(System::Object ^ obj);
public override bool Equals (object obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean

參數

obj
Object

要與這個執行個體比較的物件。An object to compare with this instance.

傳回

如果 trueobj 的執行個體,並且等於這個執行個體的值,則為 Int32,否則為 falsetrue if obj is an instance of Int32 and equals the value of this instance; otherwise, false.

範例

下列範例說明如何在 Int32的內容中使用 Equals、比較兩個 int 值,以及傳回 true (如果它們代表相同的數位),或 false (如果沒有的話)。The following example illustrates the use of Equals in the context of Int32, comparing two int values and returning true if they represent the same number, or false if they do not.

Int32 myVariable1 = 60;
Int32 myVariable2 = 60;

// Get and display the declaring type.
Console::WriteLine( "\nType of 'myVariable1' is '{0}' and  value is : {1}", myVariable1.GetType(), myVariable1 );
Console::WriteLine( "Type of 'myVariable2' is '{0}' and  value is : {1}", myVariable2.GetType(), myVariable2 );

// Compare 'myVariable1' instance with 'myVariable2' Object.
if ( myVariable1.Equals( myVariable2 ) )
      Console::WriteLine( "\nStructures 'myVariable1' and 'myVariable2' are equal" );
else
      Console::WriteLine( "\nStructures 'myVariable1' and 'myVariable2' are not equal" );

Int32 myVariable1 = 60;
Int32 myVariable2 = 60;

// Get and display the declaring type.
Console.WriteLine("\nType of 'myVariable1' is '{0}' and"+
     " value is :{1}",myVariable1.GetType(), myVariable1); 
Console.WriteLine("Type of 'myVariable2' is '{0}' and"+
     " value is :{1}",myVariable2.GetType(), myVariable2);

// Compare 'myVariable1' instance with 'myVariable2' Object.
if( myVariable1.Equals( myVariable2 ) )
   Console.WriteLine( "\nStructures 'myVariable1' and "+
         "'myVariable2' are equal");
else
   Console.WriteLine( "\nStructures 'myVariable1' and "+
         "'myVariable2' are not equal");

Dim myVariable1 As Int32 = 60
Dim myVariable2 As Int32 = 60

' Get and display the declaring type.
Console.WriteLine(ControlChars.NewLine + "Type of 'myVariable1' is '{0}' and" +  _
         " value is :{1}", myVariable1.GetType().ToString(), myVariable1.ToString())
Console.WriteLine("Type of 'myVariable2' is '{0}' and" +  _
         " value is :{1}", myVariable2.GetType().ToString(), myVariable2.ToString())

' Compare 'myVariable1' instance with 'myVariable2' Object.
If myVariable1.Equals(myVariable2) Then
   Console.WriteLine(ControlChars.NewLine + "Structures 'myVariable1' and " +  _
            "'myVariable2' are equal")
Else
   Console.WriteLine(ControlChars.NewLine + "Structures 'myVariable1' and " + _
         "'myVariable2' are not equal")
End If

給呼叫者的注意事項

編譯器多載解析可能會考慮兩個 Equals(Int32) 方法多載的行為明顯差異。Compiler overload resolution may account for an apparent difference in the behavior of the two Equals(Int32) method overloads. 如果 obj 引數與 Int32 之間的隱含轉換已定義,而引數未輸入為 Object,則編譯器會執行隱含轉換並呼叫 Equals(Int32) 方法。If an implicit conversion between the obj argument and an Int32 is defined and the argument is not typed as an Object, compilers perform an implicit conversion and call the Equals(Int32) method. 否則,它們會呼叫 Equals(Object) 方法,如果其 obj 引數不是 Int32 值,則一律會傳回 falseOtherwise, they call the Equals(Object) method, which always returns false if its obj argument is not an Int32 value. 下列範例說明兩個方法多載之間的行為差異。The following example illustrates the difference in behavior between the two method overloads. ByteInt16SByteUInt16 值的情況下,第一個比較會傳回 true,因為編譯器會自動執行擴輾轉換並呼叫 Equals(Int32) 方法,而第二個比較會傳回 false,因為編譯器會呼叫 Equals(Object) 方法。In the case of the Byte, Int16, SByte, and UInt16 values, the first comparison returns true because the compiler automatically performs a widening conversion and calls the Equals(Int32) method, whereas the second comparison returns false because the compiler calls the Equals(Object) method.

[! code-csharpsystem.object # 1][! code-vb system.string. Equals # 1][!code-csharpSystem.Int32.Equals#1] [!code-vbSystem.Int32.Equals#1]

另請參閱

適用於