String.Empty 和“"有什么区别?(空字符串)? [英] What is the difference between String.Empty and "" (empty string)?

查看:34
本文介绍了String.Empty 和“"有什么区别?(空字符串)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET 中,String.Empty"" 之间有什么区别,它们是否可以互换,或者是否存在一些有关平等的潜在参考或本地化问题String.Empty 能保证没有问题吗?

In .NET, what is the difference between String.Empty and "", and are they interchangable, or is there some underlying reference or Localization issues around equality that String.Empty will ensure are not a problem?

推荐答案

在 .NET 2.0 之前的版本中,"" 创建一个对象,而 string.Empty 创建没有objectref,这使得 string.Empty 更高效.

In .NET prior to version 2.0, "" creates an object while string.Empty creates no objectref, which makes string.Empty more efficient.

在 .NET 2.0 及更高版本中,所有出现的 "" 引用相同的字符串文字,这意味着 "" 等价于 .Empty,但仍然没有 .Length == 0 快.

In version 2.0 and later of .NET, all occurrences of "" refer to the same string literal, which means "" is equivalent to .Empty, but still not as fast as .Length == 0.

.Length == 0 是最快的选项,但 .Empty 使代码更简洁.

.Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.

有关详细信息,请参阅 .NET 规范.

See the .NET specification for more information.

这篇关于String.Empty 和“"有什么区别?(空字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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