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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:39:28+00:00 2026-05-28T20:39:28+00:00

In the following code, I would have expected calling a.Generate(v) would have resulted in

  • 0

In the following code, I would have expected calling a.Generate(v) would have resulted in calling V.Visit(A a), since when Generate is called this is of type A. Hoewever, it appears that this is seen as being an Inter instead.

Is it possible to have the intended behaviour without explicitly implementing the (identical) method in both A and B and only on the shared base class? If so, how can it be acheived?

using System;
using System.Diagnostics;

namespace Test {
    class Base {}
    class Inter: Base {
        public virtual void Generate(V v) {
            // `Visit(Base b)` and not `Visit(A a)` is called when calling
            // A.Generate(v). Why?
            v.Visit(this);
        }
    }

    class A: Inter {}
    class B: Inter {}

    class V {
        public void Visit(Base b) { throw new NotSupportedException(); }
        public void Visit(A a)    { Trace.WriteLine("a"); }
        public void Visit(B b)    { Trace.WriteLine("b"); }
    }

    class Program {
        static void Main() {
            V v = new V();
            A a = new A();
            B b = new B();

            a.Generate(v);
            b.Generate(v);
        }
    }
}

Edit
It was suggested in the answers that the code above is not polymorphic. I would object to that. V.Visit is polymorphic.

  • 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-28T20:39:29+00:00Added an answer on May 28, 2026 at 8:39 pm

    You are expecting the call to v.Visit(this) to determine which overload of Visit to call based on the run time types of both v and this.

    Languages which have that feature are called “double virtual dispatch” languages. (Or, if more than two things are considered they are called “multiple virtual dispatch” languages, or “multimethod” languages.)

    C# is not a double-virtual dispatch language; it is a single-virtual dispatch language when dispatch decisions are made at compile time. That is, the decision of which overload to choose is made on the basis of the runtime type of the receiver, but the compile-time types of the arguments.

    Now, in your case, C# does not use single virtual dispatch because the call to Visit is not even a virtual call in the first place! The fact that the call to Generate was a virtual call is completely irrelevant, and the fact that Visit is overloaded is also irrelevant. The dispatch to Visit is made non-virtually, so the dispatch logic is based entirely on the compile-time type of the receiver, v, and the argument this. Since the receiver is known to be of type V and the argument is known to be of type Inter, overload resolution must choose the best possible match given only that information. It cannot choose the versions of Visit that take A or B because those are more derived than the known argument type, Inter. It must choose the overload with the less derived formal parameter type, Base.

    If you wish to achieve double-virtual dispatch in C#, there are two standard ways to do it. First, you can use dynamic; with dynamic, the analysis is performed at runtime using the runtime types. Simply cast the receiver and the arguments to dynamic and the compiler will take care of it for you. That imposes a significant performance cost though.

    The second standard way to do it is to use the Visitor Pattern, which you can find out about by searching the internet for it. I suspect based on the names of your methods that you are trying to implement the Visitor Pattern already; this is not the right way to do it.

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

Sidebar

Related Questions

Have a look here: In the following code, what would be the type of
Consider the following code: DateTime? ndate = null; Console.WriteLine(ndate.HasValue); I would have expected a
I have the following code: for attribute in site.device_attributes device.attribute end where I would
I have the following code, which works as I expect. What I would like
I have the following html code: <h3 id=headerid><span onclick=expandCollapse('headerid')>&uArr;</span>Header title</h3> I would like to
I have the following snippet where I would like to extract code between the
I have code, similar to the following, that I would like to modify: Sub
I have the following code: public class MyElement { String name; String type; MyElement(String
I have the following form code: # forms.py class SomeForm(forms.Form): hello = forms.CharField(max_length=40) world
I'm curious if following code would be considered safe? using (SqlConnection cn = new

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.