在C#中,我应该使用string.Empty还是String.Empty或“”。初始化字符串? [英] In C#, should I use string.Empty or String.Empty or "" to intitialize a string?

查看:325
本文介绍了在C#中,我应该使用string.Empty还是String.Empty或“”。初始化字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 = "";

还是什么?

推荐答案

使用您和您的团队认为最易读的内容。

其他答案建议创建一个新字符串每次使用 。这是不正确的-由于字符串内部操作,它会针对每个程序集创建一次,也可能针对每个AppDomain创建一次(或者对于整个过程可能创建一次-不确定在那方面)。这种差异可以忽略不计-很大程度上,无足轻重。

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.

容易被误认为彼此并没有真正被我洗净。除非您使用比例字体(并且我没有与任何开发人员合作),否则很容易分辨出差异。

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.

这篇关于在C#中,我应该使用string.Empty还是String.Empty或“”。初始化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆