在 C# 中,我应该使用 string.Empty 还是 String.Empty 或 "" 来初始化字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/263191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C#, should I use string.Empty or String.Empty or "" to intitialize a string?
提问by Daniel Kreiseder
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?
或者是什么?
采纳答案by Jon Skeet
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). This difference is negligible - massively, massivelyinsignificant.
其他答案建议每次使用时都会创建一个新字符串""。这不是真的 - 由于字符串实习,它将在每个程序集或每个 AppDomain 中创建一次(或者可能在整个过程中创建一次 - 在这方面不确定)。这种差异可以忽略不计-大规模,大量微不足道。
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 anydevelopers who do) it's pretty easy to tell the difference.
""和" "很容易被误认为是对方的论点并没有真正让我接受。除非您使用的是比例字体(我还没有与任何这样做的开发人员合作过),否则很容易区分。
回答by MidnightGun
I personally prefer "" unless there is a good reason to something more complex.
我个人更喜欢 "" 除非有充分的理由来处理更复杂的事情。
回答by Quibblesome
Any of the above.
以上任何一项。
There are many, many better things to pontificate. Such as what colour bark suits a tree best, I think vague brown with tinges of dulcet moss.
有很多很多更好的事情需要宣扬。比如什么颜色的树皮最适合树,我认为是带有淡淡苔藓的模糊棕色。
回答by plinth
I use the third, but of the other two the first seems less odd. string is an alias for String, but seeing them across an assignment feels off.
我使用第三个,但在其他两个中,第一个似乎不那么奇怪。string 是 String 的别名,但是在作业中看到它们感觉很不舒服。
回答by Chris Marasti-Georg
The compiler should make them all the same in the long run. Pick a standard so that your code will be easy to read, and stick with it.
从长远来看,编译器应该使它们完全相同。选择一个标准,以便您的代码易于阅读,并坚持下去。
回答by Magus
I think the second is "proper," but to be honest I don't think it will matter. The compiler should be smart enough to compile any of those to the exact same bytecode. I use "" myself.
我认为第二个是“适当的”,但老实说,我认为这无关紧要。编译器应该足够聪明,可以将其中任何一个编译为完全相同的字节码。我自己用“”。
回答by Jimmy
I'd prefer stringto String. choosing string.Emptyover ""is a matter of choosing one and sticking with it. Advantage of using string.Emptyis it is very obvious what you mean, and you don't accidentally copy over non-printable characters like "\x003"in your "".
我宁愿string到String。选择string.Empty了""是选择一个,并与它坚持的问题。使用的优势string.Empty是非常明显你的意思,你不小心在非打印字符复制像"\x003"你""。
回答by Jimmy
I doesn't make a difference. The last one is the quickest to type though :)
我没有任何区别。最后一个是最快的输入:)
回答by Silviu Niculita
String.Emptyand string.Emptyare equivalent. Stringis the BCL class name; stringis its C# alias (or shortcut, if you will). Same as with Int32and int. See the docsfor more examples.
String.Empty并且string.Empty是等价的。String是 BCL 类名;string是它的 C# 别名(或快捷方式,如果你愿意的话)。与Int32和相同int。有关更多示例,请参阅文档。
As far as ""is concerned, I'm not really sure.
就这""方面而言,我不太确定。
Personally, I always use string.Empty.
就个人而言,我总是使用string.Empty.
回答by tvanfosson
Either of the first two would be acceptable to me. I would avoid the last one because it is relatively easy to introduce a bug by putting a space between the quotes. This particular bug would be difficult to find by observation. Assuming no typos, all are semantically equivalent.
前两者中的任何一个对我来说都是可以接受的。我会避免使用最后一个,因为通过在引号之间放置一个空格来引入错误相对容易。通过观察很难发现这个特定的错误。假设没有错别字,所有在语义上都是等价的。
[EDIT]
[编辑]
Also, you might want to always use either stringor Stringfor consistency, but that's just me.
此外,您可能希望始终使用string或String以保持一致性,但这只是我。
回答by zendar
stringis synonym for System.Stringtype, They are identical.
string是System.String类型的同义词,它们是相同的。
Values are also identical: string.Empty == String.Empty == ""
值也相同: string.Empty == String.Empty == ""
I would not use character constant "" in code, rather string.Emptyor String.Empty- easier to see what programmer meant.
我不会用字符常量“”中的代码,而string.Empty或String.Empty-更容易看到什么意思程序员。
Between stringand StringI like lower case stringmore just because I used to work with Delphi for lot of years and Delphi style is lowercase string.
之间string,String我string更喜欢小写,因为我曾经使用 Delphi 工作了很多年,而且 Delphi 风格是小写的string。
So, if I was your boss, you would be writing string.Empty
所以,如果我是你的老板,你会写 string.Empty

