Pretending .NET strings are value type

后端 未结 4 1851
死守一世寂寞
死守一世寂寞 2021-02-09 11:45

In .NET, strings are immutable and are reference type variables. This often comes as a surprise to newer .NET developers who may mistake them for value type objects due to their

4条回答
  •  温柔的废话
    2021-02-09 12:26

    String is a special breed. They are reference type yet used by most coders as a value type. By making it immutable and using the intern pool, it optimizes memory usage which will be huge if it's a pure value type.

    More readings here:
    C# .NET String object is really by reference? on SO
    String.Intern Method on MSDN
    string (C# Reference) on MSDN

    Update:
    Please refer to abel's comment to this post. It corrected my misleading statement.

提交回复
热议问题