为什么的String.Empty比&QUOT更高效;"在.net [英] Why is string.Empty more efficient than "" in .Net

查看:179
本文介绍了为什么的String.Empty比&QUOT更高效;"在.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 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.

这篇关于为什么的String.Empty比&QUOT更高效;"在.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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