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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:06:28+00:00 2026-06-01T20:06:28+00:00

Possible Duplicate: Why check this != null? // Determines whether two strings match. [ReliabilityContract(Consistency.WillNotCorruptState,

  • 0

Possible Duplicate:
Why check this != null?

// Determines whether two strings match. 
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] 
public override bool Equals(Object obj)
{
    //this is necessary to guard against reverse-pinvokes and
    //other callers who do not use the callvirt instruction
    if (this == null)
        throw new NullReferenceException();

    String str = obj as String;
    if (str == null) 
        return false;

    if (Object.ReferenceEquals(this, obj)) 
        return true;

    return EqualsHelper(this, str);
}

The part I don’t understand is the fact that it is checking for the current instance, this, against null. The comment is a bit confusing, so I was wondering what does that comment actually mean?

Can anyone give an example of how this could break if that check was not there, and does this mean that I should also place that check in my classes?

  • 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-01T20:06:29+00:00Added an answer on June 1, 2026 at 8:06 pm

    That check is there as a guard against native code that may invoke the function with a null this pointer. This can’t happen in C#, so you don’t have to put similar guards in your code. It’s quite possible that the String class was written before C# was finalized and the author might have thought it important to guard against nulls, or maybe it’s just common to call String methods from native code and other places that make it easy to call methods on null.

    Note that even if you do manage to get called with a null this and you don’t have the guard, all that will happen is that the exception will be slightly different. It might be a different exception and it might get thrown by a different member, but otherwise it’s unlikely to make a difference.

    In other words, if the null check wasn’t there, the EqualsHelper (or one of its callees) would throw the exception rather than Equals. Since it’s desirable to hide the internals of the user-visible function, it makes sense to put the check right at the beginning.

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

Sidebar

Related Questions

Possible Duplicate: How to check whether 2 DirectoryInfo objects are pointing to the same
Possible Duplicate: Is there any reason to check for a NULL pointer before deleting?
Possible Duplicate: comparing two strings with comma seperated values I am working in C++,
Possible Duplicate: javascript - check if string begins with something? I've read in this
Possible Duplicate: How check intersection of DateTime periods Hello guys I have two datetime
Possible Duplicate: Why does C# require you to write a null check every time
Possible Duplicate: How can you check for a #hash in a URL using JavaScript?
Possible Duplicate: How can I check if an ip is in a network in
Possible Duplicate: Alphabetic equivalent of PHP is_numeric If you check if the data is
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on

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.