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

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

当前回答

标签: 代表一类

字符串: 代表一个 alias

这只是微软的编码协议。

其他回答

我更喜欢使用线条,因为这种类型是如此使用,我不希望合成亮度将其与所有其他类混合。虽然它是一个类,它被使用更像原始,所以我认为不同的亮度颜色是合适的。

如果你正确点击字符串的关键字,然后从背景菜单中选择去定义,它会带你到字符串类 - 它只是合成糖,但它提高了可读性 imo。

事实上,每个 C# 类型都有一个相当于.NET. 作为另一个例子,在 C# 地图中简短和 int 到 Int16 和 Int32 在.NET. 因此,技术上没有线和线之间的区别,但在 C# 中,线是.NET 框架中的线类的标志。

坦率地说,在实践中通常没有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社区。

String 是 System.String 的意思,它是.NET Framework 类型. String 是 System.String 的 C# 语言中的一个字符串. 两者都编写到 System.String 在 IL (中间语言),所以没有区别. 选择你喜欢什么,并使用它. 如果你在 C# 中编码,我会更喜欢字符串,因为它是 C# 类型的字符串和由 C# 程序员熟悉。

我可以说同样的事情(int,System.Int32)等。

在 C# 中, string 是 System.String (String) 的短手版本,它们基本上是相同的。

它就像博尔和布莱恩,没有太大的区别。