IEqualityComparer.Equals(Object, Object) IEqualityComparer.Equals(Object, Object) IEqualityComparer.Equals(Object, Object) IEqualityComparer.Equals(Object, Object) Method

定義

判斷指定的物件是否相等。Determines whether the specified objects are equal.

public:
 bool Equals(System::Object ^ x, System::Object ^ y);
public bool Equals (object x, object y);
abstract member Equals : obj * obj -> bool
Public Function Equals (x As Object, y As Object) As Boolean

參數

x
Object Object Object Object

要比較的第一個物件。The first object to compare.

y
Object Object Object Object

要比較的第二個物件。The second object to compare.

傳回

如果指定的物件相等則為 true,否則為 falsetrue if the specified objects are equal; otherwise, false.

例外狀況

xy 是不同的型別,而且二者都不能處理與另外一方的比較。x and y are of different types and neither one can handle comparisons with the other.

範例

下列程式碼範例示範不區分大小寫的實作IEqualityComparerThe following code example demonstrates the implementation of a case-insensitive IEqualityComparer. 在此範例中,CaseInsensitiveComparer.Compare方法用來判斷兩個物件是否相等,根據所提供CultureInfoIn this example, the CaseInsensitiveComparer.Compare method is used to determine whether two objects are equal, based on the provided CultureInfo.

ref class myCultureComparer : IEqualityComparer
{
private:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

    virtual bool Equals(Object^ x, Object^ y) 
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};
class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}
Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

備註

實作此方法以提供自訂的相等比較的物件。Implement this method to provide a customized equality comparison for objects.

給實施者的注意事項

Equals(Object, Object)方法是自反、 對稱的且可轉移。The Equals(Object, Object) method is reflexive, symmetric, and transitive. 也就是說,它會傳回true如果用來比較物件與本身;true兩個物件xytrueyx; 和true兩個物件xz是否true針對xy以及trueyzThat is, it returns true if used to compare an object with itself; true for two objects x and y if it is true for y and x; and true for two objects x and z if it is true for x and y and also true for y and z.

實作所需,確保當Equals(Object, Object)方法會傳回true兩個物件xy,然後將所傳回的值GetHashCode(Object)方法x必須等於的傳回值y.Implementations are required to ensure that if the Equals(Object, Object) method returns true for two objects x and y, then the value returned by the GetHashCode(Object) method for x must equal the value returned for y.

適用於