CSharp - Overloading and Resolution

Introduction

Inheritance has an impact on method overloading.

Consider the following two overloads:

abstract class Shape{
       // Note empty implementation
       public abstract decimal NetValue { get; }
}
class Circle : Shape
{
}

class Square : Shape
{
}

void Test (Shape a) { }
void Test (Square h) { }

When an overload is called, the most specific type has precedence:

Square h = new Square (...);
Test(h);                      // Calls Test(Square)

The overloaded method to call is determined at compile time rather than at runtime.

The following code calls Test(Shape), even though the runtime type of a is Square:

Shape a = new Square (...);
Test(a);                      // Calls Test(Shape)