Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

c# - 在C#中,我应该使用string.Empty还是String.Empty或“”来初始化字符串?(In C#, should I use string.Empty or String.Empty or “” to intitialize a string?)

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

(在C#中,我想用一个空字符串初始化一个字符串值。)

How should I do this?

(我应该怎么做?)

What is the right way, and why?

(正确的方法是什么,为什么?)

string willi = string.Empty;

or

(要么)

string willi = String.Empty;

or

(要么)

string willi = "";

or what?

(或者是什么?)

  ask by Daniel Kreiseder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use whatever you and your team find the most readable.

(使用您和您的团队最容易阅读的内容。)

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).

(这是不正确的-由于字符串内部操作,它会针对每个程序集创建一次,也可能针对每个AppDomain创建一次(或者对于整个过程可能创建一次-不确定在那方面)。)

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.

(除非您使用比例字体(并且我没有与任何开发者合作过),否则很容易分辨出区别。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...