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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:52:05+00:00 2026-06-15T02:52:05+00:00

I was surprised when I ran into this situation and realized I wasn’t sure

  • 0

I was surprised when I ran into this situation and realized I wasn’t sure what the best solution was.

Say I have the following three types:

class A { }
class B : A { }
class C : A { }

And the following three methods:

DoSomething(A a){ }
DoSomething(B b){ }
DoSomething(C c){ }

I have a List<A> which contains objects of type B and C

I would like to do this:

foreach(A a in list) { DoSomething(a) }

and have it call the method which matches most closely to the underlying type,
but of course this will always call DoSomething(A a)

I’d prefer not to have a bunch of type checking to get the right method call, and I don’t want to add anything to the classes A, B or , C.

Is it possible?

  • 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-15T02:52:06+00:00Added an answer on June 15, 2026 at 2:52 am

    This is a rather well-known issue with virtual dispatch in statically typed languages: it only handles one parameter (this) “virtually”; for all other parameters, the method call is bound using the static type of the argument. Since your list is a list of A, the code is only ever going to call the A overload.

    You would need multiple dispatch to achieve the stated goal, and since the language does not provide this out of the box unless you switch to dynamic, so you will have to either make the switch or implement it yourself. There are many tradeoffs to consider when making this decision (and also when deciding how to implement multiple dispatch if needed), so don’t do this lightly.

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

Sidebar

Related Questions

This is something that I never ran into before. Say I have a class
Surprised that i havent been able to find this myself, but anyway. Let's say
I ran across an assertion that HashSet<T>.Contains() is an O(1) operation. This surprised me
Im using the above technologies and have ran into what I presume is a
So I ran into a situation today where some production code was failing precisely
Just ran into this recently and I thought it'd be helpful to share. The
I was surprised to see that the following Java code snippet compiled and ran:
Background: I ran into a problem that I find somewhat unexpected. I have a
I was surprised, if I noticed it right, that $this->Js->object() autosorts the array i
I'm surprised that MS has implemented a new feature: Validation with INotifyDataErrorInfo I have

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.