title: How to return a generic object from a method in C#
body: <p>I have a similar problem to <a href="https://stackoverflow.com/questions/4712567/c-generic-method-return-values">this</a> question only I am not learning generics so want a practical solution and my method is returning more than an <code>int</code> or a <code>double</code>.</p>
  
  <p>I have a function that may return an object (from a known set of classes which are unrelated to each other) based on the user input.</p>
  
  <p>For example if I have following two classes</p>
  
  <pre><code>public class A
  {
  }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>public class B
  {
  }
  </code></pre>
  
  <p>And my method is:</p>
  
  <pre><code>public Object getObject(int objType)
  {
      //return appropriate object based on objType (1==A, 2==B);
  }
  </code></pre>
  
  <p>I can use statements like <code>if (getObject(int objType) is A)</code> in my code but that will require a lot of <code>if</code> statements which is not a good solution (IMHO).</p>
  
  <p>So I am looking for a better design solution to solve this problem. Any suggestions please?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I will need to use the returned object for some time and might also need to pass it to some other method or return a reference of it. So expecting some moving around of that object. Please keep that in mind too while suggesting some approach to solve this problem.</p>