Goal (c#/Blazor enviro):
Singleton with a "registerListener" method. Object instances call as such "aSingleton.registerListener(this)"
registerListener method stores each registration in a Dictionary<int, AnInterfaceImplInstance>
don't require callers of registerListener to specify a unique Id/key
allow multiple object instances of the same type to call registerListener, and be notified (on a callback)
A possible solution: in the registerListener method use the value returned from passed param's GetHashCode() method as the key for the Dictionary
however, MS seems to be doc'ing out of both sides of it's metaphorical mouth (https://learn.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=net-5.0):
A hash code is intended for efficient insertion and lookup in collections that are based on a hash table. A hash code is not a permanent value. For this reason:
and then later cites:
Do not use the hash code as the key to retrieve an object from a keyed collection.
WTH? Anyone with C# internals knowledge able to say whether or not I can use GetHashCode() of an object instance to 100% reliably get a ref back to that object instance from a Dictionary key? (note, none of the object instances overrides GetHashCode(), so whatever's returns from Object will be what's used)
Thanks in advance for your time.
Blockquote