0
string x = "alok b";

string y = "alok b";

string z = "alok";

//y += x.Replace(y, string.Empty);
z += " b";

Console.WriteLine(object.ReferenceEquals(x,y));
Console.WriteLine(object.ReferenceEquals(y, z));

How is first line is printing true and second false?

and changing to below statement is printing true.

Console.WriteLine(object.ReferenceEquals(y,string.Intern(z)));
Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
  • http://stackoverflow.com/questions/38010/c-sharp-string-concatenation-and-string-interning – Mike Miller May 06 '16 at 10:14
  • The first two are string literals which are automatically added to the string-pool, which saves memory since the same string can be used multiple times. That's why `object.ReferenceEquals(x,y)` returns `true`. Then you are using a method to modify an existing string(actually you are creating a new since strings are immutable). All runtime strings are not stored in the string-pool. That's why `object.ReferenceEquals(y, z)` returns `false`. – Tim Schmelter May 06 '16 at 10:16
  • @TimSchmelter seems fair explanation, and string.Intern(z) also gives the same value (i.e. "alok b") but using this cause to print true, why? – Kylo Ren May 06 '16 at 10:21
  • 1
    @KyloRen: because [`String.Intern`](https://msdn.microsoft.com/en-us/library/system.string.intern(v=vs.110).aspx) works in that way: it returns _"the system's reference to str, if it is interned; otherwise, a new reference to a string with the value of str."_ In this case that string is already in the pool(because you've used `string y = "alok b"` which is a string literal), so you get the same reference to it as return value. – Tim Schmelter May 06 '16 at 10:31
  • @TimSchmelter when you say system's reference do you mean the reference in the string-pool you mentioned in your above comment? – Kylo Ren May 06 '16 at 10:33
  • 1
    @KyloRen: that was a quote from msdn(have you read it?). Yes, that's meant – Tim Schmelter May 06 '16 at 10:34
  • @TimSchmelter ok,when i see the behave of string outputs from that perspective, the outputs are coming as expected. thanks – Kylo Ren May 06 '16 at 10:40

2 Answers2

1

It's called string interning.

Roger
  • 1,944
  • 1
  • 11
  • 17
  • As per my knowledge when a new value is assigned to a string reference , it will be assigned a new object of string, but that will still be assigned to same ref, z in my case, so the last statement should also print true if first one is doing so. – Kylo Ren May 06 '16 at 10:16
  • @KyloRen but z was never referencing y. – parachutingturtle May 06 '16 at 10:20
  • @parachutingturtle yes and y is never referencing x. – Kylo Ren May 06 '16 at 10:22
  • @KyloRen x and y are "interned" because they are known to reference the same literal at compile time. z is created at runtime. – parachutingturtle May 06 '16 at 10:24
  • @parachutingturtle hmmm.... and when I do string.Intern(z) in place of z then it prints true. can you explain that? – Kylo Ren May 06 '16 at 10:26
0

When you create a string it creates an object (x).

when you create y, you just point to it"again", it points to the same one (has it).

When you create Z, and the do the += it creates a NEW one altogether, hence, it won't match the address in the memory for the previous one.

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • at last assignment a new object will be created and z is the reference to it. now since x & y should be same as y & z. shouldn't they? – Kylo Ren May 06 '16 at 10:14
  • @KyloRen They are not the same. They read the same, but they are references to two different places in memory. – parachutingturtle May 06 '16 at 10:18
  • @parachutingturtle if x and y is pointing to same string value, and so does z (eventually) why are they printing different output? – Kylo Ren May 06 '16 at 10:25