这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
.NET 类型与其他对象类型相同的颜色(值类型是合适的对象,毕竟)。
条件和控制的关键词(如如果,交换和返回)是下层和彩色黑蓝色(默认情况下)。
考虑一下:
String someString;
string anotherString;
其他回答
字符串是保留的单词,但字符串只是一个类名称,这意味着字符串本身不能作为变量名称使用。
如果出于某种原因,你想要一个变量称为字符串,你只会看到这些编辑中的第一个:
StringBuilder String = new StringBuilder(); // compiles
StringBuilder string = new StringBuilder(); // doesn't compile
如果你真的想要一个变量名称,称为字符串,你可以使用 @ 作为预定:
StringBuilder @string = new StringBuilder();
另一个关键差异:Stack Overflow以不同的方式突出它们。
如果有用的是真正看到没有线和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 的简称,字符串或 System.String 是 CTS(通用类型系统)中的字符串的名称。
两者之间没有区别,您可以在代码中使用其中一个。
System.String 是一个类(参考类型)定义的 mscorlib 在名空间系统. 换句话说, System.String 是 CLR 的类型。
string 是 C# 的关键词
在 MSDN 文档的背景下, String 类像任何其他数据类型(例如 XmlReader、StreamReader)一样在 BCL 中文档。
字符串被记录为一个关键字(C#参考)或任何基本的C#语言构造(例如,为,同时,默认)。
参考。