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) 。 如果引數與定義之間的隱含轉換, obj Int16 而且引數的類型不是,則 Object 編譯器會執行隱含轉換,並呼叫 Equals(Int16) 方法。 否則,它們會呼叫 Equals(Object) 方法, false 如果其 obj 引數不是值,它一律會傳回 Int16 。 下列範例說明兩個方法多載之間的行為差異。 在和值的案例 ByteSByte ,第一個比較會傳回, true 因為編譯器會自動執行擴輾轉換並呼叫 Equals(Int16) 方法,而第二個比較會傳回, false 因為編譯器會呼叫 Equals(Object) 方法。

::: code language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.int16.equals/cs/equalsoverl.cs" interactive = "try-dotnet" id = "Snippet1":::::: code language = "fsharp.core" source = "~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.int16.equals/fs/equalsoverl.fs" id = "Snippet1":::::: code language = "vb" source = "~/samples/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

如果 objInt16 的執行個體,並且等於這個執行個體的值,則為 true,否則為 false

範例

下列程式碼範例說明如何在的 Equals 內容中使用 Int16 、比較兩個簡短的值,以及在 true 它們是否代表相同的數位時傳回 false

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) 。 如果引數與定義之間的隱含轉換, obj Int16 而且引數的類型不是,則 Object 編譯器會執行隱含轉換,並呼叫 Equals(Int16) 方法。 否則,它們會呼叫 Equals(Object) 方法, false 如果其 obj 引數不是值,它一律會傳回 Int16 。 下列範例說明兩個方法多載之間的行為差異。 在和值的案例 ByteSByte ,第一個比較會傳回, true 因為編譯器會自動執行擴輾轉換並呼叫 Equals(Int16) 方法,而第二個比較會傳回, false 因為編譯器會呼叫 Equals(Object) 方法。

::: code language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.int16.equals/cs/equalsoverl.cs" interactive = "try-dotnet" id = "Snippet1":::::: code language = "fsharp.core" source = "~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.int16.equals/fs/equalsoverl.fs" id = "Snippet1":::::: code language = "vb" source = "~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.int16.equals/vb/equalsoverl.vb" id = "Snippet1":::

適用於