C# DateTime GetHashCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

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
CallWhat 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.

Library declarations
C#
public override int GetHashCode();

Parameters

ParameterDescription
NoneGetHashCode() takes no parameters.

Return Value

Return ValueDescription
intA 32-bit hash code. Equal DateTime values return equal hash codes.

C# Examples

Example 1 — Equal Dates Share a Hash

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

How It Works

  1. Equal values. a and b hold the same ticks.
  2. Same hash. Equal values must give equal hash codes, so the first line is True.
  3. Kind is not hashed. The Utc value has the same ticks, so its hash matches too.
  4. 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);
    }
}

How It Works

  1. Add returns bool. true means the value was new, false means it was already there.
  2. Two new dates. November 18 and 19 are different, so both adds print True.
  3. Duplicate. The third date equals the first, so the set refuses it and prints False.
  4. 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)));
    }
}

How It Works

  1. Date keys. Each event is stored under a DateTime key.
  2. Lookup with a new object. A brand new DateTime with the same value finds the stored entry.
  3. Hash then Equals. The dictionary uses GetHashCode to choose a bucket and Equals to confirm.
  4. 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.

Next: IsDaylightSavingTime()

Check if a DateTime falls in daylight saving time for the local time zone with System.DateTime.

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

7 people found this page helpful