这两者之间有什么区别,我应该使用哪一个?

string s = "Hello world!";
String s = "Hello world!";

当前回答

实际上没有区别

关键字 C# 连接地图到.NET 类型的 System.String - 这是一个标签,保持到语言的名称公约。

其他回答

在 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社区。

System.String 是.NET 字符串类 - 在 C# 字符串中,它是 System.String 的标志 - 所以在使用中,它们是相同的。

至于指南,我不会过于沉默,只是使用你喜欢的东西 - 生活中有更多的重要事物,代码将是相同的,无论如何。

如果你找到自己的建造系统,需要指定你正在使用的整合器的尺寸,因此倾向于使用 Int16, Int32, UInt16, UInt32 等,那么它可能看起来更自然地使用 String - 并且在不同的.net 语言之间移动时,它可能会使事情更容易理解 - 否则我会使用 String 和 int。

string 是 System.String 的 C# 中的一个 alias. 所以技术上,没有区别. 它就像 int vs. System.Int32.

至于指导方针,一般建议在您提到对象时使用字符串。

吉。

string place = "world";

同样,我认为一般建议使用 String 如果你需要具体提到课堂。

吉。

string greet = String.Format("Hello {0}!", place);

这是微软在其例子中使用的风格。

似乎该领域的指导方针可能已经改变了,因为StyleCop现在强制使用C#特定的联盟。

表示带变量与带,但在访问其静态成员之一时使用带类:

String.Format()

变量

string name = "";