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

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

当前回答

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

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:当呼叫会员时,他们更喜欢线条)。

其他回答

在 MSDN 文档的背景下, String 类像任何其他数据类型(例如 XmlReader、StreamReader)一样在 BCL 中文档。

字符串被记录为一个关键字(C#参考)或任何基本的C#语言构造(例如,为,同时,默认)。

参考。

StyleCop 规则,默认情况下,将允许您使用字符串作为一个 C# 代码风格的最佳实践,除非提到 System.String 静态功能,如 String.Format, String.Join, String.Concat 等。

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

String(System.String)是基类图书馆的一个类。 string(下一个案例)是C#的备份工作,这是System.String的标志。 Int32 vs int是一种类似的情况,如Boolean vs. bool. 这些C#语言特定的关键词允许您以类似于C的风格宣布原始性。

string 是 System.String 的 C# 中的一个 alias. 所以技术上,没有区别. 它就像 int vs. System.Int32.

至于指导方针,一般建议在您提到对象时使用字符串。

吉。

string place = "world";

同样,我认为一般建议使用 String 如果你需要具体提到课堂。

吉。

string greet = String.Format("Hello {0}!", place);

这是微软在其例子中使用的风格。

似乎该领域的指导方针可能已经改变了,因为StyleCop现在强制使用C#特定的联盟。