Int32.Equals 方法

定义

返回一个值,该值指示此实例是否等于指定的 Int32 值。

重载

Equals(Int32)

返回一个值,该值指示此实例是否等于指定的 Int32 值。

Equals(Object)

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

Equals(Int32)

返回一个值,该值指示此实例是否等于指定的 Int32 值。

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 值。

返回

Boolean

如果 true 的值与此实例相同,则为 obj;否则为 false

实现

注解

此方法实现 System.IEquatable<T> 接口,并且性能略好于 Int32.Equals(Object) 因为它不必将 obj 参数转换为对象。

调用方说明

编译器重载解析可能会导致两 Equals(Int32) 个方法重载的行为明显不同。 如果参数与参数Int32之间的obj隐式转换定义且参数未键入为类型Object,编译器将执行隐式转换并调用Equals(Int32)该方法。 否则,它们将调用该方法Equals(Object),如果该方法obj的参数不是Int32值,则始终返回false该方法。 下面的示例说明了两个方法重载的行为差异。 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=“~/snippets/csharp/System/Int32/Equalsoverloads2.cs” interactive=“try-dotnet” id=“Snippet1”::::::code language=“fsharp” source=“~/snippets/fsharp/VS_Snippets_CLR_System/system.int32.equals/fs/equalsoverloads2.fs” id=“Snippet1”::: :::code language=“vb” source=“~/snippets/visualbasic/VS_Snippets_CLR_System/system.int32.equals/vb/equalsoverloads2.vb” id=“Snippet1”::

适用于

Equals(Object)

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

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

参数

obj
Object

与此实例进行比较的对象。

返回

Boolean

如果 trueobj 的实例并且等于此实例的值,则为 Int32;否则为 false

示例

下面的示例演示 Equals 了在上下文 Int32中使用、比较两 int 个值以及返回 true 它们是否表示相同数字,或者 false 返回它们是否相同。

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");
let myVariable1 = 60
let myVariable2 = 60

// Get and display the declaring type.
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is: {myVariable1}"
printfn $"Type of 'myVariable2' is '{myVariable2.GetType()}' and value is: {myVariable2}"

// Compare 'myVariable1' instance with 'myVariable2' Object.
if myVariable1.Equals myVariable2 then
    printfn "\nStructures 'myVariable1' and 'myVariable2' are equal"
else
    printfn "\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) 个方法重载的行为明显不同。 如果参数与参数Int32之间的obj隐式转换定义且参数未键入为类型Object,编译器将执行隐式转换并调用Equals(Int32)该方法。 否则,它们将调用该方法Equals(Object),如果该方法obj的参数不是Int32值,则始终返回false该方法。 下面的示例说明了两个方法重载的行为差异。 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=“~/snippets/csharp/System/Int32/Equalsoverloads2.cs” interactive=“try-dotnet” id=“Snippet1”::::::code language=“fsharp” source=“~/snippets/fsharp/VS_Snippets_CLR_System/system.int32.equals/fs/equalsoverloads2.fs” id=“Snippet1”::: :::code language=“vb” source=“~/snippets/visualbasic/VS_Snippets_CLR_System/system.int32.equals/vb/equalsoverloads2.vb” id=“Snippet1”::

另请参阅

适用于