这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
字符串是关键词,你不能用字符串作为识别器。
条纹不是一个关键词,你可以用它作为一个标识:
例子
string String = "I am a string";
关键词行是 System.String 与关键词问题相同的标志,两者都是相同的。
typeof(string) == typeof(String) == typeof(System.String)
其他回答
使用系统类型使您更容易在 C# 和 VB.Net 之间端口,如果您进入这种类型的东西。
有一个区别 - 您不能使用 String 没有使用系统; 提前。
Jeffrey Richter 写道:
另一种方法是C#编译器自动假设您在所有源代码文件中使用下列指南:
using int = System.Int32;
using uint = System.UInt32;
using string = System.String;
...
我已经看到一些开发人员混淆了,不知道是否在他们的代码中使用线条或线条. 因为在C#线条(一个关键词)地图准确到System.String(一个FCL类型),没有区别,也可以使用。
如果有用的是真正看到没有线和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 ]
字符串等于 System.String 在 VS2015 如果您写下此:
System.String str;
如何编辑器将显示潜在的修复,以优化它,并在应用该修复后,它将看起来像这个
string str;