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

The Archive Base Latest Questions

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

IL doesn’t always use callvirt instruction for virtual methods in a case like this:

  • 0

IL doesn’t always use callvirt instruction for virtual methods in a case like this:

class MakeMeASandwich{
  public override string ToString(){
    return base.ToString();
  }
}

In this case, it is said that IL will produce call instead of callvirt where callvirt is produced to check whether variable is null or not and throws NullReferenceException otherwise.

  1. Why does a recursive invocation happen till stack overflow if callvirt is used instead of call?
  2. If call is used, then when does it check whether the instance variable it uses to call the methods is null or not?
  • 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:06:09+00:00Added an answer on June 2, 2026 at 1:06 pm

    Why does a recursive invocation happen till stack overflow if callvirt is used instead of call?

    Because then your code is exactly the same as:

    override string ToString()
    {
        return this.ToString();
    }
    

    Which clearly is an infinite recursion, provided that the method given is the most-overriding version of ToString.

    If call is used, then how come it checks whether the instance variable it uses to call the methods is null or not?

    The question is not answerable because the question assumes a falsehood. The call instruction does not check to see if the reference to the receiver is null or not, so asking why the call instruction checks for null doesn’t make any sense.

    Let me rephrase that for you into some better questions:

    Under what circumstances does the C# compiler generate a call vs a callvirt?

    If the C# code is doing a non virtual call on a virtual method then the compiler must generate a call, not a callvirt. The only time this happens really is when using base to call a virtual method.

    If the C# code is doing a virtual call then the compiler must generate a callvirt.

    If the C# code is doing a non virtual call on a non virtual method then the compiler can choose to generate either call or callvirt. Either will work. The C# compiler typically chooses to generate a callvirt.

    The call instruction does not automatically do a null check, but callvirt does. If the C# compiler chooses to generate a call instead of a callvirt, is it also obligated to generate a null check?

    No. The C# compiler can skip the null check if the receiver is already known to not be null. For example, if you said (new C()).M() for a non-virtual method M then it would be legal for the compiler to generate a call instruction without a null check. We know that (1) the method is not virtual, so it does not have to be a callvirt; we can choose whether to use callvirt or not. And we know (2) that new C() is never null, so we do not have to generate a null check.

    If the C# compiler does not know that the receiver is not null, then it will either generate a callvirt, or it will generate a null check followed by a call.

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

Sidebar

Related Questions

Why doesn't this work? HTML <div class=gallery> <div class=viewport> <div class=wrapper> <img src=img/boat1.png />
Doesn't object initialization outside of a constructor break encapsulation ? Given: class MyClass {
This doesn't have to be Java, but it's what I'm dealing with. Also, not
IE7 doesn't support :last-child pseudo selector. I am thinking of explicitly adding a class
Ruby doesn't seem to have a facility for defining a protected/private block like so:
there doesn't seem to be an existing util class for converting a storage unit
For some reason, after submitting a string like this Jack’s Spindle from a text
Doesn't matter what I do, I simply can't get this to play a sound
Why doesn't this work: <Style TargetType=s:Substance> <Setter Property=Template> <Setter.Value> <DataTemplate> <StackPanel> <TextBlock Text={TemplateBinding Name}/>
This doesn't work: $to = 'myemail@gmail.com'; $from = 'test@test.com'; $subj = 'test'; $message =

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.