Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 778669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:48:07+00:00 2026-05-14T19:48:07+00:00

I was going through C# Brainteasers ( http://www.yoda.arachsys.com/csharp/teasers.html ) and came across one question:

  • 0

I was going through C# Brainteasers (http://www.yoda.arachsys.com/csharp/teasers.html) and came across one question: what should be the output of this code?

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;
        d.Foo(i);  // it prints ("Derived.Foo(object)"
    }
} 

But if I change the code to

class Derived 
{
    public void Foo(int x)
    {
        Console.WriteLine("Derived.Foo(int)");
    }

    public void Foo(object o)
    {
        Console.WriteLine("Derived.Foo(object)");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        int i = 10;
        d.Foo(i); // prints  Derived.Foo(int)");

        Console.ReadKey();
    }
}

I want to why the output is getting changed when we are inheriting vs not inheriting; why is method overloading behaving differently in these two cases?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:48:08+00:00Added an answer on May 14, 2026 at 7:48 pm

    As I specified in the answers page:

    Derived.Foo(object) is printed – when choosing an overload, if there are any compatible
    methods declared in a derived class, all signatures declared in the base class are ignored
    – even if they’re overridden in the same derived class!

    In other words, the compiler looks at methods which are freshly-declared in the most derived class (based on the compile-time type of the expression) and sees if any are applicable. If they are, it uses the “best” one available. If none is applicable, it tries the base class, and so on. An overridden method doesn’t count as being declared in the derived class.

    See sections 7.4.3 and 7.5.5.1 of the C# 3 spec for more details.

    Now as for exactly why it’s specified like that – I don’t know. It makes sense to me that methods declared in the derived class take precedence over those declared in the base class, as otherwise you run into the “brittle base class” problem – adding a method in the base class could change the meaning of code using the derived class. However, if the derived class is overriding the method declared in the base class, it’s clearly aware of it, so that element of brittleness doesn’t apply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After going through the build process (found at: http://source.android.com/source/building.html ) for the master branch
After going through the article at http://geekexplains.blogspot.com/2008/06/diff-between-externalizable-and.html , i got to know Externalizable is
Going through some documentation on modifying CGImageRef data, I came across a strange example
While going through Effective C++ (by Scott Meyers), I came across the following code
While going through a Zend tutorial , I came across the following statement: Note
im going through the setup on http://www.railstutorial.org/chapters/static-pages#fig:autotest_green and im stuck on this particular error:
While going through the exercises, I came across something that, even after research, I
After going through the discussion here I have one more question now. Lets assume,
Going through the tutorial here: http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs I've noticed that they are passing the EF
When going through the Requirements of a JAX_WS Endpoint, I came across this, JAX-WS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.