ValueType.GetHashCode Metodo

Definizione

Restituisce il codice hash per l'istanza.

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

Restituisce

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 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();
    }
}
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. Per calcolare il valore restituito viene utilizzato uno o più campi del tipo derivato. 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 viene modificato, il valore restituito potrebbe diventare non 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.GetHashCode e System.Collections.Hashtable.

Note per il Windows Runtime

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

Si applica a