I can't for the life of me figure this out. Say I have the following two dictionary objects:
// Assume "query" is a LINQ queryable.
Dictionary<string, int> d1 = query.ToDictionary(k => k.Key, v => v.Value);
Dictionary<string, int> d1 = query.ToDictionary(k => k.Key, v => v.Value);
The following statement produces a compile time error that an implicit conversion between a Dictionary and IDictionary is not possible:
// Compile time error!
Tuple<IDictionary<string, int>, IDictionary<string, int>> = Tuple.Create(d1, d2);
I have to do the conversion explicitly:
Tuple<IDictionary<string, int>, IDictionary<string, int>> = Tuple.Create(d1 as IDictionary<string, int>, d2 as IDictionary<string, int>);
I am not understanding why the compiler can't figure out the covariance operation - Dictionary implements IDictionary - especially since something like this will of course work as we all know:
IDictionary<string, int> d3 = d1;
I am sure there is a good reason for this behavior and I am curious what it is.
Update 1: Just to clarify, I am curious about the behavior not how to solve the problem. I am aware of the different solutions :)
Update 2:
Thank you all for the great answers. I did not know Tuple
was invariant and now I do.