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

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 ]

其他回答

string 是.NET 框架中的 String 的标志。

在哪里“String”实际上是 System.String。

我会说它们是可交换的,没有区别什么时候和在哪里你应该使用一个或另一个。

最好是和你所使用的相一致。

对于什么是值得的,我使用链来宣布类型 - 变量,属性,回报值和参数. 这与其他系统类型的使用一致 - int, bool, var 等(尽管 Int32 和 Boolean 也是正确的)。

我使用 String 在 String 类上使用静态方法,如 String.Split() 或 String.IsNullOrEmpty()。我觉得这更有意义,因为方法属于一个类,并且与我使用其他静态方法一致。

它已被覆盖; 但是,你不能在反射中使用线条; 你必须使用线条。

基本上,在 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()方法。

但从编码指南的观点,最好使用字符串而不是字符串,这就是开发人员通常使用的。 例如,而不是使用 Int32 我们使用 int 作为 int 是 alias 到 Int32

FYI “关键字行仅仅是预先定义的类 System.String 的名称” - C# 语言规格 4.2.3 http://msdn2.microsoft.com/En-US/library/aa691153.aspx

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

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