ValueType.GetHashCode Metodo

Definizione

Restituisce il codice hash per questa istanza.

public:
 override int GetHashCode();
public override int GetHashCode();
override this.GetHashCode : unit -> int
Public Overrides Function GetHashCode () As Integer

Valori restituiti

Intero con segno a 32 bit che rappresenta il codice hash per questa istanza.

Esempio

Nell'esempio seguente viene illustrato come il GetHashCode metodo può essere sottoposto a override da un tipo di valore derivato.

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();
    }
}
type Complex() =
    member val m_Re = 0. with get, set
    member val m_Im = 0. with get, set

    override this.Equals(ob) =
        match ob with
        | :? Complex as c ->
            this.m_Re = c.m_Re && this.m_Im = c.m_Im
        | _ -> false
        
    override this.GetHashCode() =
        this.m_Re.GetHashCode() ^^^ this.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

Commenti

Il GetHashCode metodo si applica ai tipi derivati da ValueType. Uno o più campi del tipo derivato vengono utilizzati per calcolare il valore restituito. Se si chiama il metodo del GetHashCode tipo derivato, è probabile che il valore restituito non sia adatto per l'uso come chiave in una tabella hash. Inoltre, se il valore di uno o più campi cambia, il valore restituito potrebbe non essere adatto per l'uso come chiave in una tabella hash. In entrambi i casi, è consigliabile scrivere la propria implementazione del GetHashCode metodo che rappresenta più da vicino il concetto di codice hash per il tipo.

Per altre informazioni, vedere Object.GetHashCodee System.Collections.Hashtable.

.NET 9 e versioni successive, l'implementazione predefinita di ValueType.GetHashCode genera NotSupportedException se InlineArrayAttribute viene applicato al tipo.

Note su Windows Runtime

Quando si chiama il metodo GetHashCode in una struttura Windows Runtime, fornisce il comportamento predefinito per i tipi valore che non eseguono l'override di GetHashCode. Questo è parte del supporto fornito da .NET per Windows Runtime (vedere supporto .NET per le app di Windows Store e Windows Runtime). Windows Runtime strutture non possono eseguire l'override di GetHashCode, anche se sono scritte con C# o Visual Basic, perché non possono avere metodi. Inoltre, le strutture nel Windows Runtime stesso non ereditano ValueType. Tuttavia, sembrano avere ToString, Equals e GetHashCode metodi quando vengono usati nel codice C# o Visual Basic e .NET fornisce il comportamento predefinito per questi metodi.

Si applica a