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

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

当前回答

字符串是关键词,你不能用字符串作为识别器。

条纹不是一个关键词,你可以用它作为一个标识:

例子

string String = "I am a string";

关键词行是 System.String 与关键词问题相同的标志,两者都是相同的。

 typeof(string) == typeof(String) == typeof(System.String)

其他回答

两者都是相同的,区别在于你如何使用它。

字符串为变量

String 是为了召唤其他 String 类方法

如:

string fName = "John";
string lName = "Smith";

string fullName = String.Concat(fName,lName);

if (String.IsNullOrEmpty(fName))
{
  Console.WriteLine("Enter first name");
}

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

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

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

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

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

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

如果有用的是真正看到没有线和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.String 的名称,类似的字符串存在于 Bool, Object, Int... 唯一微妙的区别是,你可以使用字符串,而没有“使用系统;” 指令,而 String 需要它(否则,你应该指定 System.String 完整)。

关于什么是最好的使用,我猜这是一个口味问题,我个人更喜欢链条,但我不是一个宗教问题。

两者之间没有区别,您可以在代码中使用其中一个。

System.String 是一个类(参考类型)定义的 mscorlib 在名空间系统. 换句话说, System.String 是 CLR 的类型。

string 是 C# 的关键词