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

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

当前回答

绳子指一个带物体,它具有各种功能来操纵所包含的绳子。

字符串指一个原始类型

在 C# 中,他们都编写到 String 但在其他语言中,他们不这样做,你应该使用 String 如果你想处理 String 对象和 string 如果你想处理字母。

其他回答

如果有用的是真正看到没有线和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 ]

没有区别。

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

同样, int 地图到 System.Int32.

实际上没有区别

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

与其他程序员中似乎是常见的做法相反,我更喜欢线条超越线条,只是为了强调线条是一种参考类型,正如Jon Skeet提到的那样。

字符串是保留的单词,但字符串只是一个类名称,这意味着字符串本身不能作为变量名称使用。

如果出于某种原因,你想要一个变量称为字符串,你只会看到这些编辑中的第一个:

StringBuilder String = new StringBuilder();  // compiles
StringBuilder string = new StringBuilder();  // doesn't compile 

如果你真的想要一个变量名称,称为字符串,你可以使用 @ 作为预定:

StringBuilder @string = new StringBuilder();

另一个关键差异:Stack Overflow以不同的方式突出它们。