这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
字符串等于 System.String 在 VS2015 如果您写下此:
System.String str;
如何编辑器将显示潜在的修复,以优化它,并在应用该修复后,它将看起来像这个
string str;
其他回答
您不需要进口名称空间(使用系统;)来使用字符串,因为它是一个全球的名称 System.String。
要了解更多关于 Alias 的信息,您可以查看此链接。
StyleCop 规则,默认情况下,将允许您使用字符串作为一个 C# 代码风格的最佳实践,除非提到 System.String 静态功能,如 String.Format, String.Join, String.Concat 等。
基本上,在 C# 中,线和线之间没有区别。
String 是 System.String 中的.NET 框架中的一个类,而较低的案例字符串则是 System.String 的标志。
登录两个类型的完整名称可以证明这一点
string s1= "hello there 1";
String s2 = "hello there 2";
Console.WriteLine(s1.GetType().FullName); // System.String
Console.WriteLine(s2.GetType().FullName); // System.String
大多数开发人员使用字符串在C#中声明变量,并使用System.String类使用任何内置的字符串方法,例如,String.IsNullOrEmpty()方法。
坦率地说,在实践中通常没有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# 中, string 是 System.String (String) 的短手版本,它们基本上是相同的。
它就像博尔和布莱恩,没有太大的区别。