string.empty和null之间的区别 [英] Difference between string.empty and null

查看:205
本文介绍了string.empty和null之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好....

谁能告诉我sting a = string.empty和string b =之间的区别是什么?

Hi Everyone....
Can anyone tell me what is difference between sting a=string.empty and string b="" ?

推荐答案

你的标题和文字包含两个不同的问题。



String.Empty 和<$从编程的角度来看,c $ c>是相同的。请参阅: http://msdn.microsoft.com/en-us/library/system。 string.empty.aspx [ ^ ]



字符串是一个对象,它在内存中保留了空间。变量本身包含对该地址空间的引用(排序)。如果引用(变量的自身值)是 null ,则表示它所引用的内容(字符串本身)不存在。它是未初始化的。虽然 string.Empty String.Empty 意味着字符串本身用空(零长度)值初始化。
Your title and the text contain two different questions.

String.Empty and "" are the same from programming point of view. See: http://msdn.microsoft.com/en-us/library/system.string.empty.aspx[^]

A string is an object, something that has reserved space in memory. The variable itself contains a reference to that address space (sort of). If a reference (the variable's very own value) is null, that means that the content it is referring to (the string itself) does not exist. It is uninitialized. While both string.Empty, String.Empty and "" mean that the string itself is initialized with an empty (zero-length) value.


与使用空文本写问题的区别相同(我希望CodeProject软件可以防止这种情况)虽然)并没有发布任何问题。请注意,第二个选项会更好:您可以通过阅读原始文档轻松了解此事。



请注意,问题不一致(请参阅我对问题和其他答案)。



null的问题很简单,但 String.Empty 并非如此简单。首先会像立即常量那样工作,第二个就是只读字段。它们产生相同的结果,但由于字符串的非常重要的特征,特别是:字符串实习。请参阅:

http://en.wikipedia.org/wiki/String_interning [< a href =http://en.wikipedia.org/wiki/String_interningtarget =_ blanktitle =New Window> ^ ],

http://broadcast.oreilly.com/2010/08/understanding-c-stringintern-m.html [ ^ ] 。



intern pool 中,我们可能最终在代码的不同部分有多个字符串。使用实习池,它不会发生。



实际上,使用 String.Empty 非常重要,不是一个直接的常数。立即常量使代码变脏,对维护不利。可以对0,1和null等立即常量进行排除,但绝不能。想要使用文本搜索从代码中删除所有硬编码字符串的映像。会是一个好主意。然后会进入你的搜索结果,所以最好在一开始就摆脱它们。



-SA
The difference is the same as writing the question with empty text (I hope this is prevented by CodeProject software though) and not posting any question at all. Note that the second option would be much better: you could easily learn this matter by reading original documentation.

Note that the question is inconsistent (please see my comment to the question and other answers).

The problem with null is trivial, but "" vs. String.Empty is not so simple. First would work like immediate constant and the second one is the read-only field. They make the same result, but due to a very non-trivial feature of strings, specifically: string interning. Please see:
http://en.wikipedia.org/wiki/String_interning[^],
http://broadcast.oreilly.com/2010/08/understanding-c-stringintern-m.html[^].

In not intern pool, we could possibly end up having multiple "" strings in different parts of code. With intern pool, it doesn't happen.

Practically, it's important to use String.Empty, not an immediate constant. Immediate constants make code dirty, are bad for maintenance. Exclusions could be made for immediate constants like 0, 1 and null, but never "". Imaging you want to remove all hard-coded strings from the code, using text search. Would be a good idea. And then "" will get in your search result, so it's better to get rid of them in the very beginning.

—SA


阅读以下内容: http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string [ ^ ]
Read the following : http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string[^]


这篇关于string.empty和null之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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