
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Int32.GetHashCode Method in C# with Examples
The Int32.GetHashCode() method in C# is used to return the hash code for the current instance.
Syntax
Following is the syntax −
public override int GetHashCode ();
Example
Let us now see an example to implement the Int32.GetHashCode() method −
using System; public class Demo { public static void Main(){ int val1 = Int32.MinValue; int val2 = 300; Console.WriteLine("Value1 = "+val1); Console.WriteLine("Value2 = "+val2); Console.WriteLine("Are they equal? = "+val1.Equals(val2)); Console.WriteLine("HashCode for Value1 = "+val1.GetHashCode()); Console.WriteLine("HashCode for Value2 = "+val2.GetHashCode()); } }
Output
This will produce the following output −
Value1 = -2147483648 Value2 = 300 Are they equal? = False HashCode for Value1 = -2147483648 HashCode for Value2 = 300
Example
Let us now see another example to implement the Int32.GetHashCode() method −
using System; public class Demo { public static void Main(){ int val1 = 50; int val2 = 50; Console.WriteLine("Value1 = "+val1); Console.WriteLine("Value2 = "+val2); Console.WriteLine("Are they equal? = "+val1.Equals(val2)); Console.WriteLine("HashCode for Value1 = "+val1.GetHashCode()); Console.WriteLine("HashCode for Value2 = "+val2.GetHashCode()); } }
Output
This will produce the following output −
Value1 = 50 Value2 = 50 Are they equal? = True HashCode for Value1 = 50 HashCode for Value2 = 50
- Related Articles
- UInt16.GetHashCode() Method in C# with Examples
- UInt64.GetHashCode() Method in C# with Examples
- Int16.GetHashCode() Method in C# with Examples
- Int64.GetHashCode Method in C# with Examples
- UInt32.GetHashCode() Method in C# with Examples
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- Int32.GetTypeCode Method in C# with Examples
- Int32.CompareTo Method in C# with Examples
- Int32. Equals Method in C# with Examples
- Char.GetUnicodeCategory(String, Int32) Method with Examples in C#
- Int32.MaxValue Field in C# with Examples
- Int32.MinValue Field in C# with Examples
- Char.ConvertFromUtf32(Int32) Method in C#
- Char.IsControl(String, Int32) Method in C#
- Char.ConvertToUtf32(String, Int32) Method in C#

Advertisements