It is because a Dictionary<int, string>
is not a Dictionary<object, object>
.
For that to be true, the class type Dictionary<TKey, TValue>
would need to be covariant in both TKey
and TValue
, and covariance would need to work with value types (boxing).
First of all, in .NET, generic class types are always invariant. Only interface types and delegate types can be co- or contravariant.
Secondly, a Dictionary<,>
is not semantically covariant. For example you could say:
myDict.Add(new Elephant(), new BankCustomer());
and that would not be very pleasant if myDict
was actually a (run-time) Dictionary<int, string>
in a Dictionary<object, object>
variable.
Now, since .NET 4.5, some of the "non-dictionary" types implement covariant interfaces like IReadOnlyList<out T>
. You might hope that the similar interface IReadOnlyDictionary<TKey, TValue>
was covariant too. But it is not (the reason being given by the first comment by Servy below). So there is no hope for you.