# ValueType.GetHashCode 方法

## 定義

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

#### 傳回

Int32

32 位元帶正負號的整數，其為這個執行個體的雜湊碼。

## 範例

``````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
``````