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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:14:21+00:00 2026-06-17T18:14:21+00:00

The C# 4.0 specs read: When a virtual method is invoked, the runtime type

  • 0

The C# 4.0 specs read:

When a virtual method is invoked, the runtime type of the instance for
which that invocation takes place determines the actual method
implementation to invoke. In a nonvirtual method invocation, the
compile-time type of the instance is the determining factor.

At first, I thought this had something to do with initialization. For example, given two initializations:

BaseClass bcDerived = new Derived(); vs BaseClass bcBase = new BaseClass();

and an overload in a helper class:

public virtual void Method(Derived d)
{
     Console.WriteLine("Result = derived called");
}

public virtual void Method(BaseClass d)
{
     Console.WriteLine("Result = base called");
}

Method invokation is not impacted by the virtual keyword in this case. Regardless of having marked virtual, the least derived overload is called. Only during override in the Derived class does the method invocation change.

So, what do “runtime type” and “compile-time type” mean? How do they impact method invocation?

  • 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-17T18:14:22+00:00Added an answer on June 17, 2026 at 6:14 pm

    This is more a matter of virtual vs. non-virtual methods, and how the invocation occurs. The portion of the spec you are quoting deals with method calls on a variable – calling bcDerived.SomeMethod(), not calling foo.SomeMethod(bcDerived).

    The specification you are quoting refers to the case where you have non-virtual methods:

    public class A
    {
        public void Foo() { Console.WriteLine("A.Foo"); }
        public virtual void Bar() { Console.WriteLine("A.Bar"); }
    }
    public class B : A
    {
        public new void Foo() { Console.WriteLine("B.Foo"); }
        public override void Bar() { Console.WriteLine("B.Bar"); }
    }
    

    Then the method called will be determined by the compiler, at compile time, so doing:

    A someInst = new B();
    someInst.Foo();
    

    Will cause this to call A.Foo(), no matter what subclass of A is being referred to by someInst, since this is a non-virtual method.

    If you have a virtual method, however, the callvirt instruction is specified by the compiler, which moves the decision to runtime. This means that:

     someInst.Bar();
    

    Will call B.Bar(), not A.Bar().

    In your case, you’re not calling a virtual method (in the sense that the spec is referring to), but doing standard method resolution. 7.5.3 of the C# Spec deals with Overload resolution in detail. In your case, the argument list (bcDerived) is inspected by the compiler, and seen to be defined as type BaseClass. The “best match” for this is going to be public virtual void Method(BaseClass d) as the the parameter list directly matches the argument list, so that is used at compile time.

    Method overload resolution, if you look at the specification, doesn’t directly take virtual method calls into effect – it only looks at implicit conversions between types.

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

Sidebar

Related Questions

I read the specs on http://api.jquery.com/jQuery.ajax/ and found that depending on the dataType, scripts
I read from http://www.apple.com/iphone/specs.html that IPhone4's screen is 960-by-640-pixel resolution at 326 ppi. But
I am working on some specs for my Rails app that involve re-ordering Models
A git tool that meets the specs below is needed. Does one already exist?
I've had a good read of the PHP specs on overloading , and most
We know from the C# specification that reference read/writes are atomic. In a statement
I've read: http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#the-accesskey-attribute and it doesn't say anywhere if or how the 'Home' key
I started reading mp3-files in c++. All went well until I read the specs
I am looking at http://msdn.microsoft.com/en-us/library/aa691278(VS.71).aspx , which said read and write of int is
I'm trying to understand how WSGI works. I know I could read the specs,

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.