Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo ValueType.Equals (Object)

 

Data di pubblicazione: ottobre 2016

Indica se questa istanza e un oggetto specificato sono uguali.

Spazio dei nomi:   System
Assembly:  mscorlib (in mscorlib.dll)

public override bool Equals(
	object obj
)

Parametri

obj
Type: System.Object

Oggetto da confrontare con l'istanza corrente.

Valore restituito

Type: System.Boolean

true se obj e questa istanza presentano lo stesso tipo e rappresentano lo stesso valore. In caso contrario, false.

The M:System.ValueType.Equals(System.Object) method overrides M:System.Object.Equals(System.Object) and provides the default implementation of value equality for all value types in the .NET Framework.

If none of the fields of the current instance and obj are reference types, the M:System.ValueType.Equals(System.Object) method performs a byte-by-byte comparison of the two objects in memory. Otherwise, it uses reflection to compare the corresponding fields of obj and this instance.

System_CAPS_tipSuggerimento

Particularly if your value type contains fields that are reference types, you should override the M:System.ValueType.Equals(System.Object) method. This can improve performance and enable you to more closely represent the meaning of equality for the type.

When you call the M:System.ValueType.Equals(System.Object)method on a wrt structure, it provides the default behavior for value types that don’t override M:System.ValueType.Equals(System.Object). This is part of the support that the .NET Framework provides for the wrt (see .NET Framework Support for Windows Store Apps and Windows Runtime). wrt structures can’t override M:System.ValueType.Equals(System.Object), even if they’re written with C# or Visual Basic, because they can’t have methods. (In addition, structures in the wrt itself don’t inherit T:System.ValueType.) However, they appear to have M:System.ValueType.ToString, M:System.ValueType.Equals(System.Object), and M:System.ValueType.GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods.

The following example demonstrates how the M:System.ValueType.Equals(System.Object) method can be overridden by a derived value type.

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();
	}
}

Universal Windows Platform
Disponibile da 8
.NET Framework
Disponibile da 1.1
Libreria di classi portabile
Supportato in: piattaforme .NET portabili
Silverlight
Disponibile da 2.0
Windows Phone Silverlight
Disponibile da 7.0
Windows Phone
Disponibile da 8.1
Torna all'inizio
Mostra: