title: Returning an unknown object
body: <p>As an example, I have a base class called baseAbstractClass and a couple of derived classes:</p>
  
  <pre><code>public class Derived : baseAbstractClass
  {
      public Derived()
      {
      }
      // abstract methods go here
  }
  public class DerivedEx : baseAbstractClass
  {
     // code goes here
  }
  </code></pre>
  
  <p>and I have this class:</p>
  
  <pre><code>public class SomeClass
  {
       public SomeClass()
       {
       }
       object ReturnMeAnObject(int whichObject)
       {
            object toReturn = null;
  
            if (whichObject == 0)
            {
                toReturn = new Derived();
            }
            else
            if (whichObject == 1)
            {
                 toReturn = new DerivedEx();
            }
            return toReturn;
       }
   }
  </code></pre>
  
  <p>In my Main function:</p>
  
  <pre><code>public class Start
  {
       static void Main()
       {
            SomeClass someClass = new SomeClass()
            Derived derived = (Derived)someClass.ReturnMeAnObject(0);
       }
  }
  </code></pre>
  
  <p>The object that I'm returning is unknown until runtime, but in reality I don't know the type of object is going to be returned - It could be Derived, or DerivedEx.
  Is there a simplier way than having to cast my return value as shown in the Main function as this can result in a very large switch statement or, a very large set of If... Else conditions.</p>
  
  <p>I have tried to determine if my design is correct by constructing a class diagram, but can't seem to fit it into code.</p>
  
  <p>Any help would be greatly appreciated.</p>