Muokkaa

Jaa


ValueType.GetHashCode Method

Definition

Returns the hash code for this instance.

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

Returns

A 32-bit signed integer that is the hash code for this instance.

Examples

The following example demonstrates how the GetHashCode 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();
    }
}
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

Remarks

The GetHashCode method applies to types derived from ValueType. One or more fields of the derived type is used to calculate the return value. If you call the derived type's GetHashCode method, the return value is not likely to be suitable for use as a key in a hash table. Additionally, if the value of one or more of those fields changes, the return value might become unsuitable for use as a key in a hash table. In either case, consider writing your own implementation of the GetHashCode method that more closely represents the concept of a hash code for the type.

For more information, see Object.GetHashCode, and System.Collections.Hashtable.

.NET 9 and later, the default implementation of ValueType.GetHashCode throws NotSupportedException if InlineArrayAttribute is applied to the type.

Notes for the Windows Runtime

When you call the GetHashCode method on a Windows Runtime structure, it provides the default behavior for value types that don't override GetHashCode. This is part of the support that .NET provides for the Windows Runtime (see .NET Support for Windows Store Apps and Windows Runtime). Windows Runtime structures can't override GetHashCode, even if they're written with C# or Visual Basic, because they can't have methods. (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 .NET provides the default behavior for these methods.

Applies to