Byte.Equals Method

Definition

傳回數值,指示 Byte 的兩個執行個體是否表示相同的值。Returns a value indicating whether two instances of Byte represent the same value.

Overloads

Equals(Byte)

傳回數值,指示這個執行個體和指定的 Byte 物件是否表示相同的值。Returns a value indicating whether this instance and a specified Byte object represent the same value.

Equals(Object)

傳回值,指出此執行個體是否等於指定的物件。Returns a value indicating whether this instance is equal to a specified object.

Equals(Byte)

傳回數值,指示這個執行個體和指定的 Byte 物件是否表示相同的值。Returns a value indicating whether this instance and a specified Byte object represent the same value.

public:
 virtual bool Equals(System::Byte obj);
public bool Equals (byte obj);
override this.Equals : byte -> bool
Public Function Equals (obj As Byte) As Boolean

Parameters

obj
Byte

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

Returns

Boolean

如果 true 等於這個執行個體則為 obj,否則為 falsetrue if obj is equal to this instance; otherwise, false.

Implements

Examples

下列程式碼範例會判斷第一個 Byte 值是否等於第二個 Byte 值,以及第一個 Byte 值是否等於第二個 Byte 值的已封裝版本。The following code example determines whether the first Byte value is equal to the second Byte value, and whether the first Byte value is equal to the boxed version of the second Byte value.

using namespace System;

void main()
{
    Byte   byteVal1 = 0x7f;
    Byte   byteVal2 = 127;
    Object^ objectVal3 = byteVal2;

    Console::WriteLine("byteVal1 = {0}, byteVal2 = {1}, objectVal3 = {2}\n",
                       byteVal1, byteVal2, objectVal3);
    Console::WriteLine("byteVal1 equals byteVal2?: {0}", byteVal1.Equals(byteVal2));
    Console::WriteLine("byteVal1 equals objectVal3?: {0}", byteVal1.Equals(objectVal3));
}
/*
This example displays the following output:
      byteVal1 = 127, byteVal2 = 127, objectVal3 = 127

      byteVal1 equals byteVal2?: True
      byteVal1 equals objectVal3?: True
*/
// This code example demonstrates the System.Byte.Equals(Object) and
// System.Byte.Equals(Byte) methods.

using System;

class Sample
{
    public static void Main()
    {
    byte   byteVal1 = 0x7f;
    byte   byteVal2 = 127;
    object objectVal3 = byteVal2;
//
    Console.WriteLine("byteVal1 = {0}, byteVal2 = {1}, objectVal3 = {2}\n",
                       byteVal1, byteVal2, objectVal3);
    Console.WriteLine("byteVal1 equals byteVal2?: {0}", byteVal1.Equals(byteVal2));
    Console.WriteLine("byteVal1 equals objectVal3?: {0}", byteVal1.Equals(objectVal3));
    }
}

/*
This code example produces the following results:

byteVal1 = 127, byteVal2 = 127, objectVal3 = 127

byteVal1 equals byteVal2?: True
byteVal1 equals objectVal3?: True

*/
' This code example demonstrates the System.Byte.Equals(Object) and
' System.Byte.Equals(Byte) methods.

Class Sample
    Public Shared Sub Main() 
        Dim byteVal1 As Byte = &H7F
        Dim byteVal2 As Byte = 127
        Dim objectVal3 As Object = byteVal2
        '
        Console.WriteLine("byteVal1 = {0}, byteVal2 = {1}, objectVal3 = {2}" & vbCrLf, _
                          byteVal1, byteVal2, objectVal3)
        Console.WriteLine("byteVal1 equals byteVal2?: {0}", byteVal1.Equals(byteVal2))
        Console.WriteLine("byteVal1 equals objectVal3?: {0}", byteVal1.Equals(objectVal3))
    End Sub
End Class

'
'This code example produces the following results:
'
'byteVal1 = 127, byteVal2 = 127, objectVal3 = 127
'
'byteVal1 equals byteVal2?: True
'byteVal1 equals objectVal3?: True
'

Remarks

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

See also

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

Parameters

obj
Object

與這個執行個體相比較的物件,或 nullAn object to compare with this instance, or null.

Returns

Boolean

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

Examples

下列程式碼範例會判斷第一個 Byte 值是否等於第二個 Byte 值,以及第一個 Byte 值是否等於第二個 Byte 值的已封裝版本。The following code example determines whether the first Byte value is equal to the second Byte value, and whether the first Byte value is equal to the boxed version of the second Byte value.

using namespace System;

void main()
{
    Byte   byteVal1 = 0x7f;
    Byte   byteVal2 = 127;
    Object^ objectVal3 = byteVal2;

    Console::WriteLine("byteVal1 = {0}, byteVal2 = {1}, objectVal3 = {2}\n",
                       byteVal1, byteVal2, objectVal3);
    Console::WriteLine("byteVal1 equals byteVal2?: {0}", byteVal1.Equals(byteVal2));
    Console::WriteLine("byteVal1 equals objectVal3?: {0}", byteVal1.Equals(objectVal3));
}
/*
This example displays the following output:
      byteVal1 = 127, byteVal2 = 127, objectVal3 = 127

      byteVal1 equals byteVal2?: True
      byteVal1 equals objectVal3?: True
*/
// This code example demonstrates the System.Byte.Equals(Object) and
// System.Byte.Equals(Byte) methods.

using System;

class Sample
{
    public static void Main()
    {
    byte   byteVal1 = 0x7f;
    byte   byteVal2 = 127;
    object objectVal3 = byteVal2;
//
    Console.WriteLine("byteVal1 = {0}, byteVal2 = {1}, objectVal3 = {2}\n",
                       byteVal1, byteVal2, objectVal3);
    Console.WriteLine("byteVal1 equals byteVal2?: {0}", byteVal1.Equals(byteVal2));
    Console.WriteLine("byteVal1 equals objectVal3?: {0}", byteVal1.Equals(objectVal3));
    }
}

/*
This code example produces the following results:

byteVal1 = 127, byteVal2 = 127, objectVal3 = 127

byteVal1 equals byteVal2?: True
byteVal1 equals objectVal3?: True

*/
' This code example demonstrates the System.Byte.Equals(Object) and
' System.Byte.Equals(Byte) methods.

Class Sample
    Public Shared Sub Main() 
        Dim byteVal1 As Byte = &H7F
        Dim byteVal2 As Byte = 127
        Dim objectVal3 As Object = byteVal2
        '
        Console.WriteLine("byteVal1 = {0}, byteVal2 = {1}, objectVal3 = {2}" & vbCrLf, _
                          byteVal1, byteVal2, objectVal3)
        Console.WriteLine("byteVal1 equals byteVal2?: {0}", byteVal1.Equals(byteVal2))
        Console.WriteLine("byteVal1 equals objectVal3?: {0}", byteVal1.Equals(objectVal3))
    End Sub
End Class

'
'This code example produces the following results:
'
'byteVal1 = 127, byteVal2 = 127, objectVal3 = 127
'
'byteVal1 equals byteVal2?: True
'byteVal1 equals objectVal3?: True
'

See also

Applies to