Export (0) Print
Expand All

ValueType.GetHashCode Method

Returns the hash code for this instance.

[Visual Basic]
Overrides Public Function GetHashCode() As Integer
[C#]
public override int GetHashCode();
[C++]
public: int GetHashCode();
[JScript]
public override function GetHashCode() : int;

Return Value

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

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 one or more of those fields contains a mutable value, the return value might be unpredictable, and unsuitable for use as a key in a hash table. In that case, consider writing your own implementation of GetHashCode that more closely represents the concept of a hash code for the type.

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

Example

The following example demonstrates how the GetHashCode method can be overridden by a derived value type.

[Visual Basic] 
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

[C#] 
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();
    }
}

[C++] 
public __gc struct Complex {
   double  m_Re;
   double  m_Im;
   bool Equals(Object* ob) {
      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;
      }
   }
   int GetHashCode() {
      return  __box(m_Re)->GetHashCode() ^  __box(m_Im)->GetHashCode();
   }
};

[JScript] 
public class Complex 
{
    public var m_Re : double;
    public var m_Im : double;

    public override function Equals( ob ) : Boolean{
        if( ob.GetType() == Complex ) {
            var c : Complex = Complex(ob);
            return m_Re==c.m_Re && m_Im==c.m_Im;
        }
        else {
            return false;
        }
    }

    public override function GetHashCode() : int{
        return m_Re.GetHashCode() ^ m_Im.GetHashCode();
    }
            
            public static function main() {
                   var x : Complex = new Complex();
                   x.m_Re = 1;
                   x.m_Im = 2;
                   var y : Complex = new Complex();
                   y.m_Re = 2;
                   y.m_Im = 1;
                   
                   Console.Write(x.Equals(y));                       
            }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

ValueType Class | ValueType Members | System Namespace

Show:
© 2014 Microsoft