Drew Chapin
Drew Chapin

Reputation: 7989

Confusing behavior comparing "" to String.Empty and Null Coalesce operator (??)

enter image description here

The behavior shown here is very perplexing to me. If "" is equal to String.Empty, and null coalesce (??) works on "" ?? "It works" then why doesn't test ?? "It works" or String.Empty ?? "It works" act accordingly?

Note: Console.WriteLine("It works",String.Empty ?? "It works"); throws the exact same AssertFailedException

** UPDATE #1 **

The current answers are not agreeing with the results. As seen in these examples...

These do not agree with each other. It's a case of A = B, B = C, but A != C

enter image description here

** UPDATE #2 **

Answering @Willem Van Onsem's question about using Console.WriteLine("" == null);

enter image description here enter image description here

This would be the expected results. So, it seems this is possibly a problem specific to Assert.AreEqual(...,...)

** UPDATE #3 **

Please be aware that I'm perfectly aware of, and was before posting this question, how the ?? works and that "" == null is false. I know that it is only supposed to check equality to null. I know that x ?? y is equivalent to x != null ? x : y. This has been repeated entirely too much in the answers and comments to the point it's becoming insulting. Think about this, if I didn't know how the operator is supposed to work and thought anything past the first Assert was supposed to pass, then why would I be confused and post the question?

Upvotes: 0

Views: 1522

Answers (3)

Novaterata
Novaterata

Reputation: 4809

I'm going to go out on a limb and say that there is an IDE bug in which the Debug Exception alert box is pointing to the wrong line. These things happen. Stacktraces can lie, code can end up optimized so that the ordering is different. Who knows. In order to verify:

var test = String.Empty;

Assert.AreEqual("", String.Empty);
Assert.AreEqual("Case 1", "" ?? "Case 1");
Assert.AreEqual("Case 2", test ?? "Case 2");
Assert.AreEqual("Case 3", String.Empty ?? "Case 3");

Here is a link to someone reporting the issue to Microsoft with both confirmation by a 2nd person and a video that shows it happening: https://connect.microsoft.com/VisualStudio/feedback/details/641947/exception-assistant-highlights-incorrect-line-of-code

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477308

The null-coalescing operator ?? checks for null. x ?? y is short for:

x == null ? y : x

So if you have an empty string, then the x == null fails (meaning it is false), and thus "" ?? "It works" results in "", as we can see in the csharp interactive shell:

csharp> "" ?? "It works"
""
csharp> "" == null
false

Or as specified in the documentation:

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

EDIT: about update #1:

When I run the queries on the csharp interactive shell (this is the Mono C# shell):

csharp> "" == String.Empty
true                                    
csharp> null == ""                      
false                                   
csharp> null == String.Empty            
false

Something seems to be messed up with the IDE.

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726919

Null coalesce operator understands null, but has no idea about an empty string, which, obviously, different from null.

If you would like to treat an empty string and a null in the same way, use string.IsNullOrEmpty:

Assert.AreEqual("It works", !String.IsNullOrEmpty(test) ? test : "It works")

Upvotes: 4

Related Questions