Int32.Equals 方法

定义

返回一个值,该值指示此实例是否等于指定的 Int32 值。Returns a value indicating whether this instance is equal to a specified Int32 value.

重载

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.

返回

Boolean

如果 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 如果其 obj 参数不是值,则总是返回 Int32Otherwise, 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 language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.int32.equals/cs/equalsoverloads2.cs" interactive = "dotnet" id = "Snippet1":::::: code language = "vb" source = "~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.int32.equals/vb/equalsoverloads2.vb" id = "Snippet1"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.int32.equals/cs/equalsoverloads2.cs" interactive="try-dotnet" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.int32.equals/vb/equalsoverloads2.vb" id="Snippet1":::

适用于

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.

返回

Boolean

如果 trueobj 的实例并且等于此实例的值,则为 Int32;否则为 falsetrue if obj is an instance of Int32 and equals the value of this instance; otherwise, false.

示例

下面的示例阐释了在的 Equals 上下文中的用法 Int32 ,比较两个 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) 方法, false 如果其 obj 参数不是值,则总是返回 Int32Otherwise, 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 language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.int32.equals/cs/equalsoverloads2.cs" interactive = "dotnet" id = "Snippet1":::::: code language = "vb" source = "~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.int32.equals/vb/equalsoverloads2.vb" id = "Snippet1"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.int32.equals/cs/equalsoverloads2.cs" interactive="try-dotnet" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.int32.equals/vb/equalsoverloads2.vb" id="Snippet1":::

另请参阅

适用于