简体   繁体   中英

Cannot implicitly convert type from non-covariant generic interface

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.

you can do as below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM