我现在总是使用String.Empty而不是“"” [英] Shall I now always use String.Empty instead of ""

查看:49
本文介绍了我现在总是使用String.Empty而不是“"”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我总是使用"在我的代码中到处都是空字符串

没有问题。


现在,你认为我应该使用String.Empty而不是" (总共

次)?


让我知道你的想法。

解决方案
"一个****** @ hotmail.com"写道:


过去我总是使用"在我的代码中到处都是空字符串

没有问题。


现在,你认为我应该使用String.Empty而不是" (总共

次)?



如果你要检查空字符串,最好使用:

String.IsNullOrEmpty(strStringName)

从.NET 2.0开始





这两者之间差别不大。


""很容易写,易读,几乎不可能误解。

然而,它会创建一个String.Empty不会的对象,但是"

对象在你的应用程序的整个生命周期中被重用,因此可以忽略

的差异。


两者都不会检测到null字符串所以如果你可以有空字符串你需要

来检查它或者使用String.IsNullErEmtpy作为Leon

指出。

if(str == null || str =="")//或

if(String.IsNullOrEmpty(str))


另一方面,如果你知道你永远不会得到空字符串,那么测试

for String.Length == 0是最快的方法。


On Thu,2006年11月23日10:57:06 +0100,< an ****** @ hotmail.comwrote:


过去我总是使用在我的代码中到处都是空字符串

没有问题。


现在,你认为我应该使用String.Empty而不是" (总共

次)?


让我知道你的想法。




-

快乐编码!

Morten Wennevik [C#MVP]


< an *** ***@hotmail.com写的:


过去我总是使用"在我的代码中到处都是空字符串

没有问题。


现在,你认为我应该使用String.Empty而不是" (总共

次)?



您觉得哪个更容易阅读?这是任何重要的唯一

差异,IMO。


-

Jon Skeet - < sk***@pobox.com>
http://www.pobox。 com / ~siget 博客: http://www.msmvps.com/ jon.skeet

如果回复小组,请不要给我发邮件


In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?

Let me know your thoughts.

解决方案

"an******@hotmail.com" wrote:

In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?

If you''re checking for an empty string it''s better to use:
String.IsNullOrEmpty(strStringName)
As of .NET 2.0


Hi,

There is very little difference between the two.

"" is easy to write, easy to read, and near impossible to misunderstand.
It will however create an object where String.Empty would not, but the ""
object is reused throughout the lifespawn of your application so the
difference can therefore be ignored.

Neither will detect a null string so if you can have null strings you need
to either check for that as well or use String.IsNullErEmtpy as Leon
pointed out.

if(str == null || str == "") // or
if(String.IsNullOrEmpty(str))

If, on the other hand, you know you will never get null strings, testing
for String.Length == 0 is the fastest way.

On Thu, 23 Nov 2006 10:57:06 +0100, <an******@hotmail.comwrote:

In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?

Let me know your thoughts.



--
Happy Coding!
Morten Wennevik [C# MVP]


<an******@hotmail.comwrote:

In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?

Which do you find easier to read? That''s pretty much the only
difference of any significance, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于我现在总是使用String.Empty而不是“&quot;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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