C# String.Empty 和 "" 和 null 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1028170/
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
What is the difference between String.Empty and “” and null?
提问by Joan Venge
Possible Duplicate:
What is the difference between String.Empty and “”
可能的重复:
String.Empty 和“”之间有什么区别
Is ""
equivalent to String.Empty
?
是""
相当于String.Empty
?
Which is preferred for initializing string values?
哪个更适合初始化字符串值?
采纳答案by yfeldblum
public sealed class String {
//...
public static readonly String Empty = "";
//...
}
Use null
when you want to represent that there is no value;
使用null
时,你要代表没有价值;
Use String.Empty
when you want to represent that there is a value, but the value is a blank string.
使用String.Empty
时要表示不存在是一种价值,但价值是一个空字符串。
回答by CodeMonkey1313
String.Empty because it is a static variable, rather than "" which has to create a new string, and null means that you must then set the string equal to a new instance of a string.
String.Empty 因为它是一个静态变量,而不是必须创建新字符串的 "",而 null 意味着您必须将字符串设置为等于字符串的新实例。
(thanks for the correction)
(感谢指正)
回答by Ed James
It is considered better practise to use string.Empty, however they are effectively equal. They are not the same as null, however.
使用 string.Empty 被认为是更好的做法,但它们实际上是相等的。但是,它们与 null 不同。
回答by Matthew Flaschen
Yes. And String.Empty, but please don't worry about it.
是的。和 String.Empty,但请不要担心。
The difference between String.Empty and “” are pretty small, but there is a difference. “” actually creates an object, it will likely be pulled out of the string intern pool and String.Empty creates no object, so if you are really looking for ultimate memory efficiency, I suggest String.Empty. However, you should keep in mind the difference is so trival you will like never see it in your code…
String.Empty 和 "" 之间的区别很小,但还是有区别的。“”实际上创建了一个对象,它很可能会被从字符串实习池中拉出来,而String.Empty不会创建任何对象,所以如果你真的在寻找最终的内存效率,我建议String.Empty。但是,您应该记住,差异是如此微不足道,您可能永远不会在代码中看到它……
回答by Johnno Nolan
FROM http://msdn.microsoft.com/en-us/library/system.string.empty.aspx
来自 http://msdn.microsoft.com/en-us/library/system.string.empty.aspx
The value of this field is the zero-length string, "".
该字段的值是零长度字符串“”。
In application code, this field is most commonly used in assignments to initialize a string variable to an empty string. To test whether the value of a string is String..::.Empty, use the IsNullOrEmpty method.
在应用程序代码中,此字段最常用于赋值以将字符串变量初始化为空字符串。要测试字符串的值是否为 String..::.Empty,请使用 IsNullOrEmpty 方法。
回答by joe
They are equal . But String.Empty is constant .
他们是平等的。但 String.Empty 是常量。
"" - is way of creating String .
"" - 是创建 String 的方式。
回答by Jon Skeet
Use whichever you find most readable.
使用您认为最易读的任何一个。
I challenge anyone to find a realisticapplication where there's a significant performance difference... it just won't happen. However, different people find different approaches more readable.
我挑战任何人找到一个现实的应用程序,其中存在显着的性能差异......它不会发生。然而,不同的人发现不同的方法更具可读性。
Personally, I'm a ""
person. I find it less cluttered, and I've never encountered a problem where I actuallyused " "
(or something similar) accidentally. (That's one of the objections frequently raised.)
就个人而言,我是一个""
人。我觉得它更简洁,而且我从来没有遇到过,我的问题其实使用" "
(或类似的东西)意外。(这是经常提出的反对意见之一。)
If you prefer string.Empty
, I'm certainly not going to claim you're "wrong". I would suggest, however, that if you're working on a team you discuss it to find out what most people think is more readable, and stick to that. Consistency is generally a good thing.
如果您愿意string.Empty
,我当然不会声称您“错了”。然而,我建议,如果你在一个团队中工作,你可以讨论它以找出大多数人认为更具可读性的内容,并坚持下去。一致性通常是一件好事。
EDIT: Just to allay some fears which might be induced by the claim that "" will create a new string... it maycreate a new string once(possibly per AppDomain
, possibly per process; I'm not sure and it reallydoesn't matter). It won'tcreate a new string every time you use it. For example, consider this code:
编辑:只是为了消除一些可能由“”将创建一个新字符串的说法引起的恐惧......它可能会创建一个新字符串一次(可能是每个AppDomain
,可能是每个进程;我不确定,它确实没有)没关系)。它不会创建一个新的字符串每次使用它的时候。例如,考虑以下代码:
public void Foo()
{
string x = "";
for (int i=0; i < 10000000; i++)
{
string y = "";
// Do something here
}
string z = "";
}
You are guaranteed that x
, y
and z
will refer to the same string. At most, invoking Foo
will mean a single empty string is created, and only the first time you call it, and only if you haven't used an empty string literal anywhere else in the program yet. The code above is notgoing to create 10 million new strings.
这样保证了x
,y
而且z
将引用相同的字符串。最多,调用Foo
将意味着创建一个空字符串,并且仅在您第一次调用它时,并且仅当您尚未在程序中的其他任何地方使用空字符串文字时。上面的代码不会创建 1000 万个新字符串。
回答by David Basarab
The answer you are looking for is here.
您正在寻找的答案在这里。
What is the difference between String.Empty and "" (empty string)?
use String.Empty
使用 String.Empty