No, this is a covariance problem. If you could do:
Dictionary<int, A> dict = new Dictionary<int, B>();
It would be possible if the compiler error did not put an object of type A in the dict.
The problem is that the dict looks like this: Dictionary<int, A> , but actually it is a type of Dictionary<int, B>() (therefore placing an object of type A will lead to a runtime error at runtime due to invalid transfer) so you should not be allowed to even try to put an object of type A in a dict, so you cannot do:
Dictionary<int, A> dict = new Dictionary<int, B>();
This will protect you from a runtime error.
Do you want to check Eric Lippert's blog on: http://blogs.msdn.com/b/ericlippert/
This is one of his favorite topics to talk about, so itβs detailed enough.
kemiller2002 Nov 10 '10 at 16:45 2010-11-10 16:45
source share