tags:

views:

92

answers:

3

I am relatively new to C# and I noticed something interesting today that I guess I have never noticed or perhaps I am missing something. Here is an NUnit test to give an example:

object boolean1 = false;
object booloan2 = false;
Assert.That(boolean1 == booloan2);

This unit test fails, but this one passes:

object string1 = "string";
object string2 = "string";
Assert.That(string1 == string2);

I'm not that surprised in and of itself that the first one fails seeing as boolean1 and boolean2 are different references. But it is troubling to me that the first one fails and the second one passes. I read (on MSDN somewhere) that some magic was done to the String class to facilitate this. I think my question really is why wasn't this behavior replicated in bool? As a note... if the boolean1 and 2 are declared as bool then there is no problem.

Does anyone know the reason for these differences or why it was implemented that way? Can anyone think of a situation where you would want to reference a bool object for anything except its value?

+3  A: 

It's because the strings are in fact referring the same instance. Strings are interned, so that unique strings are reused. This means that in your code, the two string variables will refer to the same, interned string instance.

You can read some more about it here: Strings in .NET and C# (by Jon Skeet)

Update
Just for completeness; as Anthony points out string literals are interned, which can be showed with the following code:

object firstString = "string1";
object secondString = "string1";
Console.WriteLine(firstString == secondString); // prints True

int n = 1;
object firstString = "string" + n.ToString();
object secondString = "string" + n.ToString();
Console.WriteLine(firstString == secondString); // prints False
Fredrik Mörk
Are strings interned by default?
abatishchev
Literals are (`string s = "some string";`), other instances are not.
Anthony Pegram
Coming from Java the intern pool makes sense, although Java does not explicitly overload the == with Equals behavior (which leads to some interesting discussion). But it seems odd that this behavior was not extended to boolean in C#. I mean there are always only two values to boolean to 'intern'. Also, for Googlers coming from Java, the boolean scenario works exactly opposite. Two different Objects that are boxed false values are ==. Thanks for the answer, Fredrik.
Ray Pendergraph
A: 

Operator Overloading.

The Boolean class does not have an overloaded == operator. The String class does.

CDSO1
No, this is wrong. Which version of operator== is called depends on the static type of the operands, which is `object`. Fredrik has the correct answer.
erikkallen
I appreciate the clarification. Thank you.
CDSO1
A: 

As Fredrik said, you are doing a reference compare with the boolean comparison. The reason the string scenario works is because the == operator has been overloaded for strings to do a value compare. See the System.String page on MSDN.

michaelkoss
No, this is wrong. Which version of operator== is called depends on the static type of the operands, which is `object`. Fredrik has the correct answer.
erikkallen