Yes, it is possible, as long as the corresponding Equals()
method of your key returns true
, and GetHashCode()
returns the same integer value for what you call different but identical instances
.
In your example the result is 1.0 because String.Equals
compares contents of the strings, not references (although, to be precise, references in your example will most probably be the same as well, because the compiler usually pools identical strings).
EDIT: this paragraph applied to the original question, where the OP used string
as the key.
If you use custom class for keys, just override its Equals
method to achieve desired behavior. Don't forget to override GetHashCode()
as well for efficient dictionary lookup, see this for reasoning: Why is it important to override GetHashCode when Equals method is overridden?