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 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

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sql server : ISDATE (Transact-SQL) ----Invalid date SELECT ISDATE('30/2/2007') RETURNS… May 16, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer It's a connection timeout - if you're not having a… May 16, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer Tell it to ignore mouse events. May 16, 2026 at 1:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hey guys. I have a method that gets called each second which I want
In PHP, how can a class reference its own name? For example, what would
I know that there are many different architectures exist. In this question I consider
I have the following partial. It can be called from three different times in
I'm making a class that initializes instances of certain classes. This class will be
According to PHP, locale information is maintained per process. Is my understanding correct that
I understand that a ModelBinder is a good place to do work on the
I've got an C# .Net 3.5 executable that runs on a local machine and
I'd really appreciate some advice around testing for errors using OCMock. Method Under Test
Just a simple question: I have read that a class should be made static

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.