1

Section 7.10.7 of C# Programming Language (A. Hejlsberg, et. al, 2011 Fourth Edition, Annotated for 4.0 ) states that, " When two separate string instances contain the exact same sequences of characters, the values of the strings are equal, but the references are different."

Is this ever the run-time case? Won't the .NET runtime and BCL ALWAYS intern two identical strings to the same reference box?

I understand the book is talking about a 'spec', but the next question - is there ANY known implementation of C# that behaves this way?

EDIT

OK what's really bothering me is this:

String FirstOne = "StatusOK"
String MiddleOne = "StatusOK"
String LastOne = "StatusOK"

Integer FindMeOne(object AnythingtoFind) { return MsgLst.IndexOf(AnythingToFind) }

List<String> MsgLst;
MsgLst.Add(FirstOne);
MsgLst.Add(MiddleOne);
Msglst.Add(LastOne);

Integer foo = FindMeOne( LastOne );

I don't expect foo to be 1! I guess I'm just silly that way.

UPDATE All I have to do to get what I want is:

public class MyNiceStringsThatICanFind 
{ 
private String foobar;
}

But again this is not nice. If objects track themselves by HashCode, what is so stinking special about a string? Why doesn't the runtime look at your Objects and see, hey, maybe it is the exact same thing inside, let's make it the same reference.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Andyz Smith
  • 698
  • 5
  • 20
  • I just built and ran this using VS 2013; Framework V4.5.2 and it returned 0 as expected and documented. Perhaps you have a typo somewhere. Likewise with Framework V4.5. This happens both with `static List MsgLst;` and with `static List MsgLst;`. – Pieter Geerkens May 25 '15 at 03:08
  • 1
    Are you expecting the run-time to keep track of all the strings in memory as it computes a new string to see if the new string should share a reference? This would be especially wasteful for a computation that generated a lot of temporary strings. – Enigmativity May 25 '15 at 03:16
  • If I refer to a String as an Object, then I want that Object back. Period. Interning causes a leak in that metaphor. – Andyz Smith May 25 '15 at 03:22
  • @Pieter Geerkens ok, i'm just saying I don't want the first one, I want the last one. So that's 2 that I expect, not 0. – Andyz Smith May 25 '15 at 03:23
  • Documentation for `ListIndexOf(T)`: *Searches for the specified object and returns the zero-based index of the **first occurrence** within the entire List.* Link:https://msdn.microsoft.com/en-us/library/e4w08k17(v=vs.110).aspx – Pieter Geerkens May 25 '15 at 03:27
  • Yeah, I got you man. – Andyz Smith May 25 '15 at 03:29

1 Answers1

1

This equality behaviour only applies to string literals (although see NoStringInterning). If you create equal strings at runtime they will not necessarily refer to the same instance:

var abc = "abc";
var ab = "ab";
var c = "c";

var abc2 = ab + c;
bool eq = Object.ReferenceEquals(abc, abc2);  //false

var interned = String.Intern(abc2);
bool ieq = Object.ReferenceEquals(interned, abc);  //true
Lee
  • 142,018
  • 20
  • 234
  • 287
  • And " (object)abc == (object)abc2 " will produce similar results? – Andyz Smith May 23 '15 at 18:26
  • @AndyzSmith - `==` for `object` tests reference equality, so yes. – Lee May 23 '15 at 18:30
  • Why am I seeing something like this "The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system." https://msdn.microsoft.com/en-us/library/system.string.intern%28v=vs.110%29.aspx – Andyz Smith May 23 '15 at 18:37
  • 1
    Created programmatically being the key phrase. – Andyz Smith May 23 '15 at 18:38
  • Mr. Skeet has something enlightening to add: "As others have explained, string interning occurs for all string literals, but not on "dynamically created" strings (e.g. those read from a database or file, or built using StringBuilder or String.Format.)" http://stackoverflow.com/questions/555871/c-strings-with-same-contents – Andyz Smith May 23 '15 at 18:48