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)

傳回值,指出這個執行個體 (Instance) 是否和指定的物件相等。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)方法, false如果Int32obj引數不是值,則一律會傳回。Otherwise, 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. Bytetrue Equals(Int32)Int16、和值的案例中,第一個比較會傳回,因為編譯器會自動執行擴輾轉換並呼叫方法,而UInt16 SByte第二個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.Int32.Equals#1] [!code-vbSystem.Int32.Equals#1][!code-csharpSystem.Int32.Equals#1] [!code-vbSystem.Int32.Equals#1]

Equals(Object)

傳回值,指出這個執行個體 (Instance) 是否和指定的物件相等。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.

範例

下列範例Equals說明在的int Int32內容中使用,並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)方法, false如果Int32obj引數不是值,則一律會傳回。Otherwise, 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. Bytetrue Equals(Int32)Int16、和值的案例中,第一個比較會傳回,因為編譯器會自動執行擴輾轉換並呼叫方法,而UInt16 SByte第二個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.Int32.Equals#1] [!code-vbSystem.Int32.Equals#1][!code-csharpSystem.Int32.Equals#1] [!code-vbSystem.Int32.Equals#1]

另請參閱

適用於