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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:36:43+00:00 2026-05-16T07:36:43+00:00

Is it correct that a instance method can be called on a null reference

  • 0

Is it correct that a instance method can be called on a null reference in IL..?
Is there any example to show this..?

  • 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-16T07:36:44+00:00Added an answer on May 16, 2026 at 7:36 am

    Yes, this is possible, as long as the method doesn’t use this because the CLR does not do a null check for call instructions.

    You would have to modify the IL by hand as the C# compiler would almost always generate a callvirt instruction1.

    See this blog post for details and an example:

    Instance Methods Called on null References

    Sample

    .method private hidebysig static void  Main(string[] args) cil managed
    {
        .entrypoint
        // Code size       18 (0x12)
        .maxstack  1
        .locals init ([0] class SomeClass o, [1] string hello)
        IL_0000:  nop
        IL_0001:  ldnull
        IL_0002:  stloc.0
        IL_0003:  ldloc.0
        IL_0004:  call       instance string SomeClass::GetHello()
        IL_0009:  stloc.1
        IL_000a:  ldloc.1
        IL_000b:  call       void [mscorlib]System.Console::WriteLine(string)
        IL_0010:  nop
        IL_0011:  ret
    } 
    

    1In fact the reason that the C# compiler emits callvirt even in cases where a simple call instruction would be sufficient is to prevent calling instance methods on null references. With this behavior of the compiler users will get a NullReferenceException so the weird situation of calling a method on a null pointer is avoided. Eric Gunnerson explained this in a blog post some time ago: Why does C# always use callvirt? Gishu also has a nice explanation in a related question.

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

Sidebar

Related Questions

Am I correct that the only way to prevent a method from being executed
i used this code: List<string> lists=new List<string>(apple,orange,banana,apple,mang0,orange); string names; names=lists.Distinct() is that correct?
I know that there are many different architectures exist. In this question I consider
Hey guys. I have a method that gets called each second which I want
I have a generic class called Repository. This class has a function that calls
Is it correct that one can create spring beans using just the @Component annotation
I'm pretty sure I know the answer to this (that you can't do it),
In Ruby, how can I programmatically determine which class/module defines a method being called?
I have a method called handleLocalNotification in my AppDelegate that fires when my app
Is it correct that the reveal applescript command is broken with iTunes 9? Works:

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.