Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTimeOffset.GetHashCode() Method in C#
The DateTimeOffset.GetHashCode() method in C# returns a hash code for the current DateTimeOffset object. This method is inherited from the Object class and provides a 32-bit signed integer that represents the unique identifier for the DateTimeOffset instance, which is useful for hash-based collections like Dictionary and HashSet.
Syntax
Following is the syntax for the GetHashCode() method −
public override int GetHashCode();
Return Value
This method returns a 32-bit signed integer hash code that represents the current DateTimeOffset object. Two DateTimeOffset objects that are equal will have the same hash code, but objects with the same hash code are not necessarily equal.
Using GetHashCode() with Different DateTimeOffset Objects
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 09, 10, 6, 20, 10, new TimeSpan(-5, 0, 0));
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(2019, 11, 12, 8, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset1 = {0}", dateTimeOffset1);
int hash1 = dateTimeOffset1.GetHashCode();
Console.WriteLine("DateTimeOffset1 HashCode = {0}", hash1);
Console.WriteLine("DateTimeOffset2 = {0}", dateTimeOffset2);
int hash2 = dateTimeOffset2.GetHashCode();
Console.WriteLine("DateTimeOffset2 HashCode = {0}", hash2);
int comparison = DateTimeOffset.Compare(dateTimeOffset1, dateTimeOffset2);
Console.WriteLine("Comparison result: {0}", comparison);
}
}
The output of the above code is −
DateTimeOffset1 = 9/10/2019 6:20:10 AM -05:00 DateTimeOffset1 HashCode = -543087392 DateTimeOffset2 = 11/12/2019 8:20:10 AM -05:00 DateTimeOffset2 HashCode = 64112243 Comparison result: -1
Using GetHashCode() with Single DateTimeOffset Object
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 11, 10, 6, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
int hash = dateTimeOffset.GetHashCode();
Console.WriteLine("DateTimeOffset HashCode = {0}", hash);
// Create another DateTimeOffset with the same values
DateTimeOffset sameDateTime = new DateTimeOffset(2019, 11, 10, 6, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("Same DateTimeOffset HashCode = {0}", sameDateTime.GetHashCode());
Console.WriteLine("Hash codes are equal: {0}", hash == sameDateTime.GetHashCode());
}
}
The output of the above code is −
DateTimeOffset = 11/10/2019 6:20:10 AM -05:00 DateTimeOffset HashCode = -92293937 Same DateTimeOffset HashCode = -92293937 Hash codes are equal: True
Common Use Cases
The GetHashCode() method is commonly used when storing DateTimeOffset objects in hash-based collections such as Dictionary<TKey, TValue> or HashSet<T>. It ensures efficient storage and retrieval of objects by providing a unique identifier for each distinct DateTimeOffset value.
Conclusion
The DateTimeOffset.GetHashCode() method provides a hash code representation of a DateTimeOffset object, which is essential for hash-based collections. Equal DateTimeOffset objects will always return the same hash code, making it reliable for dictionary keys and set membership operations.
