Make a reference to another string in C#

前端 未结 5 770
夕颜
夕颜 2021-01-19 22:25

As far as I know a string in C# is a reference type.

So in the following code \'a\' should be equal to \"Hi\", but it still keeps its value which is \"Hello\". Why?<

5条回答
  •  暖寄归人
    2021-01-19 22:52

    The line b = "Hi"; changes which string b references. a still references "Hello".

    string a = "Hello";  // Set a to reference the string "Hello"
    string b = a;        // Set b to reference the same string as a
    b = "Hi";            // Set b to reference the string "Hi"
    

提交回复
热议问题