title: C# Generic Interface with different return types
body: <p>I have webservice that can return data in multiple formats. For example json and xml. I'm building a simple C# api against this webservice and I would like the methods to be able to return either fully serialized objects from json, raw json or raw xml. For example:</p>
  
  <pre><code>List&lt;Order&gt; GetOrders(int customerId)
  string GetOrders(int customerId)
  XMLDocument GetOrders(customerId)
  
  Customer GetCustomer(int customerId)
  string GetCustomer(int customerId)
  XMLDocument GetCustomer(int customerId)
  </code></pre>
  
  <p>I have an idea about doing a fluent api where you would call a SetFormat() method which would then return a common Interface for the above methods. But I'm stuck on how that interface would look like, since the implementation that returns serialized objects returns objects of different types.</p>
  
  <p>Another simpler solution would to just have the methods that return serialized objects and then add an out paramater like this:</p>
  
  <pre><code>List&lt;Order&gt; GetOrders(int customerId, out string data)
  </code></pre>
  
  <p>but that's not very nice solution I think....</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I prefered the none-generic solution that Sjoerd suggested, I had over-complicated my problem. This is what I ended up doing:</p>
  
  <pre><code>public class ServiceEntity {
      List&lt;Order&gt; GetOrders(int customerId)....
  }    
  public class ServiceJson {
      string GetOrders(int customerId)....
  }
  public class ServiceXml {
      XmlDocument GetOrders(int customerId)....
  }
  </code></pre>
  
  <p>Then a fluent service class like this:</p>
  
  <pre><code>public class Service : IService {
      ....
      public AsJson() { return new ServiceJson(); }
      public AsEntity() { return new ServiceEntity(); }
      public AsXml() { return new ServiceXml(); }
  }
  </code></pre>
  
  <p>Used like this:</p>
  
  <pre><code>string json = Service.New().AsJson().GetCategories(1);
  List&lt;Order&gt; = Service.New().AsEntity().GetCategories(1);
  </code></pre>
  
  <p>Thanks for all the replies!</p>