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

  • Home
  • SEARCH
  • 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 8215663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:51:35+00:00 2026-06-07T11:51:35+00:00

I got several questions about calling overloaded(or maybe I should call them hidden) methods

  • 0

I got several questions about calling overloaded(or maybe I should call them hidden) methods in C#. Suppose I have classes as following:

class ParaA {}
class ParaB : ParaA {}
class ParaC : ParaB {}

class TheBaseClass 
{
   public void DoJob (ParaA a){Console.WriteLine ("DoJob in TheBaseClass is being invoked");}

}

class TheDerivedClass : TheBaseClass 
{
  public void DoJob (ParaB b){Console.WriteLine ("DoJob in TheDerivedClass is being invoked");}
}

class Test
{
  //Case 1: which version of DoJob() is being called?
  TheDerivedClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaA ());

  //Case 2: which version of DoJob() is being called?
  TheBaseClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaA ());

  //Case 3: which version of DoJob() is being called?
  TheBaseClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaB ());

  //Case 4: which version of DoJob() is being called?
  TheBaseClass aInstance= new TheDerivedClass ();
  aInstance.DoJob(new ParaC ());

}

I hope that I have made myself clear about what I am trying to do. I want to know how will C# search for the ‘matched’ version of method to invoke, when the parameters provided by the invoker do not perfectly match any signature but are compatible with some signatures. It makes me even more confused when methods are not just overloaded within a class, but also hidden, or overrided, or overloaded by derived classes. The example above does not cover every possible scenario. Is there any term for this?

Thank you guys in advance!

Matthew

  • 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-06-07T11:51:37+00:00Added an answer on June 7, 2026 at 11:51 am

    I’ve added a few more lines to make the code compile:

    void Main()
    {
        var t = new Test();
        t.Run();
    }
    class ParaA {}
    class ParaB : ParaA {}
    class ParaC : ParaB {}
    
    class TheBaseClass 
    {
       public void DoJob (ParaA a){Console.WriteLine ("DoJob in TheBaseClass is being invoked");}
    
    }
    
    class TheDerivedClass : TheBaseClass 
    {
      public  void DoJob (ParaB b){Console.WriteLine ("DoJob in TheDerivedClass is being invoked");}
    }
    
    public class Test
    {
        public void Run()
        {
            //Case 1: which version of DoJob() is being called?
            TheDerivedClass aInstance= new TheDerivedClass ();
            aInstance.DoJob(new ParaA ());
    
            //Case 2: which version of DoJob() is being called?
            TheBaseClass aInstance2= new TheDerivedClass ();
            aInstance2.DoJob(new ParaA ());
    
            //Case 3: which version of DoJob() is being called?
            TheBaseClass aInstance3= new TheDerivedClass ();
            aInstance3.DoJob(new ParaB ());
    
            //Case 4: which version of DoJob() is being called?
            TheBaseClass aInstance4= new TheDerivedClass ();
            aInstance4.DoJob(new ParaC ());
        }
    }
    

    The produces the output:

    DoJob in TheBaseClass is being invoked
    DoJob in TheBaseClass is being invoked
    DoJob in TheBaseClass is being invoked
    DoJob in TheBaseClass is being invoked
    

    i.e. the base class method is called every time.

    In Case 1, it is called because the argument is ParaA, and ParaA is not a ParaB.
    In the other cases, it is called because the object instance is of type ‘TheBaseClass’.

    Here’s the same code modified to illustrate method overloading:

    void Main()
    {
        var t = new Test();
        t.Run();
    }
    
    class ParaA {}
    class ParaB : ParaA {}
    class ParaC : ParaB {}
    
    class TheBaseClass 
    {
       public virtual void DoJob (ParaA a){Console.WriteLine ("DoJob in TheBaseClass is being invoked");}
    
    }
    
    class TheDerivedClass : TheBaseClass 
    {
      public override void DoJob (ParaA b){Console.WriteLine ("DoJob in TheDerivedClass is being invoked");}
    }
    
    public class Test
    {
        public void Run()
        {
            //Case 1: which version of DoJob() is being called?
            TheDerivedClass aInstance= new TheDerivedClass ();
            aInstance.DoJob(new ParaA ());
    
            //Case 2: which version of DoJob() is being called?
            TheBaseClass aInstance2= new TheDerivedClass ();
            aInstance2.DoJob(new ParaA ());
    
            //Case 3: which version of DoJob() is being called?
            TheBaseClass aInstance3= new TheDerivedClass ();
            aInstance3.DoJob(new ParaB ());
    
            //Case 4: which version of DoJob() is being called?
            TheBaseClass aInstance4= new TheDerivedClass ();
            aInstance4.DoJob(new ParaC ());
        }
    }
    

    The output is now:

    DoJob in TheDerivedClass is being invoked
    DoJob in TheDerivedClass is being invoked
    DoJob in TheDerivedClass is being invoked
    DoJob in TheDerivedClass is being invoked
    

    TheDerivedClass method is invoked everytime, because the object is of type ‘TheDerivedClass’.

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

Sidebar

Related Questions

Several questions about functional programming languages have got me thinking about whether XSLT is
I've seen several questions about password protecting pages, but none of them seem to
I have several little questions about best practices with JPA (via hibernate). My first
I've seen several questions on SO about this, but none of them quite fit
After reading several questions/answers about unit testing Entity Framework, I have decided to forgo
This is part of a homework assignment. I've got several questions asking find the
I am working on a project with several custom classes. I have a CardModel
This is a Two (2) Part Question about Generics I've got to create several
I've got a pretty strong background in C-style languages. And have worked on several
My homepage is about 25K by itself but I've got several PNG images on

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.