这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
如果有用的是真正看到没有线和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 在 VS2015 如果您写下此:
System.String str;
如何编辑器将显示潜在的修复,以优化它,并在应用该修复后,它将看起来像这个
string str;
在 MSDN 文档的背景下, String 类像任何其他数据类型(例如 XmlReader、StreamReader)一样在 BCL 中文档。
字符串被记录为一个关键字(C#参考)或任何基本的C#语言构造(例如,为,同时,默认)。
参考。
在 C# 中, string 是 System.String (String) 的短手版本,它们基本上是相同的。
它就像博尔和布莱恩,没有太大的区别。
两者都是相同的,区别在于你如何使用它。
字符串为变量
String 是为了召唤其他 String 类方法
如:
string fName = "John";
string lName = "Smith";
string fullName = String.Concat(fName,lName);
if (String.IsNullOrEmpty(fName))
{
Console.WriteLine("Enter first name");
}