title: Is it possible to get c# to use method overload of most specific type rather than base type?
body: <p>If you have a method which is overloaded with a derived type, the method called at run-time depends on the type of your variable, even if the underlying object is actually of the derived type:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          BaseClass theBaseObject = new BaseClass
          {
              Foo = "FOO"
          };
          DerivedClass theDerivedObject = new DerivedClass
          {
              Foo = "FOO",
              Bar = "BAR"
          };
  
          Processor processor = new Processor();
  
          Console.WriteLine(processor.Compose(theBaseObject));
          Console.WriteLine(processor.Compose(theDerivedObject));
          Console.WriteLine(processor.Compose((BaseClass) theDerivedObject));
      }
  }
  
  public class Processor
  {
      public string Compose(BaseClass item)
      {
          return item.Foo;
      }
  
      public string Compose(DerivedClass item)
      {
          return Compose((BaseClass)item) + item.Bar;
      }
  }
  
  public class BaseClass
  {
      public string Foo { get; set; }
  }
  
  public class DerivedClass : BaseClass
  {
      public string Bar { get; set; }
  }
  </code></pre>
  
  <p>Actual Output:</p>
  
  <pre><code>FOO
  FOOBAR
  FOO
  </code></pre>
  
  <p>I would like to find a way to alter this behaviour such that the most specific method invoked for a given parameter.</p>
  
  <p>Desired output:</p>
  
  <pre><code>FOO
  FOOBAR
  FOOBAR  // because theDerivedObject is an instance of DerivedClass 
  </code></pre>
  
  <p>This would allow specific processing for a bunch of items, all derived from a base.</p>
  
  <p>Is this possible in c#?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>A clarification - in practice there would be no explicit cast, as the items would likely be in a list of collection of mixed types:</p>
  
  <p>Example using a list without an explicit cast:</p>
  
  <pre><code>    foreach (BaseClass item in new []{ theBaseObject, theDerivedObject })
      {
          Console.WriteLine(processor.Compose(item));
      }
  </code></pre>
  
  <p>Actual output: </p>
  
  <pre><code>FOO
  FOO
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>FOO
  FOOBAR
  </code></pre>
  
  <p>Obviously the cast is still happening - but it's not easy to remove it.</p>