UInt16.Equals UInt16.Equals UInt16.Equals UInt16.Equals Method

定義

傳回表示這個執行個體是否等於指定物件或 UInt16 的值。Returns a value indicating whether this instance is equal to a specified object or UInt16.

多載

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,指出這個執行個體 (Instance) 是否和指定的物件相等。Returns a value indicating whether this instance is equal to a specified object.

Equals(UInt16) Equals(UInt16) Equals(UInt16) Equals(UInt16)

傳回值,表示這個執行個體是否等於指定的 UInt16 值。Returns a value indicating whether this instance is equal to a specified UInt16 value.

Equals(Object) Equals(Object) Equals(Object) 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 Object Object Object

與這個執行個體相比較的物件。An object to compare to this instance.

傳回

如果 trueobj 的執行個體,並且等於這個執行個體的值,則為 UInt16,否則為 falsetrue if obj is an instance of UInt16 and equals the value of this instance; otherwise, false.

範例

下列範例示範Equals方法。The following example demonstrates the Equals method.

UInt16 myVariable1 = 10;
UInt16 myVariable2 = 10;

//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" );
   UInt16 myVariable1 = 10;
   UInt16 myVariable2 = 10;

   //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 UInt16 = UInt16.Parse(10)
   Dim myVariable2 As UInt16 = UInt16.Parse(10)

   ' 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(UInt16)方法多載行為中的明顯差異。Compiler overload resolution may account for an apparent difference in the behavior of the two Equals(UInt16) method overloads. 如果已定義obj引數UInt16與之間的隱含轉換,且引數未輸入為Object,則Equals(UInt16)編譯器會執行隱含轉換並呼叫方法。If an implicit conversion between the obj argument and a UInt16 is defined and the argument is not typed as an Object, compilers perform an implicit conversion and call the Equals(UInt16) method. 否則,它們會呼叫Equals(Object)方法, false如果UInt16obj引數不是值,則一律會傳回。Otherwise, they call the Equals(Object) method, which always returns false if its obj argument is not a UInt16 value. 下列範例說明兩個方法多載之間的行為差異。The following example illustrates the difference in behavior between the two method overloads. Byte值的案例中,第一個true比較會傳回,因為編譯器會自動執行Equals(UInt16)擴輾轉換並呼叫方法,而第二個比較false會傳回,因為編譯器會呼叫Equals(Object)方法。In the case of a Byte value, the first comparison returns true because the compiler automatically performs a widening conversion and calls the Equals(UInt16) method, whereas the second comparison returns false because the compiler calls the Equals(Object) method.

[!code-csharpSystem.UInt16.Equals#1] [!code-vbSystem.UInt16.Equals#1][!code-csharpSystem.UInt16.Equals#1] [!code-vbSystem.UInt16.Equals#1]

Equals(UInt16) Equals(UInt16) Equals(UInt16) Equals(UInt16)

傳回值,表示這個執行個體是否等於指定的 UInt16 值。Returns a value indicating whether this instance is equal to a specified UInt16 value.

public:
 virtual bool Equals(System::UInt16 obj);
public bool Equals (ushort obj);
override this.Equals : uint16 -> bool
Public Function Equals (obj As UShort) As Boolean

參數

obj
UInt16 UInt16 UInt16 UInt16

和這個執行個體比較之 16 位元不帶正負號的整數。A 16-bit unsigned integer to compare to this instance.

傳回

如果 true 與這個執行個體具有相同的值,則為 obj,否則為 falsetrue if obj has the same value as this instance; otherwise, false.

實作

備註

這個方法System.IEquatable<T>會實作用介面,而且執行Equals效果稍微優於,因為它obj不需要將參數轉換成物件。This method implements the System.IEquatable<T> interface, and performs slightly better than Equals because it does not have to convert the obj parameter to an object.

給呼叫者的注意事項

編譯器多載解析可能會考慮兩個Equals(UInt16)方法多載行為中的明顯差異。Compiler overload resolution may account for an apparent difference in the behavior of the two Equals(UInt16) method overloads. 如果已定義obj引數UInt16與之間的隱含轉換,且引數未輸入為Object,則Equals(UInt16)編譯器會執行隱含轉換並呼叫方法。If an implicit conversion between the obj argument and a UInt16 is defined and the argument is not typed as an Object, compilers perform an implicit conversion and call the Equals(UInt16) method. 否則,它們會呼叫Equals(Object)方法, false如果UInt16obj引數不是值,則一律會傳回。Otherwise, they call the Equals(Object) method, which always returns false if its obj argument is not a UInt16 value. 下列範例說明兩個方法多載之間的行為差異。The following example illustrates the difference in behavior between the two method overloads. Byte值的案例中,第一個true比較會傳回,因為編譯器會自動執行Equals(UInt16)擴輾轉換並呼叫方法,而第二個比較false會傳回,因為編譯器會呼叫Equals(Object)方法。In the case of a Byte value, the first comparison returns true because the compiler automatically performs a widening conversion and calls the Equals(UInt16) method, whereas the second comparison returns false because the compiler calls the Equals(Object) method.

[!code-csharpSystem.UInt16.Equals#1] [!code-vbSystem.UInt16.Equals#1][!code-csharpSystem.UInt16.Equals#1] [!code-vbSystem.UInt16.Equals#1]

適用於