Int16.Equals 方法

定义

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

重载

Equals(Int16)

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

Equals(Object)

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

Equals(Int16)

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

public:
 virtual bool Equals(short obj);
public bool Equals (short obj);
override this.Equals : int16 -> bool
Public Function Equals (obj As Short) As Boolean

参数

obj
Int16

要与此实例进行比较的 Int16 值。

返回

Boolean

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

实现

注解

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

调用方说明

编译器重载解析可能会导致两 Equals(Int16) 个方法重载的行为明显不同。 如果参数与参数Int16之间的obj隐式转换定义且参数未键入为类型Object,编译器将执行隐式转换并调用Equals(Int16)该方法。 否则,它们将调用该方法Equals(Object),如果该方法obj的参数不是Int16值,则始终返回false该方法。 下面的示例说明了两个方法重载的行为差异。 对于 ByteSByte 值,第一个比较返回 true 是因为编译器会自动执行扩大转换并调用 Equals(Int16) 方法,而第二个比较返回是因为 false 编译器调用 Equals(Object) 该方法。

:::code language=“csharp” source=“~/snippets/csharp/System/Int16/Equalsoverl.cs” interactive=“try-dotnet” id=“Snippet1”::: ::::code language=“fsharp” source=“~/snippets/fsharp/VS_Snippets_CLR_System/system.int16.equals/fs/equalsoverl.fs” id=“Snippet1”: ::::code language=“vb” source=“~/snippets/visualbasic/VS_Snippets_CLR_System/system.int16.equals/vb/equalsoverl.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 的实例并且等于此实例的值,则为 Int16;否则为 false

示例

下面的代码示例演示了在上下文中的用法Equals,比较两个短值,并返回true它们是否表示相同数字,或者false返回它们是否Int16相同。

Int16 myVariable1 = 20;
Int16 myVariable2 = 20;

// 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" );
Int16 myVariable1 = 20;
Int16 myVariable2 = 20;

// 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 = 20s
let myVariable2 = 20s

// Get and display the declaring type.
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is: {myVariable1}"
printfn $"\nType of 'myVariable1' 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 Int16 = 20
  Dim myVariable2 As Int16 = 20
  
' 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(Int16) 个方法重载的行为明显不同。 如果参数与参数Int16之间的obj隐式转换定义且参数未键入为类型Object,编译器将执行隐式转换并调用Equals(Int16)该方法。 否则,它们将调用该方法Equals(Object),如果该方法obj的参数不是Int16值,则始终返回false该方法。 下面的示例说明了两个方法重载的行为差异。 对于 ByteSByte 值,第一个比较返回 true 是因为编译器会自动执行扩大转换并调用 Equals(Int16) 方法,而第二个比较返回是因为 false 编译器调用 Equals(Object) 该方法。

:::code language=“csharp” source=“~/snippets/csharp/System/Int16/Equalsoverl.cs” interactive=“try-dotnet” id=“Snippet1”::: ::::code language=“fsharp” source=“~/snippets/fsharp/VS_Snippets_CLR_System/system.int16.equals/fs/equalsoverl.fs” id=“Snippet1”: ::::code language=“vb” source=“~/snippets/visualbasic/VS_Snippets_CLR_System/system.int16.equals/vb/equalsoverl.vb” id=“Snippet1”::

适用于