title: c# Generics: Different Return Types using Generics
body: <p>I have a Data access library which I would like to return different data formats (XML, Json, DataTable).  I was trying to use Generics to achieve this.</p>
  
  <pre><code>public interface IDBInteractor&lt;T&gt;
  {
      T ExecuteDSQuery(string myQuery)
  }
  
  public class DBInteractorDT : IDBInteractor&lt;DataTable&gt;
  {
      public DataTable ExecuteDSQuery(string myQuery)
      {
          return new DataTable();
      }
  }
  
  public class DBInteractorJson : IDBInteractor&lt;JsonString&gt;
  {
      public JsonString ExecuteDSQuery(string myQuery)
      {
          return new JsonString();
      }
  }
  </code></pre>
  
  <p>I am having trouble invoking the correct method. Ideally I would to declare something like</p>
  
  <pre><code>     SomeClass&lt;DataTable&gt; dt = new SomeClass&lt;DataTable&gt;();
       SomeClass&lt;JsonString&gt; js = new SomeClass&lt;JsonString&gt;();
  
       DataTable myDT = dt.ExecuteDSQuery(myQuery);
       JsonString myJson = js.ExecuteDSQuery(myQuery);
  </code></pre>
  
  <p>I'm not sure how to declare SomeClass. I know I could do something like</p>
  
  <pre><code>    public class SomeClass&lt;T&gt; where T : IDBInteractor &lt;T&gt;
      {
          public T ExecuteQuery(T dtobject, string myQuery)
          {
             return dtobject.ExecuteDSQuery(myQuery);
          }
      }
  </code></pre>
  
  <p>But I don't want to have to pass an object instance (dtobject) to each method call.</p>