title: C# - Generic method calling method which returns concrete type?
body: <p>Say you have a method that takes a Generic type, and e.g. an enum eType as well as a concrete object. The Method then determines which method to call based on the enum Type. Each private method being called knows its own return type (instantiates a concrete typed class based on values from the object being passed in). The public method doesn't know beforehand which type it will return.</p>
  
  <pre><code>/// A Generic method to call individual methods depending on the given enum
  /// and returns a Generic type.
  public T GetSomething&lt;T&gt;(enum eType, Model myClass) where T : class
  {
      // Do some common tasks here before calling the private methods
      // ... 
      switch(eType)
      {
          case eType.A:
              return GetMethodA&lt;T&gt;(myClass);
          case eType.B:
              return GetMethodB&lt;T&gt;(myClass);
      }
  }
  
  /// A method that returns a ConcreteTypeA object
  private T GetMethodA&lt;T&gt;(MyClass myClass) 
  {
      ConcreteTypeA cta = new ConcreteTypeA(); 
      cta.X = myClass.X;
      //... etc.
      return cta;
  }
  
  /// A method that returns a ConcreteTypeA object
  private T GetMethodB&lt;T&gt;(MyClass myClass)
  { 
      ConcreteTypeB ctb = new ConcreteTypeB(); 
      ctb.Y = myClass.Y;
      //... etc.
      return ctb;
  }
  </code></pre>
  
  <ul>
  <li>eType = identify which method to call</li>
  <li>T = Generic return type</li>
  <li>GetSomething(): Knows nothing of the return type, other than it extends class. </li>
  <li>GetMethodA(): Gets a MyClass object, initializes, populates, returns ConcreteTypeA</li>
  </ul>
  
  <p>This is based off of a Factory pattern vs. the alternative of calling the individual methods (making them public) so common preparation can be done before the switch case.</p>
  
  <p>Visual studio is saying "Cannot implicitly convert type "ConcreteTypeA" to "T" which is leading to covariance and contravariance but I'm not sure if this is a bad version of the factory pattern.</p>
  
  <p>The main idea is that GetSomething() doesn't know/care about the return type other than it's generic (the places it would be used would have to know which type they want in return). The private methods (MethodA, B, ...) have to deal with concrete types as they do custom object creation. ConcreteTypeA and B don't share the same interface or base class.</p>
  
  <p><em>The eType enum is to identify which method to call (see <a href="https://stackoverflow.com/questions/4209791/design-patterns-abstract-factory-vs-factory-method/4211307">Design Patterns: Abstract Factory vs Factory Method</a>) as is generally done in Factories.
  The generic type T would be to return a known type instead of e.g. an object because the concrete return types don't implement an interface or base class.</em> </p>
  
  <p>The main difference with the Simple Factory is that the actual return "type T" is only known by the caller, and the private methods. The objects returned by GetSomething do NOT have a common base/interface or it wouldn't even need to be generic. enum eType is like the ID in <code>public static Position Get(int id)</code> <a href="http://www.dotnetperls.com/factory" rel="nofollow noreferrer">http://www.dotnetperls.com/factory</a> and just identifies what the main factory method should do.</p>
  
  <p>Is this a bad or wrong approach? Should I just use the individual methods directly (which doesn't seem to precisely follow the Factory pattern)?</p>
  
  <p>The usage would be that different classes need to be initialized here. It would be easy to just make the individual methods public and call them directly. The reason to put everything inside a single method call is to allow common processing since they all need an instance of MyClass for their initialization.</p>