重构:替换为 Constant String.Empty

发布于 2024-12-10 08:45:30 字数 194 浏览 0 评论 0原文

我有以下代码行

Dim a As String = ""

,重构建议将 "" 替换为 String.Empty

这是一种重构,但为什么呢?由于我是我未来声明的新手,我应该更好地使用 String.Empty 吗?

I have the following line of code

Dim a As String = ""

and the Refactor suggests replacing the "" with String.Empty

It is a kind of Reafactoring but why? Since i am a newbie in the future declarations of mine should i better use the String.Empty ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生生漫 2024-12-17 08:45:30

这实际上主要是一个偏好问题。就我个人而言,我发现 String.Empty 更清楚地显示了程序员的意图(例如,他没有意外地放置空字符串而不是空格)......这可能就是“Refactor”建议它的原因。但除此之外,它们是完全相同的。按照您认为合适的方式进行此操作。

It's mostly a matter of preference really. Personally, I find String.Empty more clearly shows the programmer's intent (for example, that he didn't accidentally put an empty string instead of a space)... that's probably why "Refactor" suggests it. But other than that, they're exactly the same thing. Do as you see fit on this one.

爱本泡沫多脆弱 2024-12-17 08:45:30

String.Empty 字段的值为零- 长度字符串,“”。

在应用程序代码中,此字段最常用于将字符串变量初始化为空字符串的赋值。要测试字符串的值是否为 null 或 String.Empty,请使用 IsNullOrEmpty 方法。

The value of the String.Empty 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 either null or String.Empty, use the IsNullOrEmpty method.

勿忘初心 2024-12-17 08:45:30

这很大程度上取决于个人喜好。我更喜欢尽可能使用常量,特别是如果我正在使用的框架已经提供了常量。我不会竭尽全力消除代码中所有“”的使用。 (特别是因为编译器将折叠字符串文字的所有重复项,并仅在运行时保留其中之一。)

It's pretty much a matter of personal preference. I prefer using constants whenever possible, especially so if they are already provided by the framework I'm using. I wouldn't go out of my way to eliminate every use of "" in the code. (Especially since the compiler will fold all repetitions of a string literal and keep only one of them for runtime.)

揽月 2024-12-17 08:45:30

"" 可能会创建一个新的 String 对象,而 String.Empty 则引用预定义的(现有的)对象。这是一个小问题,但这就是重构工具的作用:找到各种问题,包括小问题。

"" potentially creates a new String object, while String.Empty refers to a predefined (existing) object. It's a minor nitpick, but that's what refatoring tools do: find all kinds of issues, inluding minor nitpicks.

转身泪倾城 2024-12-17 08:45:30

我总是采取相反的做法,将字符串设置为 null。实际上在某些情况下,您想要使用“”来表示您已经构建了一个字符串但没有内容。通常你的意思是你没有构建字符串。这就是为什么我更喜欢 null 构造。

I always go the opposite and set the string to null. There are really a few situations where want you want to mean with "" is that you have a string built but with no content. Usually what you mean is that you have the string unbuilt. That's why I prefer the null construct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文