这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
您不需要进口名称空间(使用系统;)来使用字符串,因为它是一个全球的名称 System.String。
要了解更多关于 Alias 的信息,您可以查看此链接。
其他回答
使用系统类型使您更容易在 C# 和 VB.Net 之间端口,如果您进入这种类型的东西。
string 只是 System.String 的标志,编辑器将以相同的方式处理它们。
唯一的实用差异是你提到的合成突出,如果你使用String,你必须使用系统写作。
字符串不是一个关键词,它可以用作识别器,而字符串是一种关键词,不能用作识别器。
字符串是 System.String 的简称,字符串或 System.String 是 CTS(通用类型系统)中的字符串的名称。
如果有用的是真正看到没有线和System.String之间的区别:
var method1 = typeof(MyClass).GetMethod("TestString1").GetMethodBody().GetILAsByteArray();
var method2 = typeof(MyClass).GetMethod("TestString2").GetMethodBody().GetILAsByteArray();
//...
public string TestString1()
{
string str = "Hello World!";
return str;
}
public string TestString2()
{
String str = "Hello World!";
return str;
}
两者都产生相同的 IL 比特序列:
[ 0, 114, 107, 0, 0, 112, 10, 6, 11, 43, 0, 7, 42 ]