13

Here is how my last interview went:

Question: Where are strings stored?

Answer: Heap since it is a reference type

Question: Explain me this following code:

static void Main(string[] args)
{
     string one = "test";
     string two = one;
     one = one + " string";

     Console.WriteLine("One is {0}" , one);
     Console.WriteLine("Two is {0}", two);
}

Answer: Drew two diagrams like below:

(represents the statement, string two = one;

enter image description here

(represents the statement, one = one + " string";. A new string is created on heap and assigned)

enter image description here

Question: Correct. Draw similar for the code snippet below:

class Program
{
    static void Main(string[] args)
    {
        Test one = new Test { someString = "test" };
        Test two = one;
        one.someString = "test String";

        Console.WriteLine("One is {0}", one.someString);
        Console.WriteLine("Two is {0}", two.someString);
    }
}

class Test
{
    public string someString { get; set; }
}

Answer:

[Test two = one] enter image description here

one.someString = "test String";

enter image description here

Question: Ok. You said both strings and classes are reference types. Then why did a new object for string is created and assigned the value whereas for class, the existing string property itself gets modified ?

Answer: Because strings are immutable whereas classes are mutable.

(Though I cleared the interview, I still did not understand this behaviour. Why did the designers of class make it mutable while keeping strings immutable ? There are a lot of therotical answers everywhere, but could any one make it simple by explaining this particular behaviour with the above code ?)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

4 Answers4

4

One of reasons strings were made immutable, even though they are reference types, was to make them look and behave like primitive types (e.g., int, double, float).

That's also the reason why strings are the only reference type that can be represented as a literal (e.g., "some string"). Lots of other languages take the same approach, like Java for example.

dcastro
  • 66,540
  • 21
  • 145
  • 155
3

Strings are immutable because logically, they are a single value, and being mutable would lead to a lot of unexpected behavior.

However, strings are not value types, because they tend to be passed around a lot, which would require a lot of copying of values. This would become quite expensive, especially for large strings.

So in order to get the best of both worlds, strings in .Net are reference types, but also immutable.

Rik
  • 28,507
  • 14
  • 48
  • 67
1

The command one = ... sets the *pointer named one to a new value. Since two is a different pointer, it retains it's original value.

The command one.someString = ... modifies the object referred by one. two is still a different pointer, but since it points to the same object, the modifications are shared between them.

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
0

Strings share buffer. In your case if one and two were strings, they would be separate objects (two objects on the heap), but internally they would point to the same buffer (third object on the heap).

Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43