这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
在 C# 中, string 是 System.String (String) 的短手版本,它们基本上是相同的。
它就像博尔和布莱恩,没有太大的区别。
其他回答
坦率地说,在实践中通常没有System.String和 string之间的区别。
所有类型在 C# 是对象,所有衍生于 System.Object 类. 一个区别是,字符串是一个 C# 关键字,字符串可以用作变量名称. System.String 是这个类型的常规.NET 名称,字符串是方便的 C# 名称. 这里是一个简单的程序,显示 System.String 和字符串之间的区别。
string a = new string(new char[] { 'x', 'y', 'z' });
string b = new String(new char[] { 'x', 'y', 'z' });
String c = new string(new char[] { 'x', 'y', 'z' });
String d = new String(new char[] { 'x', 'y', 'z' });
MessageBox.Show((a.GetType() == typeof(String) && a.GetType() == typeof(string)).ToString()); // shows true
MessageBox.Show((b.GetType() == typeof(String) && b.GetType() == typeof(string)).ToString()); // shows true
MessageBox.Show((c.GetType() == typeof(String) && c.GetType() == typeof(string)).ToString()); // shows true
MessageBox.Show((d.GetType() == typeof(String) && d.GetType() == typeof(string)).ToString()); // shows true
@JonSkeet 在我的编辑器中
public enum Foo : UInt32 { }
我是Visual Studio 2015社区。
使用系统类型使您更容易在 C# 和 VB.Net 之间端口,如果您进入这种类型的东西。
有一个区别 - 您不能使用 String 没有使用系统; 提前。
string 只是 System.String 的标志,编辑器将以相同的方式处理它们。
唯一的实用差异是你提到的合成突出,如果你使用String,你必须使用系统写作。
字符串等于 System.String 在 VS2015 如果您写下此:
System.String str;
如何编辑器将显示潜在的修复,以优化它,并在应用该修复后,它将看起来像这个
string str;