为什么的String.Empty比&QUOT更高效;"在.net高效、Empty、String、net

由网友(遍体鳞伤才叫漂亮)分享简介:我肯定的String.Empty比网络语言使用,更有效。我想知道的是为什么会更有效率?I am certain that string.Empty is more efficient than using "" in .Net languages. What I would like to know is WHY i...

我肯定的String.Empty比网络语言使用,更有效。我想知道的是为什么会更有效率?

I am certain that string.Empty is more efficient than using "" in .Net languages. What I would like to know is WHY is it more efficient?

推荐答案

我认为在大多数情况下是没有区别的。在正常情况下,当你使用在code此字符串将被拘禁和相同的字符串实例将被重用。因此,当使用的String.Empty

I think in most cases there is no difference. In normal cases, when you use "" in your code this string will be interned and the same string instance will be reused. So there will not be more string instances around when using "" as compared to String.Empty.

如果你想证明:

Dim a As String
Dim b As String

a = ""
b = ""

Console.WriteLine(Object.ReferenceEquals(a, b)) ' Prints True '

以上code打印另外,如果你使用的String.Empty 更换其中的一个,所以你甚至可以混合的方法,而无需创建额外的字符串实例。

The above code prints True also if you replace one of them with String.Empty, so you can even mix the approaches without creating extra string instances.

底线:不同的是个人品味

Bottom line: the difference is personal taste.

阅读全文

相关推荐

最新文章