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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:59:07+00:00 2026-05-15T13:59:07+00:00

Occasionally I like to spend some time looking at the .NET code just to

  • 0

Occasionally I like to spend some time looking at the .NET code just to see how things are implemented behind the scenes. I stumbled upon this gem while looking at the String.Equals method via Reflector.

C#

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public override bool Equals(object obj)
{
    string strB = obj as string;
    if ((strB == null) && (this != null))
    {
        return false;
    }
    return EqualsHelper(this, strB);
}

IL

.method public hidebysig virtual instance bool Equals(object obj) cil managed
{
    .custom instance void System.Runtime.ConstrainedExecution.ReliabilityContractAttribute::.ctor(valuetype System.Runtime.ConstrainedExecution.Consistency, valuetype System.Runtime.ConstrainedExecution.Cer) = { int32(3) int32(1) }
    .maxstack 2
    .locals init (
        [0] string str)
    L_0000: ldarg.1 
    L_0001: isinst string
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: brtrue.s L_000f
    L_000a: ldarg.0 
    L_000b: brfalse.s L_000f
    L_000d: ldc.i4.0 
    L_000e: ret 
    L_000f: ldarg.0 
    L_0010: ldloc.0 
    L_0011: call bool System.String::EqualsHelper(string, string)
    L_0016: ret 
}

What is the reasoning for checking this against null? I have to assume there is purpose otherwise this probably would have been caught and removed by now.

  • 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-15T13:59:07+00:00Added an answer on May 15, 2026 at 1:59 pm

    I assume you were looking at the .NET 3.5 implementation? I believe the .NET 4 implementation is slightly different.

    However, I have a sneaking suspicion that this is because it’s possible to call even virtual instance methods non-virtually on a null reference. Possible in IL, that is. I’ll see if I can produce some IL which would call null.Equals(null).

    EDIT: Okay, here’s some interesting code:

    .method private hidebysig static void  Main() cil managed
    {
      .entrypoint
      // Code size       17 (0x11)
      .maxstack  2
      .locals init (string V_0)
      IL_0000:  nop
      IL_0001:  ldnull
      IL_0002:  stloc.0
      IL_0003:  ldloc.0
      IL_0004:  ldnull
      IL_0005:  call instance bool [mscorlib]System.String::Equals(string)
      IL_000a:  call void [mscorlib]System.Console::WriteLine(bool)
      IL_000f:  nop
      IL_0010:  ret
    } // end of method Test::Main
    

    I got this by compiling the following C# code:

    using System;
    
    class Test
    {
        static void Main()
        {
            string x = null;
            Console.WriteLine(x.Equals(null));
    
        }
    }
    

    … and then disassembling with ildasm and editing. Note this line:

    IL_0005:  call instance bool [mscorlib]System.String::Equals(string)
    

    Originally, that was callvirt instead of call.

    So, what happens when we reassemble it? Well, with .NET 4.0 we get this:

    Unhandled Exception: System.NullReferenceException: Object
    reference not set to an instance of an object.
        at Test.Main()
    

    Hmm. What about with .NET 2.0?

    Unhandled Exception: System.NullReferenceException: Object reference 
    not set to an instance of an object.
       at System.String.EqualsHelper(String strA, String strB)
       at Test.Main()
    

    Now that’s more interesting… we’ve clearly managed to get into EqualsHelper, which we wouldn’t have normally expected.

    Enough of string… let’s try to implement reference equality ourselves, and see whether we can get null.Equals(null) to return true:

    using System;
    
    class Test
    {
        static void Main()
        {
            Test x = null;
            Console.WriteLine(x.Equals(null));
        }
    
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    
        public override bool Equals(object other)
        {
            return other == this;
        }
    }
    

    Same procedure as before – disassemble, change callvirt to call, reassemble, and watch it print true…

    Note that although another answers references this C++ question, we’re being even more devious here… because we’re calling a virtual method non-virtually. Normally even the C++/CLI compiler will use callvirt for a virtual method. In other words, I think in this particular case, the only way for this to be null is to write the IL by hand.


    EDIT: I’ve just noticed something… I wasn’t actually calling the right method in either of our little sample programs. Here’s the call in the first case:

    IL_0005:  call instance bool [mscorlib]System.String::Equals(string)
    

    here’s the call in the second:

    IL_0005:  call instance bool [mscorlib]System.Object::Equals(object)
    

    In the first case, I meant to call System.String::Equals(object), and in the second, I meant to call Test::Equals(object). From this we can see three things:

    • You need to be careful with overloading.
    • The C# compiler emits calls to the declarer of the virtual method – not the most specific override of the virtual method. IIRC, VB works the opposite way
    • object.Equals(object) is happy to compare a null “this” reference

    If you add a bit of console output to the C# override, you can see the difference – it won’t be called unless you change the IL to call it explicitly, like this:

    IL_0005:  call   instance bool Test::Equals(object)
    

    So, there we are. Fun and abuse of instance methods on null references.

    If you’ve made it this far, you might also like to look at my blog post about how value types can declare parameterless constructors… in IL.

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

Sidebar

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.