C# String GetHashCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, GetHashCode() returns an int hash for the string content. Equal strings should produce equal hash codes under the same rules.

You call it with no parameters. The hash is not a unique ID. Do not save it as a permanent key across process runs.

Syntax

Call GetHashCode on the string. Start with this line.

C#
int h = text.GetHashCode();
CallWhat it does
text.GetHashCode()Returns an int hash for the characters in text.

If you customize equality, keep Equals and GetHashCode in sync, especially for Dictionary keys.

Library declarations
C#
public override int GetHashCode();
public int GetHashCode(StringComparison comparisonType);

Parameters

ParameterDescription
comparisonTypeOptional. Rules that should match the Equals comparison you use.

Return Value

Return ValueDescription
intA hash code for the string content.

C# Examples

Example 1 — Same Content, Same Hash

Two strings with identical text produce the same hash within this process.

C#
using System;

class Program {
    static void Main() {
        string a = "Hello";
        string b = "Hello";

        int hashA = a.GetHashCode();
        int hashB = b.GetHashCode();

        Console.WriteLine($"Same text? {a == b}");
        Console.WriteLine($"Same hash? {hashA == hashB}");
    }
}

How It Works

  1. Identical content. a and b both hold "Hello".
  2. Call GetHashCode. Each call returns an int based on the characters.
  3. Compare hashes. Because the strings are equal, the hashes must match.
  4. Do not store the int. Exact values can change across runs/versions—compare relationships instead.

Example 2 — Hash Is Not Enough for Equality

Matching hashes do not prove two strings are equal. Always finish with Equals (or ==).

C#
using System;

class Program {
    static void Main() {
        string a = "Alpha";
        string b = "Beta";

        bool sameHash = a.GetHashCode() == b.GetHashCode();
        bool sameText = string.Equals(a, b);

        Console.WriteLine($"Same hash? {sameHash}");
        Console.WriteLine($"Equals?    {sameText}");

        // Safe pattern:
        bool maybeEqual = sameHash && sameText;
        Console.WriteLine($"Really equal? {maybeEqual}");
    }
}

How It Works

  1. Different strings. "Alpha" and "Beta" are not equal.
  2. Hashes usually differ. Output shows Same hash? False in typical runs (a collision would still be allowed).
  3. Equals is decisive. Content equality is false.
  4. Safe pattern. Treat hash as a filter; confirm with Equals.

Example 3 — Dictionary Uses Hash + Equals

String keys in a dictionary rely on GetHashCode internally—you rarely call it yourself.

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

class Program {
    static void Main() {
        var scores = new Dictionary<string, int>();

        scores["Ada"] = 95;
        scores["Grace"] = 98;

        string key = "Ada";
        Console.WriteLine($"Score for {key}: {scores[key]}");
        Console.WriteLine($"Contains \"Ada\"? {scores.ContainsKey("Ada")}");
    }
}

How It Works

  1. Store with a string key. scores["Ada"] = 95 hashes "Ada" into a bucket.
  2. Lookup repeats the hash. scores[key] finds the bucket, then uses Equals.
  3. ContainsKey does the same. Fast check without exposing hash numbers.
  4. Your job: keep keys immutable (strings already are) and use consistent equality rules.

Frequently Asked Questions

GetHashCode() returns an int hash for the string content.
The common override takes none. Some overloads accept StringComparison.
An int hash code. Equal strings should hash equally under the same rules.
No. Different strings can share a hash, and values can change across process runs.
Dictionary and HashSet rely on both. If they disagree, lookups break.

Next: GetType()

Learn how to inspect the runtime type of a string.

GetType() 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.

9 people found this page helpful