这是在 ASP.NET(C#) 中的字符串中使用的最佳实践

发布于 2024-08-07 14:03:56 字数 189 浏览 3 评论 0原文

我想知道在 C# 中声明字符串的首选方式是:

string strEmpty = String.Empty;

  或者

string strNull = null;

哪种做法更好或者这些陈述之间有什么区别。

I want to know which is the preferred way to declare a string in C#:

string strEmpty = String.Empty;

   or

string strNull = null;

Which practice is better or what is the difference between these statements.

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

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

发布评论

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

评论(5

影子是时光的心 2024-08-14 14:03:56

第一个答案使字符串的值成为实际的空字符串。将其分配为 null 会使指针指向任何内容。这意味着如果您尝试执行 strEmpty.Function(),则在第二种情况下它将不起作用。

第一个最初需要更多内存,但更清晰。

正确答案取决于您下一步要做什么。如果您只是要重新分配字符串,我会将其设为 null。如果您打算对字符串执行一些操作(执行函数、追加等),我会将其设置为string.empty

the first answer makes the value of the string an actual empty string. Assigning it to null makes the pointer point to nothing. This means that if you tried to do strEmpty.Function(), it wouldn't work in the second case.

The first takes more memory initially, but is more clear.

The correct answer depends on what you would do next. If you are just going to reassign the string, I would make it null. If you intend to do stuff to the string (execute functions, append, etc), I would make it string.empty.

半﹌身腐败 2024-08-14 14:03:56

区别在于语义

每当您在代码中发现 NULL 指针时,就意味着它确实什么都没有。这意味着它还没有被初始化为任何东西,所以你不能使用它。

另一方面,空字符串一个字符串。它已被初始化,任何查看代码的程序员都会将该字符串视为要使用的有效对象。

例如,考虑将相对 URL 存储在字符串中。如果您发现该字符串为空,您会认为它指向根路径。但如果它是 NULL,你应该认为它不是一个有效的变量 - 它没有被初始化,你不应该使用它。

基于此,您将采用不同的路径 - 空 URL 意味着与 NULL 变量非常不同的东西。

The difference is semantical.

Whenever you find in the code a NULL pointer, it means that is truly nothing. It means it has not been initialized to anything, so you can't use it.

On the other hand, an empty string is a string. It's been initialized and any programmer looking at the code will consider that string as a valid object to be used.

Consider for example having a relative URL stored in a string. If you find that string empty, you'll think it's pointing to the root path. But if it's NULL, you should consider it's not a valid variable whatsoever - it's not been initialized and you should not use it.

Based on that, you'll take different paths - an empty URL means something very different from a NULL variable.

白云不回头 2024-08-14 14:03:56

没有“最好”。你做什么取决于你的意思。

空字符串的长度为 0,但它是一个字符串。当您绝对必须返回某种字符串时,即使它的长度为零,也请执行此操作。这是很少见的。

null 首先不是一个字符串,它是一个空指针。当缺少字符串是可能影响应用程序其他部分的有意义的条件时,请执行此操作。

There is no "best". What you do depends on what you mean.

An empty string has a length of 0, but it is a string. Do this when you absolutely must return some kind of string, even if it's zero length. This is rare.

A null is not a string in the first place, it's a null pointer. Do this when the absence of a string is a meaningful condition that may influence other parts of the application.

夜还是长夜 2024-08-14 14:03:56

在典型的业务线应用程序中,语义差异非常明显:Null 表示该值未知或不适用,Empty 表示该值已知为空白。

在其他类型的应用程序中,语义或多或少取决于您,只要您记录它们并在整个团队中一致地应用它们即可。

性能上没有区别。

In a typical line-of-business app, the semantic difference is very clear: Null means the value is unknown or not applicable, and Empty means the value is known to be blank.

In other types of app, the semantics are more or less up to you, as long as you document them and apply them consistently across your whole team.

There is no difference in performance.

七分※倦醒 2024-08-14 14:03:56

null 和空字符串之间的区别确实是语义上的,但按照惯例它们在很大程度上是等效的。

在 .NET 中,System.String 类有一个静态方法 IsNullOrEmpty,在大多数情况下使用它是最佳实践。

The difference between null and empty string is indeed semantic but they are largely equivalent by convention.

In .NET the System.String class has a static method IsNullOrEmpty that it is best practice to use in most cases.

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