title: up-casting in C# and call a specific method based on the derived type
body: <p>I have a couple of classes, all derived from the same base type.</p>
  
  <pre><code>class basetype{}
  class TypeA : basetype{}
  class TypeB : basetype{}
  ...
  </code></pre>
  
  <p>A number of them is stored in a list.</p>
  
  <pre><code>List&lt;basetype&gt; myObjects
  </code></pre>
  
  <p>As always, each of these types has to be handled differently. Now I have a couple of methods to handle them, and one method that takes the basetype as parameter.</p>
  
  <pre><code>HandleTypes(TypeA obj){}
  HandleTypes(TypeB obj){}
  HandleTypes(basetype obj)
  </code></pre>
  
  <p>currently, my HandleAllTypes looks like that:</p>
  
  <pre><code>string name = obj.GetType().Name
  switch(name)
  {
    case "TypeA":
      return HandleTypes(obj as TypeA);
    case "TypeB":
      return HandleTypes(obj as TypeB);
    ....
  }
  </code></pre>
  
  <p>now this is crap. Is there a way like</p>
  
  <pre><code>HandleTypes(obj ?"as derived type"?)
  </code></pre>
  
  <p>Searched through MSDN and other sources, found nothing.</p>