In C#, GetHashCode produces an integer hash for a char. Equal characters produce equal hash codes.
You rarely call it yourself—dictionaries and hash sets call it when a char is used as a key. Knowing it exists helps when you debug hashing behavior.
This is an instance member of System.Char. You call it on a char value with a dot, such as c.GetHashCode(...). A char holds one 16-bit UTF-16 code unit.
Syntax
Call on a char value.
C#
char c = 'A';
int h = c.GetHashCode();
Call
What it does
c.GetHashCode()
Returns an int hash for c.
Do not rely on a specific numeric formula across .NET versions; rely on equality consistency.
Library declarations
C#
public override int GetHashCode();
Parameters
Parameter
Description
None
GetHashCode() takes no parameters.
Return Value
Return Value
Description
int
A hash code for this character.
C# Examples
Example 1 — Print a Hash Code
Show the hash for a letter.
C#
using System;
class Program {
static void Main() {
char c = 'A';
Console.WriteLine("char: " + c);
Console.WriteLine("hash: " + c.GetHashCode());
Console.WriteLine("code: " + (int)c);
}
}
Output
char: A
hash: 65
code: 65
How It Works
Hash call.GetHashCode returns an int for the char.
Often matches code. For a simple char, the hash commonly mirrors the code unit.
Print both. Comparing hash and (int)c helps beginners connect the ideas.
Implementation detail. Treat the exact number as illustrative, not a contract to hard-code.
Example 2 — Same Char, Same Hash
Two equal letters share a hash.
C#
using System;
class Program {
static void Main() {
char a = 'Z';
char b = 'Z';
Console.WriteLine(a.GetHashCode() == b.GetHashCode());
Console.WriteLine(a.Equals(b));
}
}