在C#中,我应该使用的String.Empty或的String.Empty或QUOT;"?String、Empty、QUOT

2023-09-02 01:16:28 作者:给生活一份感激

在C#中,我要初始化字符串值与空字符串。

In C#, I want to initialize a string value with an empty string.

我应该怎么办呢? 什么是正确的方式,为什么?

How should I do this? What is the right way, and why?

string willi = string.Empty;

string willi = String.Empty;

string willi = "";

还是什么?

推荐答案

使用任何你和你的团队找到最可读的。

其他答案表明,一个新的字符串被创建每次使用的时间。这是不正确的 - 由于字符串实习,它将要么每个装配或每个应用程序域( - 不知道在这一方面或可能一次全过程)创建一次。这种差异可以忽略不计 - 大量,大型的显着

Other answers have suggested that a new string is created every time you use "". This is not true - due to string interning, it will be created either once per assembly or once per AppDomain (or possibly once for the whole process - not sure on that front). This difference is negligible - massively, massively insignificant.

,你觉得更可读则另当别论,但是。这是主观的,会有所不同,从人到人 - 所以我建议你找出大多数人对你的团队一样,和所有去与一致性。我个人觉得更容易阅读。

Which you find more readable is a different matter, however. It's subjective and will vary from person to person - so I suggest you find out what most people on your team like, and all go with that for consistency. Personally I find "" easier to read.

的说法很容易误认为对方并没有真正跟我洗。除非你使用的是比例字体(我还没有合作过的任意的谁做开发商)是pretty的容易分辨出来。

The argument that "" and " " are easily mistaken for each other doesn't really wash with me. Unless you're using a proportional font (and I haven't worked with any developers who do) it's pretty easy to tell the difference.