C# String GetHashCode() Method

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.
int h = text.GetHashCode();
| Call | What 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
public override int GetHashCode();
public int GetHashCode(StringComparison comparisonType);
Parameters
| Parameter | Description |
|---|
| comparisonType | Optional. Rules that should match the Equals comparison you use. |
Return Value
| Return Value | Description |
|---|
| int | A 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.
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}");
}
}
Same text? True
Same hash? True
How It Works
- Identical content.
a and b both hold "Hello". - Call GetHashCode. Each call returns an
int based on the characters. - Compare hashes. Because the strings are equal, the hashes must match.
- 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 ==).
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}");
}
}
Same hash? False
Equals? False
Really equal? False
How It Works
- Different strings.
"Alpha" and "Beta" are not equal. - Hashes usually differ. Output shows
Same hash? False in typical runs (a collision would still be allowed). - Equals is decisive. Content equality is
false. - 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.
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")}");
}
}
Score for Ada: 95
Contains "Ada"? True
How It Works
- Store with a string key.
scores["Ada"] = 95 hashes "Ada" into a bucket. - Lookup repeats the hash.
scores[key] finds the bucket, then uses Equals. - ContainsKey does the same. Fast check without exposing hash numbers.
- 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.
About the author
Developer, cloud engineer, and technical writer
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