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

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

当前回答

有一个区别 - 您不能使用 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# 连接地图到.NET 类型的 System.String - 这是一个标签,保持到语言的名称公约。

这个YouTube视频实际上展示了它们的差异。

当我们谈论.NET 时,有两个不同的东西,一个是.NET 框架,另一个是使用该框架的语言(C#,VB.NET 等)。

此分類上一篇

String s = "I am String";

string s = "I am String";

同样,还有其他 C# 数据类型的联盟,如下所示:

现在,从程序员的观点来看,百万美元的问题:那么什么时候使用“String”和“String”?

在下面的代码中,左侧是变量声明,它被声明使用“丝带”。在右侧,我们称一种方法,所以“丝带”更敏感。

string s = String.ToUpper() ;

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

使用系统类型使您更容易在 C# 和 VB.Net 之间端口,如果您进入这种类型的东西。