C#
String Manipulation
HashCode
.NET
String Methods

Does String.GetHashCode consider the full string or only part of it?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

String.GetHashCode() in .NET conceptually hashes the entire string, not just the beginning or a small fixed prefix. The runtime may process characters in chunks internally for speed, but every character can influence the final hash code.

That said, the exact algorithm is an implementation detail. You should understand what the method guarantees, but you should not build logic that depends on a particular numeric hash value staying stable forever.

The Whole String Contributes to the Hash

A hash code is meant to summarize the contents of a string so that hash-based collections can distribute keys efficiently. If only the first few characters mattered, strings that differ near the end would cluster badly and hash tables would perform poorly.

You can see that changing a late character changes the hash:

csharp
1using System;
2
3string first = "customer-order-0001-A";
4string second = "customer-order-0001-B";
5
6Console.WriteLine(first.GetHashCode());
7Console.WriteLine(second.GetHashCode());

The values are very likely to differ because the full content influences the result. That does not mean collisions are impossible. It only means the algorithm is designed to account for the whole string, not only a slice of it.

Hash Code Does Not Mean Unique Identity

Even though the whole string contributes, two different strings can still produce the same hash code. That is a normal property of hashing because an int has far fewer possible values than the set of all possible strings.

This is why Dictionary<string, T> does not rely on hash codes alone. It uses the hash code to find a bucket and then checks equality to distinguish true matches from collisions.

csharp
1using System;
2using System.Collections.Generic;
3
4var counts = new Dictionary<string, int>();
5counts["apple"] = 1;
6counts["banana"] = 2;
7
8Console.WriteLine(counts["apple"]);

The collection remains correct even if collisions occur, because equality comparison is the final authority.

Do Not Persist or Compare Hash Codes Across Runs

A more subtle point is that GetHashCode() is not intended as a stable external identifier. On modern .NET runtimes, string hash codes may be randomized across processes for security reasons, and the implementation can change across framework versions or platforms.

That means code like this is a bug waiting to happen:

csharp
File.WriteAllText("saved-hash.txt", "hello".GetHashCode().ToString());

You should not:

  • store string hash codes in a database as permanent IDs
  • compare hash codes generated on different machines and expect them to match
  • use GetHashCode() for cryptographic or security-sensitive purposes

If you need a stable digest, use a real hashing algorithm from System.Security.Cryptography instead.

Performance Implications

Because the hash depends on the content, computing it is generally proportional to the string length. In big-O terms, that is linear in the number of characters. The runtime may optimize the implementation heavily, but the important practical takeaway is that hashing a short string is cheaper than hashing a very long one.

In normal application code, this is rarely a problem. The method is designed for frequent use in collections and is fast enough for that purpose. The only mistake is assuming it is free or assuming the runtime can ignore most of the string.

What You Can Safely Assume

You can safely treat GetHashCode() like this:

  • equal strings in the same execution produce the same hash code
  • the whole string content contributes to the result
  • unequal strings may still collide
  • the numeric value is not a stable contract across executions

That is the right mental model when using strings as dictionary keys or when implementing your own value types that combine string fields into a hash code.

Common Pitfalls

  • Assuming only the first part of the string matters. The runtime is designed to hash based on full content.
  • Treating a matching hash code as proof that two strings are equal. Collisions are always possible.
  • Persisting GetHashCode() results and expecting them to stay the same across runs or platforms.
  • Using GetHashCode() as a security hash. It is not a cryptographic primitive.
  • Depending on a specific numeric result from one runtime version in tests or serialized data.

Summary

  • 'String.GetHashCode() conceptually considers the full string, not just a prefix.'
  • Different strings can still share a hash code because collisions are unavoidable.
  • Hash codes are useful for in-memory lookup structures, not for permanent identity.
  • Do not assume the numeric value stays stable across executions or framework versions.
  • If you need a stable or secure digest, use a dedicated hashing algorithm instead of GetHashCode().

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.