TechQuiz Question 7: Calling derived methods..

What is the result of the following code?

using System;
class Base
    public virtual void Foo(int x) { Console.WriteLine(“Base.Foo(int)”); }
class Derived : Base
    public override void Foo(int x)  { Console.WriteLine(“Derived.Foo(int)”); }
    public void Foo(object o)  { Console.WriteLine(“Derived.Foo(object)”); }
class Test
    static void Main()
        Derived d = new Derived();
        int i = 10;

The result is: Derived.Foo(object) 

When the compiler chooses the overload, all functions that are declared in the derived class are chosen first, even when there is a more precise match in the base class.

When designing an inheritance hierarchy it’s wise to make sure you are not declaring more general methods in derived classes that would hide base functionality.


Wouter de Kort works as a lead architect and consultant. He helps organizations stay on the cutting edge of software development. Wouter focuses on DevOps. He loves solving complex problems and helping other developers to grow. Wouter authored the book DevOps on the Microsoft stack and a couple of other books. Wouter is a Microsoft MVP and an ALM Ranger. You can find him on Twitter (@wouterdekort), on his blog at and at the various conferences where Wouter speaks.


Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.