- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Object.GetHashCode() Method with Examples
The Object.GetHashCode() method in C# is used to serve as the default hash function.
Syntax
public virtual int GetHashCode ();
Example
Let us now see an example -
using System; public class Demo { public static void Main() { Object ob = new Object(); String str = "Jim"; Type type1 = ob.GetType(); Type type2 = str.GetType(); Console.WriteLine("Hash Code = "+type1.GetHashCode()); Console.WriteLine("Hash Code = "+type2.GetHashCode()); } }
Output
Hash Code =30015890 Hash Code =21083178
Example
Let us now see another example:
using System; public struct Value { private int v1; private int v2; private int v3; public Value(int v1, int v2, int v3) { this.v1 = v1; this.v2 = v2; this.v3 = v3; } public override int GetHashCode() { return Tuple.Create(v1, v2, v3).GetHashCode(); } } public class Demo { public static void Main() { Value v = new Value(1, 7, 12); Console.WriteLine(v.GetHashCode()); v = new Value(12, 8, 7); Console.WriteLine(v.GetHashCode()); v = new Value(8, 7, 12); Console.WriteLine(v.GetHashCode()); } }
Output
This will produce the following output -
1258 12803 8931
Advertisements