title: Generic lists: The type arguments for method cannot be inferred from the usage in lambdas
body: <p>I got a problem in C#, giving me an error 'The type arguments for method cannot be inferred from the usage'. Seems that the compiler cannot determine the correct interface, if I derive a generic list from a non-generic one:</p>
  
  <p>Code:</p>
  
  <pre><code>public class SpecialItem : BaseItem
  {
      public string Title { get; set; }
  }
  
  public class BaseItem
  {
      public string Name { get; set; }
  }
  
  
  public class GenericList&lt;T&gt; : NongenericBaseList, IEnumerable&lt;T&gt;
      where T: BaseItem
  {
      public new T this[int index]
      {
          get { return _items[index] as T; }
      }
  
      public new IEnumerator&lt;T&gt; GetEnumerator()
      {
          var iter = _items.GetEnumerator();
          while (iter.MoveNext())
          {
              yield return iter.Current as T;
          }
      }
  }
  
  
  public class NongenericBaseList : IEnumerable&lt;BaseItem&gt;
  {
      protected List&lt;BaseItem&gt; _items;
  
      public BaseItem this[int index]
      {
          get { return _items[index]; }
      }
  
      public IEnumerator&lt;BaseItem&gt; GetEnumerator()
      {
          return _items.GetEnumerator();
      }
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>    var genericList = new GenericList&lt;SpecialItem&gt;();
      foreach (var item in genericList)  // Uses IEnmerable&lt;SpecialItem&gt;, OK!
      {
          Console.WriteLine(item.Title);
      }
  
      var l = genericList.ToList();  // ERROR!
  </code></pre>
  
  <p>The ForEarch gets the correct Enumerator (SpecialItem), but the lambda does not know what to use (<code>IEnumerable&lt;BaseItem&gt;</code> or <code>IEnumerable&lt;SpecialItem&gt;</code>).</p>
  
  <p>What to do? How can I set <code>IEnumerable&lt;SpecialItem&gt;</code> as 'default' interface? I dont want to explicetly code the type all the time like this:</p>
  
  <pre><code>var l = genericList.ToList&lt;SpecialItem&gt;();
  </code></pre>