5

On my VS 2015 compiler, I tested that

    static void Main(string[] args)
    {
        string str1 = null;
        string str2 = null;
        if(str1==str2)  //they are the same on my machine
        {
        }
    }

But this is a documented behavior? NULL by definition, is an undefined behavior, so comparing NULL to another NULL could be undefined. It could happen that on my machine, using my current .Net framework, the two NULLs turn out to be the same. But in the future, they could be no longer the same.

In that case, my code will break silently.

Is it safe to always assume that the above two NULL strings are always the same?

6
  • 5
    Strange question. Commented Jul 31, 2018 at 8:32
  • 3
    In a managed environment I do not see how null is undefined behavior. Commented Jul 31, 2018 at 8:33
  • 6
    NULL doesn't have a universal meaning. Just because in some languages (say, SQL, since I see you've done some mysql stuff on here in the past) NULL has a meaning closely associated with "unknown" or "undefined" behaviour, it doesn't mean the same is true of C#'s null. Commented Jul 31, 2018 at 8:35
  • 1
    In C# and .NET, null has a well-defined meaning, and does not mean "undefined behavior". Commented Jul 31, 2018 at 8:38
  • 1
    @JᴀʏMᴇᴇ: why is it strange? If your application relies on it you have to know if it's something you can rely on, or if it might break under certain circumstances or in future versions of the .NET framework. Btw, this is even true for VB.NET where Nothing really means unspecified/undefined(or default value). Commented Jul 31, 2018 at 9:19

2 Answers 2

15

Yes, that's documented here

If both a and b are null, the method returns true.

and this method is used when you use ==, which is mentioned here.

calls the static Equals(String, String) method

Sign up to request clarification or add additional context in comments.

4 Comments

@MickyD Make that 10. i need to use up my upvotes for the day
ok ok...11. I do like the static Equals. Only discovered that recently ;)
@MickyD: i like the static Equals because of this overload which enables to compare in a case insensitive manner(as opposed to ==) and because it avoids a NullReferenceException which could happen with the instance method.
Agreed good sir. (less verbose than string.Compare (...) too)
2

If both strings are null, the method always return true because == are used for reference comparison. In simple words, == checks if both objects point to the same memory location.

I tried this example with java str1.Equals(str2) and this returns Null Pointer Exception, because .Equals evaluates to the comparison of values in the objects.

Hope it helps you.

4 Comments

Operator overloading means that you cannot assume that == is always reference comparison. String is a class that does overload it
You are getting a NullPointerException because you are trying to access the .Equals method of a null object, not because you compare the values. Try with string.Equals(str1, str2), you'll get true.
Also, it's a NullReferenceException, not a NullPointerException
Sorry, the point of srt1.Equals(str2) is in java context and in java it returns NullPointerException. I qoute that example just to make clear understanding. However in c#, string.Equals(str1, str2) returns true, beacause it checks equality on the basis of content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.