C# | Object.GetHashCode() Method with Examples Last Updated : 17 Feb, 2020 Comments Improve Suggest changes Like Article Like Report This method is used to return the hash code for this instance. A hash code is a numeric value which is used to insert and identify an object in a hash-based collection. The GetHashCode method provides this hash code for algorithms that need quick checks of object equality. Syntax: public virtual int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code for the current object. Below programs illustrate the use of Object.GetHashCode() Method: Example 1: CSharp // C# program to demonstrate // Object.GetHashCode() Method using System; class GFG { // Main Method public static void Main() { // declaring an object Object obj = new Object(); // taking Type type and assigning // the value as type of above // defined types using GetType // method Type t = obj.GetType(); // Display type and hash code Console.WriteLine("Type is :{0}", t); Console.WriteLine("Hash Code is :{0}", t.GetHashCode()); } } Output: Type is :System.Object Hash Code is :37162120 Example 2: CSharp // C# program to demonstrate // Object.GetHashCode() Method using System; public class Author { public string f_Name; public string l_Name; public Author(string f_Name, string l_Name) { this.f_Name = f_Name; this.l_Name = l_Name; } public void Show() { Console.WriteLine("first Name : " + f_Name); Console.WriteLine("last Name : " + l_Name); } } // Driver Class class GFG { // Main method public static void Main() { // Creating and initializing // the object of Author class Author aobj = new Author("Kirti", "Mangal"); Console.WriteLine("Author details:"); aobj.Show(); // Get the Hash Code of aobj object // Using GetHashCode() method Console.WriteLine("The hash code of object is: {0}", aobj.GetHashCode()); } } Output: Author details: first Name : Kirti last Name : Mangal The hash code of object is: -751588944 Important Points: Two objects that return different hash codes means objects are not equal but the reverse is not true. Means, equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes. The .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value this method returns may differ between .NET Framework versions and platforms, such as 32-bit and 64-bit platforms. A hash code is not a permanent value so do not serialize, store the hash values in databases etc. Do not test for equality of hash codes to determine whether two objects are equal. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Object.GetHashCode() Method with Examples K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp Object Class Similar Reads SByte.GetHashCode Method in C# with Examples SByte.GetHashCode method is used to get the HashCode for the current SByte instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of the above discussed-method: Example 1: csharp // C# program to il 1 min read Single.GetHashCode() Method in C# with Examples Single.GetHashCode() Method is used to return the hash code for this instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of Single.GetHashCode() Method: Example 1: csharp // C# program to demonst 1 min read UInt32.GetHashCode Method in C# with Examples UInt32.GetHashCode method is used to get the HashCode for the current UInt32 instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of the above discussed-method: Example 1: csharp // C# program to 1 min read UInt16.GetHashCode Method in C# with Examples UInt16.GetHashCode method is used to get the HashCode for the current UInt16 instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of the above discussed-method: Example 1: csharp // C# program to 1 min read UInt64.GetHashCode Method in C# with Examples UInt64.GetHashCode method is used to get the HashCode for the current UInt64 instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of the above discussed-method: Example 1: csharp // C# program to 1 min read Like