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 8607495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:22:13+00:00 2026-06-12T03:22:13+00:00

I have two Data classes: BaseDataClass and DerivedDataClass which derives from first one. I

  • 0

I have two Data classes: BaseDataClass and DerivedDataClass which derives from first one.
I also have two Consuming classes: ConsumingBaseClass and ConsumingDerivedClass which derives from first one.
In ConsumingBaseClass I have virtual method DoWork, which accepts DerivedDataClass and do some work.

In ConsumingDerivedClass I have override for method DoWork and also I have overload for DoWork which accepts BaseDataClass.
When I try to call DoWork, passing instance of DerivedDataClass, DoWork(BaseDataClass) is called instead of DoWork(DerivedDataClass).

Have anybody idea, why wrong method is called?

Following code demostrates the problem:

class Program
{
    private static void Main(string[] args)
    {
        ConsumingDerivedClass x = new ConsumingDerivedClass();
        // Wrong DoWork is called - expected calling of DoWork(DerivedDataClass) but actually called DoWork(BaseDataClass)
        x.DoWork(new DerivedDataClass());
        Console.ReadKey();
    }
}

public class ConsumingBaseClass
{
    public virtual void DoWork(DerivedDataClass instance)
    {
        Console.WriteLine("ConsumingBaseClass.DoWork(DerivedDataClass); Type of argument is '{0}'", instance.GetType());
    }
}

public class ConsumingDerivedClass : ConsumingBaseClass
{
    public override void DoWork(DerivedDataClass instance)
    {
        Console.WriteLine("ConsumingDerivedClass.DoWork(DerivedDataClass); Type of argument is '{0}'", instance.GetType());
        base.DoWork(instance);
        // Some additional logic
    }

    public void DoWork(BaseDataClass instance)
    {
        Console.WriteLine("ConsumingDerivedClass.DoWork(BaseDataClass); Type of argument is '{0}'", instance.GetType());
        DerivedDataClass derivedInstance = new DerivedDataClass();
        // Some logic based on what is in baseInstacne
        derivedInstance.SomeProperty = "Value, got from some logic";

        base.DoWork(derivedInstance);
        // Some additional logic
    }
}

public class BaseDataClass
{ }

public class DerivedDataClass : BaseDataClass
{ 
    public string SomeProperty { get; set; } 
}
  • 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-12T03:22:14+00:00Added an answer on June 12, 2026 at 3:22 am

    Have anybody idea, why wrong method is called?

    The right method is being called according to the language specification. It’s your expectations which are wrong.

    In ConsumingDerivedClass I have override for method DoWork and also I have overload for DoWork which accepts BaseDataClass.

    That’s the problem, basically. Overload resolution doesn’t work the way you think it does. The compiler works from the compile-time type of the target of the call, up through base classes towards object. Only methods newly declared (not even overridden) are considered.

    You can change which overload would be used by writing:

    ((ConsumingBaseClass) x).DoWork(new DerivedDataClass());
    

    Ideally though, if you’ve got something this brittle, it would be worth giving the methods different names. Overloads should have the same effect, just providing information in different ways.

    Basically, have a look at the first question on my brainteasers page, and the details of the first answer. I also have an article on overload resolution which you may find useful.

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

Sidebar

Related Questions

i have two data classes which hold only data members(no functions). One is CallTask
i have two data tables with the same structure the first one has one
I have two classes, one which inherits the other, for example:- public class A
I have two separate web services, the first configured to extract data from a
Imagine I have two classes, class1 and class2. Class1 inherits from baseClass, which looks
I have two Data Centers, each one with replication factor 3. Will write with
I have two data sources: a legacy one (web service) and a database one.
I have two data frames with two columns each. The first column is timestamps
I am trying to have two data series plotted in one graph as boxes
I am working on a simple Core Data app. I have two classes: Client

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.