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

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

当前回答

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

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

其他回答

上述的一切基本上是正确的,一个人可以检查它。

public static void Main()
{
    var s = "a string";
}

编辑并打开.exe 与 ildasm 查看

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       8 (0x8)
  .maxstack  1
  .locals init ([0] string s)
  IL_0000:  nop
  IL_0001:  ldstr      "a string"
  IL_0006:  stloc.0
  IL_0007:  ret
} // end of method Program::Main

然后变成线条和线条,编译,打开与 ildasm 并看到 IL 不会改变. 它也显示语言的创作者在定义变量时更喜欢线条(spoiler:当呼叫会员时,他们更喜欢线条)。

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

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

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

string str = "Hello";

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

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

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

至少有四种差异:

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

2、你不能使用 String 而不“使用系统”。所以你用“string”写得更少。

3、“String”比“string”更好地命名公约,因为它是一种不变的类型。

“String”是大多数编辑器中突出的C#关键字和合成,但不是“String”。

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

字符串为变量

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

如:

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

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

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