title: interface generic return type
body: <p>I have serveral classes with similar method signatures that I wish to capture in an interface:</p>
  
  <pre><code>namespace MyLib
  
  public class ClientList
      public ICollection&lt;Client&gt; Fetch() {
      {
          //do stuff
         return this.CreateCollection();
      }
  
      private ICollection&lt;Client&gt; CreateCollection()
      {
          List&lt;Client&gt; clientList = new List&lt;Client&gt;();
          // populate list
          return clientList;
      }
  
  public class ProductList
      public ICollection&lt;Product&gt; Fetch() {
          //do stuff
         return this.CreateCollection();
      }
  
      private ICollection&lt;Product&gt; CreateCollection()
      {
          List&lt;Product&gt; productList = new List&lt;Product&gt;();
          // populate list
          return productList ;
      }
  </code></pre>
  
  <p>I would like an interface with a method signature for Fetch that returns an ICollection, type undefined (as it will be different for every list). This will ensure each *list object will have a fetch method and new ones won't have 'getList' or other such named calls. After doing a bit of research I believe generics may be the way to go, but I'm unsure how.</p>
  
  <p>I tried</p>
  
  <pre><code>public interface IDataRequest
      ICollection&lt;T&gt; Fetch&lt;T&gt;();
  </code></pre>
  
  <p>but when I implemented this as</p>
  
  <pre><code>public ICollection&lt;Client&gt; Fetch&lt;Client&gt;()
  </code></pre>
  
  <p>I got an error on 'return this.CreateCollection();':</p>
  
  <blockquote>
    <p>Cannot implicitly convert type
    'System.Collections.Generic.ICollection&lt;MyLib.Client&gt;' to
    'System.Collections.Generic.ICollection&lt;Client&gt;'. An explicit
    conversion exists (are you missing a cast?)</p>
  </blockquote>
  
  <p>I thought maybe it was because I wasn't specifying the namespace. but if I changed to</p>
  
  <pre><code>public ICollection&lt;MyLib.Client&gt; Fetch&lt;MyLib.Client&gt;()
  </code></pre>
  
  <p>then I got the error:</p>
  
  <blockquote>
    <p>Type parameter declaration must be an identifier not a type</p>
  </blockquote>
  
  <p>on Fetch&lt;MyLib.Client&gt;()</p>
  
  <p>and finally if I changed it to:</p>
  
  <pre><code>public ICollection&lt;MyLib.Client&gt; Fetch&lt;Client&gt;()
  </code></pre>
  
  <p>then I got the error:</p>
  
  <blockquote>
    <p>'MyLib.ClientList' does not implement interface member 'MyLib.IDataRequest.Fetch()'. 'MyLib.ClientList.Fetch()' cannot implement 'MyLib.IDataRequest.Fetch()' because it does not have the matching return type of 'System.Collections.Generic.ICollection'.</p>
  </blockquote>
  
  <p>I don't have a big knowledge of generics and have reached the limit of what I can try with cargo cult attempts. Is what I want to do possible? If so can you show me both the interface method signature and an example of the class method definition.</p>
  
  <p>As requested in a comment here is the client class:</p>
  
  <pre><code>namespace MyLib
  {
      using System.Data;
      using System.Runtime.Serialization;
  
      [DataContract]
      public class Client
      {
          public Client(DataRow clientRecord)
          {
              this.clientId = clientRecord.Field&lt;string&gt;("ID");
              this.cphh = clientRecord.Field&lt;string&gt;("ID");
              this.name = clientRecord.Field&lt;string&gt;("Name");
              this.address = clientRecord.Field&lt;string&gt;("Address");
  
              if (CommonUtilities.GIS.ValidateOSMapRef(clientRecord.Field&lt;string&gt;("Locationd")))
              {
                  this.location = string.Format(
                      "{0}, {1}",
                      CommonUtilities.GIS.ConvertMapRefToEasting(clientRecord.Field&lt;string&gt;("Locationd")),
                      CommonUtilities.GIS.ConvertMapRefToNorthing(clientRecord.Field&lt;string&gt;("Locationd")));
              }
          }
  
          [DataMember]
          public string clientId { get; private set; }
  
          [DataMember]
          public string name { get; private set; }
  
          [DataMember]
          public string address { get; private set; }
  
          [DataMember]
          public string location { get; private set; }
  
          [DataMember]
          public string cphh { get; private set; }
      }
  }
  </code></pre>