This is more of an academic inquiry than a practical question. Are there any language or framework features that can, or will in future, allow a heterogeneous typed dcitionary, e.g.
myDict.Add('Name', 'Bill'); myDict.Add('Height', 1.2);
where myDict now contains not two object types as values, but one string and one double? I could then retrieve my double with
double dbl = myDict['Height'];
and expect a double or an exception to be thrown?
Please note: The Name and Height values are not necessarily of the same object.
The only way you’ll be able to do this if you have a custom collection with generic overloads for Add and Get methods. But that would mean you can ask for the wrong type when reading the key out, so it doesn’t gain you much (if anything) over doing the cast yourself when you call your Get method.
However, if you can push the generic type into the key then that could work. Something like (untested code here)
Then you can define your keys like:
and use it like