title: How to implicitly convert type 'System.Collections.Generic.Dictionary>' to 'System.Collections.Generic.IDictionary>'?
body: <p>When I assign a <code>Dictionary&lt;int, HashSet&lt;int&gt;&gt;</code> to a <code>IDictionary&lt;int, IEnumerable&lt;int&gt;&gt;</code>, I get the following error:</p>
  
  <blockquote>
    <p>Compilation error (line 27, col 9): Cannot implicitly convert type 'System.Collections.Generic.Dictionary>' to 'System.Collections.Generic.IDictionary>'. An explicit conversion exists (are you missing a cast?)</p>
  </blockquote>
  
  <p>The error is quite clear. A cast is missing. Why do I need a cast here? <code>Dictionary&lt;T,U&gt;</code> implements <code>IDictionary&lt;T,U&gt;</code> and <code>HashSet&lt;T&gt;</code> implements <code>IEnumerable&lt;T&gt;</code>.</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Linq;
  using System.Collections.Generic;
  
  public class Program
  {   
      public class A
      {
          public IDictionary&lt;int, IEnumerable&lt;int&gt;&gt; D { get; set; }
          public IEnumerable&lt;int&gt; H { get; set; }
      }
  
      public static void Main()
      {
          var hashSet = new HashSet&lt;int&gt;{1,2,1};
          var a = new A { H = hashSet };
          PrintCollection(a.H);
  
          var d = new Dictionary&lt;int, HashSet&lt;int&gt;&gt;{{ 3, hashSet  }};
          a.D = d; // error here
          PrintCollection(a.D.First().Value);
      }
  
      public static void PrintCollection(IEnumerable&lt;int&gt; collections)
      {
          foreach (var item in collections)
              Console.WriteLine(item);
      }
  }
  </code></pre>
  
  <p><a href="https://dotnetfiddle.net/gVFzIn" rel="nofollow noreferrer">Try it Online!</a></p>