ValueType.Equals(Object) Yöntem
Tanım
Bu örnek ile belirtilen bir nesnenin eşit olup olmadığını gösterir.Indicates whether this instance and a specified object are equal.
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
Parametreler
- obj
- Object
Geçerli örnekle Karşılaştırılacak nesne.The object to compare with the current instance.
Döndürülenler
true Eğer obj ve bu örnek aynı türde ve aynı değeri temsil ediyorsa, aksi durumda, false .true if obj and this instance are the same type and represent the same value; otherwise, false.
Örnekler
Aşağıdaki örnek, Equals yönteminin türetilmiş bir değer türü tarafından nasıl geçersiz kılınabileceğini gösterir.The following example demonstrates how the Equals method can be overridden by a derived value type.
public ref struct Complex
{
public:
double m_Re;
double m_Im;
virtual bool Equals( Object^ ob ) override
{
if ( dynamic_cast<Complex^>(ob) )
{
Complex^ c = dynamic_cast<Complex^>(ob);
return m_Re == c->m_Re && m_Im == c->m_Im;
}
else
{
return false;
}
}
virtual int GetHashCode() override
{
return m_Re.GetHashCode() ^ m_Im.GetHashCode();
}
};
public struct Complex
{
public double m_Re;
public double m_Im;
public override bool Equals( object ob ){
if( ob is Complex ) {
Complex c = (Complex) ob;
return m_Re==c.m_Re && m_Im==c.m_Im;
}
else {
return false;
}
}
public override int GetHashCode(){
return m_Re.GetHashCode() ^ m_Im.GetHashCode();
}
}
Public Structure Complex
Private m_Re As Double
Private m_Im As Double
Public Overloads Function Equals(ob As Object) As Boolean
If TypeOf ob Is Complex Then
Dim c As Complex = CType(ob, Complex)
Return m_Re = c.m_Re And m_Im = c.m_Im
Else
Return False
End If
End Function
Public Overloads Function GetHashCode() As Integer
Return m_Re.GetHashCode() ^ m_Im.GetHashCode()
End Function
End Structure
Açıklamalar
ValueType.Equals(Object)Yöntemi geçersiz kılar Object.Equals(Object) ve .NET Framework tüm değer türleri için varsayılan değer eşitlik uygulamasını sağlar.The ValueType.Equals(Object) method overrides Object.Equals(Object) and provides the default implementation of value equality for all value types in the .NET Framework.
Varsayılan uygulama, Object.Equals(Object) geçerli örneğin her bir alanında çağırır ve obj true tüm alanlar eşitse, döndürür.The default implementation calls Object.Equals(Object) on each field of the current instance and obj and returns true if all fields are equal.
İpucu
Özellikle değer tipinizdeki başvuru türleri olan alanlar içeriyorsa, yöntemini geçersiz kılmanız gerekir Equals(Object) .Particularly if your value type contains fields that are reference types, you should override the Equals(Object) method. Bu, performansı iyileştirebilir ve tür için eşitlik anlamını daha yakından göstermenize olanak tanır.This can improve performance and enable you to more closely represent the meaning of equality for the type.
Windows Çalışma Zamanı notlarıNotes for the Windows Runtime
EqualsYöntemi Windows çalışma zamanı bir yapıda çağırdığınızda, geçersiz kılınmaz değer türleri için varsayılan davranışı sağlar Equals .When you call the Equals method on a Windows Runtime structure, it provides the default behavior for value types that don't override Equals. Bu, .NET Framework Windows Çalışma Zamanı için sağladığı desteğin bir parçasıdır (bkz. Windows Mağazası uygulamaları için .NET Framework desteği ve Windows çalışma zamanı).This is part of the support that the .NET Framework provides for the Windows Runtime (see .NET Framework Support for Windows Store Apps and Windows Runtime). Windows Çalışma Zamanı yapıları Equals , C# veya Visual Basic yazılmış olsa bile, metotlara sahip olmadıkları halde geçersiz kılınamaz.Windows Runtime structures can't override Equals, even if they're written with C# or Visual Basic, because they can't have methods. (Ayrıca Windows Çalışma Zamanı içindeki yapılar aktarılmaz ValueType .) Ancak, ToString Equals GetHashCode bunları C# veya Visual Basic kodunuzda kullanırken,, ve yöntemlerine sahip oldukları gibi görünürler ve .NET Framework bu yöntemler için varsayılan davranışı sağlar.(In addition, structures in the Windows Runtime itself don't inherit ValueType.) However, they appear to have ToString, Equals, and GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods.