Why is new String(“Hello”) invalid in C#?

后端 未结 9 2500
太阳男子
太阳男子 2021-02-19 03:41

What is the logic/reason behind making

String s= new String(\"Hello World\");

Illegal in C#? The error is

The best over

9条回答
  •  误落风尘
    2021-02-19 04:21

    Because strings are immutable and have language support in how they are constructed.

    In your example, since you are using a string literal, it will get interned. Any copied string that would have been created from it would end up being the same exact reference, as it would come from the intern pool.

提交回复
热议问题