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

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

当前回答

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

其他回答

字符串是一种序列的字符集,用于代表文本。

序列对象是代表序列的 System.Char 对象的序列集,而 System.Char 对象与 UTF-16 代码单位相匹配。

String 对象的值是 System.Char 对象的序列收藏的内容,这个值是不可改变的(即它仅是阅读的)。

有关线条不变性的更多信息,请参见MSDN中的不变性和StringBuilder类部分。

内存中的 String 对象的最大尺寸为 2GB 或大约 1 亿个字符。

注意 : 答案是从 msdn 帮助部分提取的. 在 msdn String Class 主题中,您可以在此处查看完整内容。

线条:一个线条对象被称为不可改变(仅阅读),因为它的值一旦创建,就无法修改。 似乎改变一个线条对象的方法实际上返回一个包含修改的新线条对象。

字符串: 字符串类型代表一个序列的零或多个 Unicode 字符. 字符串是字符串在.NET 框架中的字符串的标志. 字符串是内在的 C# 数据类型,是系统提供的类型“System.String”的标志。

String 与 String 的区别:

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

然而,两者之间没有区别 - 行似乎是考虑到其他开发者的源代码时最受欢迎的选项。

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