MBen
MBen

Reputation: 3996

Why does string equality compares characters?

I am just a bit confused with strings, and their comparison. What I understand is that doing this :

string one = "stackoverflow";
string two = "stackoverflow";

bool equal = one == two;

This will compare character by character right?

Why is this the case? if string are immutable, and two variables will always refer to the same string if they have equal characters. Why doesn't the compiler just checks the references? If there is one place where I would think that references equality means value equality I would think that would be for strings. What am I missing?

Upvotes: 2

Views: 130

Answers (6)

Oded
Oded

Reputation: 498904

It is true that string literals are automatically interned, but not all strings are interned. When you build a string dynamically or get it somehow during runtime, it will not be interned by default - you need to call String.Intern for that to happen.

This means you can have identical string instances with different references.

So, in your case, if you dynamically build the string "stackoverflow" twice and assign each to the variables one and two, the references of one and two will be different, though the value is identical.

Upvotes: 7

Sean
Sean

Reputation: 62472

Only string literals are interned explicitly. For example if you do this:

string x=Console.ReadLine();
string y=Console.ReadLine();

And enter "hello" for both then they're no requirement that x and y will reference the same string. Therefore when they're compared the underlying implementation will need to do a character by character comparison.

It's possible to do do your own interning at runtime by calling string.Intern.

Upvotes: 1

Stefan Paul Noack
Stefan Paul Noack

Reputation: 3734

This is explained here: http://msdn.microsoft.com/en-us/library/362314fe.aspx

Basically, it's because even if two equal strings might be the same instance due to interning, being different instances doesn't always mean they're different in content.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160852

Strings are immutable, that doesn't mean that there cannot be two separate string instances that have the same characters - those arguably should be considered equal too. This is the case for all strings assembled at run-time (non-literals) which are not interned by default.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292345

and two variables will always refer to the same string if they have equal characters

What make you think that? This is not true... you can have several distinct instances of String that have the same characters:

string s = "hello world";
string s1 = s.Substring(0, 5);
string s2 = s.Substring(0, 5);

bool sameInstance = ReferenceEquals(s1, s2); // False

Upvotes: 1

Stephan Bauer
Stephan Bauer

Reputation: 9249

[...] Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive. [...]

See MSDN documentation

Upvotes: 3

Related Questions