title: Generic Method Constraint error - Consider using an explicit interface implementation instead
body: <p>I have this method:</p>
  
  <pre><code>public T GetRepositoryByType&lt;T&gt;() where T : IContextDependent
  {
      var instance = this.Repositories.SingleOrDefault(x =&gt; x is T);
  
      return (T)instance;
  }
  </code></pre>
  
  <p>Which needs to be able to return any repository instance, that implements <code>IContextDependent</code>, so I can set the DbContext for each manually.</p>
  
  <p>When I build I get this error:</p>
  
  <blockquote>
    <p>Error CS0425  The constraints for type parameter 'T' of method
    'RepositoryProvider.GetRepositoryByType()' must match the
    constraints for type parameter 'T' of interface method
    'IRepositoryProvider.GetRepositoryByType()'. Consider using an
    explicit interface implementation instead.</p>
  </blockquote>
  
  <p>However, if I consider this code from <a href="https://msdn.microsoft.com/en-us/library/twcad0zb.aspx" rel="nofollow">MSDN</a>:</p>
  
  <pre><code>void SwapIfGreater&lt;T&gt;(ref T lhs, ref T rhs) where T : System.IComparable&lt;T&gt;
  {
      T temp;
      if (lhs.CompareTo(rhs) &gt; 0)
      {
          temp = lhs;
          lhs = rhs;
          rhs = temp;
      }
  }
  </code></pre>
  
  <p>I see no immediate difference (unless the further generics dependency is necessary?).</p>
  
  <p>FYI, the interface is pretty simple:</p>
  
  <pre><code>public interface IContextDependent
  {
      void SetContext(MyEntities context);
  }
  </code></pre>
  
  <p>And the IRepositoryProvider interface:</p>
  
  <pre><code>public interface IRepositoryProvider
  {
      T GetRepositoryByType&lt;T&gt;();
  }
  </code></pre>
  
  <p>What causes the error?</p>