Skip to content

Commit 65e8e1e

Browse files
committed
Fix Location hash stability
Root cause: Location.GetHashCode accidentally combined latitude with itself, so longitude never influenced the hash for this public value-like core type.
1 parent 55506b2 commit 65e8e1e

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ samples/
6868
- Use modern C# features where the target frameworks support them
6969
- Follow SOLID, DRY principles; remove unused code and parameters
7070
- Clear, descriptive naming; prefer explicit over clever
71+
- For existing public value-like types, prefer additive equality fixes over record conversions unless an API shape change is explicitly intended
7172
- Handle cancellation tokens properly: pass through call chains
7273
- Always dispose resources: use `using` statements
7374

src/Geocoding.Core/Location.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ public bool Equals(Location? coor)
139139
/// <returns>A hash code for this location.</returns>
140140
public override int GetHashCode()
141141
{
142-
return Latitude.GetHashCode() ^ Latitude.GetHashCode();
142+
unchecked
143+
{
144+
return (Latitude.GetHashCode() * 397) ^ Longitude.GetHashCode();
145+
}
143146
}
144147

145148
/// <summary>

test/Geocoding.Tests/LocationTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ public void Equals_SameCoordinates_ReturnsTrue()
3131
Assert.Equal(loc1.GetHashCode(), loc2.GetHashCode());
3232
}
3333

34+
[Fact]
35+
public void GetHashCode_IncludesLongitudeHash()
36+
{
37+
// Arrange
38+
Location loc = new Location(85.6789, 92.4517);
39+
int expectedHashCode = unchecked((loc.Latitude.GetHashCode() * 397) ^ loc.Longitude.GetHashCode());
40+
41+
// Assert
42+
Assert.Equal(expectedHashCode, loc.GetHashCode());
43+
}
44+
3445
[Fact]
3546
public void DistanceBetween_TwoLocations_ReturnsSameDistanceBothDirections()
3647
{

0 commit comments

Comments
 (0)