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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:12:39+00:00 2026-06-18T02:12:39+00:00

Consider the following code that I was reviewing: public override bool Equals(object other) {

  • 0

Consider the following code that I was reviewing:

public override bool Equals(object other)
{
    return !object.ReferenceEquals(null, this)
           && (object.ReferenceEquals(this, other)
           || ((other is MyType) && this.InternalEquals((MyType)other)));
}

The first line in this code triggered my curiosity. Whenever this is null, the method should return false. Now I am pretty sure the programmer meant to write !object.ReferenceEquals(other, null), to shortcut situations with null, but he’s insistent that this can be null. I’m insistent that it cannot (unless someone uses direct memory manipulation). Should we leave it in?

  • 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-18T02:12:40+00:00Added an answer on June 18, 2026 at 2:12 am

    While I certainly wouldn’t normally check this for nullity, it’s possible, without any actual memory nastiness – just a bit of reflection:

    using System;
    
    public class Test
    {
        public void CheckThisForNullity()
        {
            Console.WriteLine("Is this null? {0}", this == null);
        }
    
        static void Main(string[] args)
        {
            var method = typeof(Test).GetMethod("CheckThisForNullity");
            var openDelegate = (Action<Test>) Delegate.CreateDelegate(
                   typeof(Action<Test>), method);
            openDelegate(null);
        }
    }
    

    Alternatively, generate IL which uses call instead of callvirt to call an instance method on a null target. Entirely legit, just not something the C# compiler would normally do.

    This has nothing to do with finalization, which is hairy in its own right but in different ways. It’s possible for a finalizer to run while an instance method is executing if the CLR can prove that you’re not going to use any fields in the instance (which I would strongly expect to include the this reference).

    As for the code presented – nope, that looks like it’s just a mistake. I would rewrite it as:

    public override bool Equals(object other)
    {
        return Equals(other as MyType);
    }
    
    public bool Equals(MyType other)
    {
        if (ReferenceEquals(other, null))
        {
            return false;
        }
        // Now perform the equality check
    }
    

    … assuming that MyType is a class, not a struct. Note how I’m using another public method with the right parameter type – I’d implement IEquatable<MyType> at the same time.

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

Sidebar

Related Questions

Consider the following code that uses JSch to create an SSH connection: public class
Consider following code public class City { public string Name { get { return
Consider this following code (that retrieves the response from a HTTP request and prints
Consider the following code snippet: bool SomeObject::equal(const SomeObject& rhs) const { if (this ==
Consider the following code sample that uses MEF to create an object of type
Consider the following code and assume that list is an synchronized List. List list
Consider the following code. It's an anchor tag with an id called leader-module-total that
Consider the following code: int i = 3 << 65; I would expect that
Consider the following code: while(true) { someFunction(); Thread.sleep(1000); } What I want is that,
Consider the following code: public interface A { public A another(); } public interface

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.