In C#, GetHashCode returns an int built from the ticks of a DateTime. Two values that are equal always have the same hash code.
You rarely call it yourself. Collections such as HashSet<DateTime> and Dictionary<DateTime, T> use it to find values quickly.
This is an instance member of System.DateTime. You call it on a DateTime value, such as dt.GetHashCode(...). DateTime is a struct, so every value is copied and never changed in place.
Syntax
Call GetHashCode() on a DateTime value. It takes no arguments.
C#
DateTime a = new DateTime(2026, 11, 18);
DateTime b = new DateTime(2026, 11, 18);
bool same = a.GetHashCode() == b.GetHashCode(); // true
var set = new HashSet<DateTime> { a, b }; // Count is 1
Call
What it does
a.GetHashCode()
Returns an int for a.
new HashSet<DateTime>()
Uses the hash code to remove duplicates.
new Dictionary<DateTime, string>()
Uses the hash code to find a key quickly.
Do not print or store the number and expect it to stay the same on another .NET version or platform. Use it only in memory, for hash-based collections.
Check that two equal values, even with different Kind, have the same hash code.
C#
using System;
class Program {
static void Main() {
DateTime a = new DateTime(2026, 11, 18, 10, 0, 0);
DateTime b = new DateTime(2026, 11, 18, 10, 0, 0);
DateTime utc = new DateTime(2026, 11, 18, 10, 0, 0, DateTimeKind.Utc);
Console.WriteLine(a.GetHashCode() == b.GetHashCode());
Console.WriteLine(a.GetHashCode() == utc.GetHashCode());
}
}
Output
True
True
How It Works
Equal values.a and b hold the same ticks.
Same hash. Equal values must give equal hash codes, so the first line is True.
Kind is not hashed. The Utc value has the same ticks, so its hash matches too.
Comparison only. The code compares two numbers and never prints the raw hash, which can vary.
Example 2 — Remove Duplicate Dates
Add three dates, two equal, to a HashSet.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var set = new HashSet<DateTime>();
Console.WriteLine(set.Add(new DateTime(2026, 11, 18)));
Console.WriteLine(set.Add(new DateTime(2026, 11, 19)));
Console.WriteLine(set.Add(new DateTime(2026, 11, 18)));
Console.WriteLine(set.Count);
}
}
Output
True
True
False
2
How It Works
Add returns bool.true means the value was new, false means it was already there.
Two new dates. November 18 and 19 are different, so both adds print True.
Duplicate. The third date equals the first, so the set refuses it and prints False.
Count is 2. The set uses the hash code and then Equals to find the match.
Example 3 — Use a Date as a Dictionary Key
Store events by date and read one back.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var events = new Dictionary<DateTime, string> {
{ new DateTime(2026, 11, 18), "Release day" },
{ new DateTime(2026, 12, 25), "Holiday" }
};
Console.WriteLine(events[new DateTime(2026, 11, 18)]);
Console.WriteLine(events.ContainsKey(new DateTime(2026, 11, 19)));
}
}
Output
Release day
False
How It Works
Date keys. Each event is stored under a DateTime key.
Lookup with a new object. A brand new DateTime with the same value finds the stored entry.
Hash then Equals. The dictionary uses GetHashCode to choose a bucket and Equals to confirm.
Missing key. November 19 was never added, so ContainsKey prints False.
Frequently Asked Questions
Yes. If a.Equals(b) is true, their hash codes are equal.
No. It uses ticks only, so Utc and Local with the same ticks match.
Yes. Dictionary and HashSet use GetHashCode and Equals for DateTime.
Do not rely on it. The value is an implementation detail and can change between .NET versions.
Yes, collisions are possible. Collections handle this by also calling Equals.