Created
January 24, 2023 09:49
-
-
Save jeroenlandheer/adf9808ca0b9aad6512b6b94f88c64c1 to your computer and use it in GitHub Desktop.
GetHashcode unique? I don't think so...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello, World!"); | |
HashSet<NastyEquals> uglyHash = new HashSet<NastyEquals>(); | |
var myHash = new NastyEquals(); | |
for (int i = 0; i < 10; i++) | |
{ | |
uglyHash.Add(new NastyEquals()); | |
} | |
uglyHash.Add(myHash); | |
Console.WriteLine("Contains: {0}", uglyHash.Contains(myHash)); | |
} | |
} | |
internal class NastyEquals | |
{ | |
private Guid internalId; | |
public NastyEquals() | |
{ | |
internalId = Guid.NewGuid(); | |
} | |
public override bool Equals(object? obj) | |
{ | |
if (obj == null) return false; | |
if (obj.GetType() != typeof(NastyEquals)) return false; | |
Console.WriteLine($"Called: NastyEquals.Equals on object {internalId}"); | |
return (((NastyEquals)obj).internalId == internalId); | |
} | |
public override int GetHashCode() | |
{ | |
Console.WriteLine("Called: NastyEquals.GetHashCode"); | |
// Just mess this up. | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment