为什么新的String(“ Hello”)在C#中无效? [英] Why is new String("Hello") invalid in C#?
问题描述
制作
String s= new String("Hello World");
在C#中非法?
错误是
Illegal in C#? The error is
string.String(char *)的最佳重载方法匹配具有无效参数
The best overloaded method match for `string.String(char*)' has some invalid arguments
我对API文档不感兴趣,我对为什么这是非法的感兴趣。
I'm not interested in the API docs, I am interested in why this is illegal.
是因为池化了静态字符串吗?像Java池将Integer(-128)到Integer(127)产生可怕的结果? (当然也是字符串)
Is is because of pooling static strings? like Java pools Integer(-128) to Integer(127) with horrendous results? ( of course strings too )
推荐答案
使用构造函数根据现有的另一个字符串创建新字符串将毫无意义-这就是为什么没有允许这样做的构造函数重载的原因。
It would be rather pointless to use the constructor to create a new string based on another existing string - that's why there is no constructor overload that allows this. Just do
string s = "Hello World";
这篇关于为什么新的String(“ Hello”)在C#中无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!