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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:16:15+00:00 2026-06-04T03:16:15+00:00

This very simple example confuses me: public class X { public void XMethod(A a)

  • 0

This very simple example confuses me:

public class X {
    public void XMethod(A a) {
        Console.WriteLine(a.GetType());
        Console.WriteLine("Got A");
    }

    public void XMethod(B b) {
        Console.WriteLine(b.GetType());
        Console.WriteLine("Got B");
    }
}

public abstract class A {
    public virtual void M(X x) {
        Console.WriteLine(this.GetType());
        x.XMethod(this);
    }
}

public class B : A {

}

class Program {
    static void Main(string[] args) {
        X x = new X();
        B b = new B();
        b.M(x);
    }
}

Output of this is

B
B
Got A

Everything up until ‘Got A’ is fine. I would expect that method X.XMethod(B) would be called when I invoke method M on instance of class B.

What is going on here? Why is XMethod(A) called, and not XMethod(B), when it is clear that type of provided argument is B and not A?

PS: I got same output in java for equivalent implementation.

  • 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-04T03:16:16+00:00Added an answer on June 4, 2026 at 3:16 am

    There is only on A.M method. Not one for A and one for B.

    The IL is the same, in A.M, for all instances; at compile-time, A.M only knows this to be A (or object), hence it resolves always to XMethod(A). This method resolution is in the IL generated at compile-time, and doesn’t change for subclasses (indeed, the subclass could be in a separate assembly that the compiler doesn’t even know about):

    .method public hidebysig newslot virtual instance void M(class X x) cil managed
    {
        .maxstack 8
        L_0000: ldarg.0 
        L_0001: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
        L_0006: call void [mscorlib]System.Console::WriteLine(object)
        L_000b: ldarg.1 
        L_000c: ldarg.0 
        L_000d: callvirt instance void X::XMethod(class A)
        L_0012: ret 
    }
    

    To get the behaviour you want, you could use dynamic. Not saying you should, though.

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

Sidebar

Related Questions

I have this very simple C++ class: class Tree { public: Node *head; };
This is a very simple example where accessing directly could be dangerous class Something{
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have this very simple example that I am using to learn structs in
In this very simple code example, messages get lost every once in a while.
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I have something very similar to this example: https://developers.google.com/maps/documentation/javascript/examples/directions-simple All I am trying to
Ok, I'm very simply trying to take this example... http://jsfiddle.net/shanabus/HGF59/ and put it on
I was making this very simple lex program (just an introductory program). But on
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =

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.