1

Important edit: Do not use a Dictionary in this context. It uses Object.GetHashCode() which isn't good for use as a dictionary key. I still learned something from this though.

I have the following declaration

private static IDictionary<IDictionary<string,string>, IEnumerable<Fishtank>> _cache;

and the following code to attempt to initialize it

if (_cache == null) 
  _cache = new Dictionary<Dictionary<string,string>, LinkedList<Fishtank>>(); 

The problem I'm having is the initialization generates a compile time error.

Cannot implicitly convert type 'System.Collections.Generic.Dictionary,System.Collections.Generic.LinkedList>' to 'System.Collections.Generic.IDictionary,System.Collections.Generic.IEnumerable>'. An explicit conversion exists (are you missing a cast?)

I know that Dictionary and LinkedList implement IDictionary and IEnumerable respectively, so I'm at a bit of a loss here.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • 1
    This is because IDictionary does not support covariance. http://stackoverflow.com/questions/2149589/idictionarytkey-tvalue-in-net-4-not-covariant – Jim Skerritt Aug 23 '13 at 03:03
  • I see, I had thought I would always be able to cast to an implemented interface - thanks for the link – jdphenix Aug 23 '13 at 03:25
  • I added an edit because I discovered that using a Dictionary in the context I was (as a Dictionary key) doesn't work properly – jdphenix Aug 23 '13 at 05:10

1 Answers1

3

you can do as below

if (_cache == null) 
    _cache = new Dictionary<IDictionary<string, string>, IEnumerable<Fishtank>>();
Damith
  • 62,401
  • 13
  • 102
  • 153