String interning?

前端 未结 6 1452
抹茶落季
抹茶落季 2020-12-09 13:15

The second ReferenceEquals call returns false. Why isn\'t the string in s4 interned? (I don\'t care about the advantages of StringBuilder over string concatenation.)

6条回答
  •  青春惊慌失措
    2020-12-09 13:34

    Strings are immutable. This means their contents can't be changed.

    When you do s4 += "m"; internally, the CLR copies the string to another location in memory which contains the original string and the appended part.

    See MSDN string reference.

提交回复
热议问题