String Comparison

M

MaSTeR

Can anyone provide a practical short example of why in C# I shouldn't
compare two strings with == ?

If I write this in JAVA
String string1 = "Widget";

if (string1 == "Widget")

System.Console.WriteLine("String1 Equal.");

String string2 = new String("Widget");

if (string2 == "Widget")

System.Console.WriteLine("String2 Equal.");

The comparison og string2 will fail because the first time string1 and
"Widget" will be the same object because the VM use interning to avoid
repeated strings in memory.

String string2 = new String("Widget"); will tell the VM that I want a brand
new object don't intern this string (that is don't make it a new reference
to the old object string1 points to) hence the comparison will fail.

if in C# you write the same code changing

String string2 = new String("Widget");
to

String string2 = new String(new char[]{'W','i','d','g','e','t'});



The second comparison will succeed anyway, it seems there no obvious way to
make the same string be stored in different object because .Net forces
interning.



Thanks for your help.
 
J

Jon Skeet [C# MVP]

MaSTeR said:
Can anyone provide a practical short example of why in C# I shouldn't
compare two strings with == ?

You should, IMO - in general.
If I write this in JAVA
String string1 = "Widget";

if (string1 == "Widget")

System.Console.WriteLine("String1 Equal.");

String string2 = new String("Widget");

if (string2 == "Widget")

System.Console.WriteLine("String2 Equal.");

The comparison og string2 will fail because the first time string1 and
"Widget" will be the same object because the VM use interning to avoid
repeated strings in memory.

String string2 = new String("Widget"); will tell the VM that I want a brand
new object don't intern this string (that is don't make it a new reference
to the old object string1 points to) hence the comparison will fail.

Well, also because == is doing a reference comparison.
if in C# you write the same code changing

String string2 = new String("Widget");
to

String string2 = new String(new char[]{'W','i','d','g','e','t'});

The second comparison will succeed anyway, it seems there no obvious way to
make the same string be stored in different object because .Net forces
interning.

No it doesn't. What's actually happening here is that the overloaded
==(string, string) operator is being used. If you do:

object string1 = "Widget";
object string2 = new string (new char[]{'W','i','d','g','e','t'});

Console.WriteLine (string1==string2);

that will print False, because the compiler doesn't know to use the
overloaded version.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


MaSTeR said:
Can anyone provide a practical short example of why in C# I shouldn't
compare two strings with == ?

You can, provide an example where you get an error, String has overloaded
the == operator because of this instead of comparing two strings as
references types ( the real thing) they are compared as valued types.
With this in mind if string1 and string2 has the same sequence of
characters they are equals, no matter which way you use to get them.

The second comparison will succeed anyway, it seems there no obvious way
to
make the same string be stored in different object because .Net forces
interning.


As I said before it has nothing to do with that but with the fact that the
== operator is overloaded.


Cheers,
 
A

Anders Borum

Hello Jon
that will print False, because the compiler doesn't know to use the
overloaded version.

What's the difference between:

if (string1 == string2) { .. }

and

Console.WriteLine (string1==string2);

... In terms of how the compiler looks at the comparison? Both evaluates to a
boolean operation, right? So how could the compiler know if it's supposed to
compare instances or the overloaded operator?

Thanks in advance.
 
B

Bjorn Abelli

...
What's the difference between:

if (string1 == string2) { .. }

and

Console.WriteLine (string1==string2);

.. In terms of how the compiler looks at the comparison?

In the first example the OP used variables of type string.

In the second example Jon used variables of type object.

The string type has overloaded the == operator, so in the first case the
overloaded method for comparison will be used. The object type doesn't have
an overloaded method for ==, so there it will only compare the references.


// Bjorn A
 
A

Anders Borum

Hello!
In the first example the OP used variables of type string.
In the second example Jon used variables of type object.

Ofcourse - I must have become blind to the obvious these days .. thanks for
clearing that out. The other stuff didn't make sense to me :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top