title: using a dictionary of string and interface in C#
body: <p>I have an Interface called as IObSrc, I have few classes that implement this interface called as ObSrc1, ObScr2 etc..</p>
  
  <pre><code>Interface IObSrc
  {
    string id;
  }
  </code></pre>
  
  <p>I also have an interface called as IDtSrc, this interface inturn is implemented by a few classes called as DtSrc1, DtSrc2.</p>
  
  <pre><code>Interface IDtSrc
  {
    IObSrc getObject();
    Dictionary&lt;string,IObSrc&gt; getObjects();
  }
  </code></pre>
  
  <p>When i implement this interface from DtSrc1, and I return ObSrc1 from DtSrc1, there is no issue, but when i return <code>Dictionary&lt;string,ObSrc1&gt;</code> or <code>Dictionary&lt;string,ObSrc2&gt;</code>, i get an indication of typemismatch.</p>
  
  <pre><code>public class DtSrc : IDtSrc
  {
    public Dictionary&lt;string,IObSrc&gt; getObjects()
    {
      Dictionary&lt;string,ObSrc1&gt; val1 = new ...'
      ...
      return val1;
    }
  
  }
  </code></pre>
  
  <p>How can i solve this issue.</p>
  
  <p><strong>* EDITED *</strong></p>
  
  <p>I have one more query, what if a method should return a generic type that can be varied by the implementation. </p>
  
  <p><strong>Example</strong>: </p>
  
  <pre><code>interface IDtSrc&lt;T&gt; where T:IObSrc
  { 
    T ExecuteObject(string objId);
  }
  </code></pre>
  
  <p>This will be used as </p>
  
  <pre><code>class DtSrc1 
  { 
    string ExecuteObject(string objId)
    {..}
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>class DtSrc2
  { 
   Dictionary&lt;string,string&gt; ExecuteObject(string objId)
   {..}
  }
  </code></pre>
  
  <p>Kindly suggest a right procedure for this also.</p>