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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:11:21+00:00 2026-06-02T13:11:21+00:00

Lets say we have following sample code in C#: class BaseClass { public virtual

  • 0

Lets say we have following sample code in C#:

class BaseClass
  {
    public virtual void HelloWorld()
    {
      Console.WriteLine("Hello Tarik");
    }
  }

  class DerivedClass : BaseClass
  {
    public override void HelloWorld()
    {
      base.HelloWorld();
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      DerivedClass derived = new DerivedClass();
      derived.HelloWorld();
    }
  }

When I ildasmed the following code:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] class EnumReflection.DerivedClass derived)
  IL_0000:  nop
  IL_0001:  newobj     instance void EnumReflection.DerivedClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance void EnumReflection.BaseClass::HelloWorld()
  IL_000d:  nop
  IL_000e:  ret
} // end of method Program::Main

However, csc.exe converted derived.HelloWorld(); –> callvirt instance void EnumReflection.BaseClass::HelloWorld(). Why is that? I didn’t mention BaseClass anywhere in the Main method.

And also if it is calling BaseClass::HelloWorld() then I would expect call instead of callvirt since it looks direct calling to BaseClass::HelloWorld() method.

  • 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-02T13:11:24+00:00Added an answer on June 2, 2026 at 1:11 pm

    The call goes to BaseClass::HelloWorld because BaseClass is the class that defines the method. The way virtual dispatch works in C# is that the method is called on the base class, and the virtual dispatch system is responsible for ensuring that the most-derived override of the method gets called.

    This answer of Eric Lippert’s is very informative: https://stackoverflow.com/a/5308369/385844

    As is his blog series on the topic: http://blogs.msdn.com/b/ericlippert/archive/tags/virtual+dispatch/

    Do you have any idea why this is implemented this way? What would happen if it was calling derived class ToString method directly? This way didnt much sense this to me at first glance…

    It’s implemented this way because the compiler does not track the runtime type of objects, just the compile-time type of their references. With the code you posted, it’s easy to see that the call will go to the DerivedClass implementation of the method. But suppose the derived variable was initialized like this:

    Derived derived = GetDerived();
    

    It’s possible that GetDerived() returns an instance of StillMoreDerived. If StillMoreDerived (or any class between Derived and StillMoreDerived in the inheritance chain) overrides the method, then it would be incorrect to call the Derived implementation of the method.

    To find all possible values a variable could hold through static analysis is to solve the halting problem. With a .NET assembly, the problem is even worse, because an assembly might not be a complete program. So, the number of cases where the compiler could reasonably prove that derived doesn’t hold a reference to a more-derived object (or a null reference) would be small.

    How much would it cost to add this logic so it can issue a call rather than callvirt instruction? No doubt, the cost would be far higher than the small benefit derived.

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

Sidebar

Related Questions

Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have the following: public class MyObject { [Bindable] public var foo:int
Lets say i have the following c# classes: abstract class a { protected abstract
Lets say I have the following DataMapper resources: class Post include DataMapper::Resource has n,
Let's say I have the following code: typedef std::function<void ()> func_type; void some_func() {
Lets say I have the following array in JavaScript: var skins = new Array('Light',
Lets say I have the following pages: # Include.asp <% Response.Write IsIncluded() & <br>
Lets say i have the following unordered list <ul> <li><a>Hank</a></li> <li><a>Alice</a></li> <li><a>Tom</a></li> <li><a>Ashlee</a></li> </ul>
lets say i have the following RGB values: R:129 G:98 B:87 Photoshop says the
I have the following mysql table called test. lets say it has the data

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.