C# Char GetHashCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Char

Definition

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();
CallWhat 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

ParameterDescription
NoneGetHashCode() takes no parameters.

Return Value

Return ValueDescription
intA 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);
    }
}

How It Works

  1. Hash call. GetHashCode returns an int for the char.
  2. Often matches code. For a simple char, the hash commonly mirrors the code unit.
  3. Print both. Comparing hash and (int)c helps beginners connect the ideas.
  4. 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));
    }
}

How It Works

  1. Equal values. Both variables store Z.
  2. Matching hashes. Equality implies equal hash codes.
  3. Equals confirms. Hash equality alone is not enough in general; Equals seals it.
  4. Dictionary ready. This is why char keys work in hash maps.

Example 3 — Use Char as a Key

Count letters with a dictionary keyed by char.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var counts = new Dictionary<char, int>();
        string word = "book";

        foreach (char ch in word) {
            if (!counts.ContainsKey(ch)) counts[ch] = 0;
            counts[ch]++;
        }

        Console.WriteLine("o -> " + counts['o']);
        Console.WriteLine("b -> " + counts['b']);
    }
}

How It Works

  1. Char keys. Dictionary uses GetHashCode and Equals behind the scenes.
  2. Tally letters. Each character updates its count.
  3. Two o's. The letter o appears twice in book.
  4. No manual hash. You rarely call GetHashCode yourself in app code.

Frequently Asked Questions

Hash-based collections call it when a char is a key or set element.
Yes. That is required for correct dictionary behavior.
No. Different chars can collide; equality still decides true matches.
No. Use equality and collection APIs instead.
Yes. Call it on a char value.

Next: GetNumericValue()

Return the numeric value of a digit character, or -1 when it has none with System.Char.

GetNumericValue() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful