Because Person
is a class, you are only putting a reference into the list, so each list will have a reference to the same person. The Person
object will indeed not be duplicated, and names["juan"]
will de-reference the original object.
However! That doesn't make the code faultless:
- there might be more than one person aged 23;
SortedList<,>
won't like that - changing the age via
names["juan"]
won't automatically updateages
;ages[24]
will fail
If Person
was a struct
, then the Person
would be copied every time you assign it (but: this is not a good candidate for a struct
; don't do that)