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

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

当前回答

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

字符串为变量

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

如:

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

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

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

其他回答

图像结果为 string vs String www.javatpoint.com 在 C# 中, string 是.NET 框架中的 string 类的 alias. 事实上,每个 C# 类型都有.NET 的同等值. 另一个小差异是,如果你使用 string 类,你需要进口系统名称空间,而你不需要在使用 string 关键词时进口名称空间。

在 C# 中,线和线之间没有重大区别。

String 是系统名称空间中的.NET 框架中的一个类,完全合格的名称是 System.String。

但是,它建议使用字符串,同时声明变量如:

string str = "Hello";

我们可以使用 String 同时使用任何内置方法为 String.IsNullOrEmpty(等线条。

此外,这两者之间的区别就像在使用 String 之前,我们必须在 CS 文件中输入系统名称空间,并且可以直接使用 String。

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

另一个没有提到的论点,最好是复活节案例:

System.String 是参考类型,参考类型名称是经典案例。

实际上没有区别

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