C#String.IsNullOrEmpty:好还是坏? [英] C# String.IsNullOrEmpty: good or bad?

查看:617
本文介绍了C#String.IsNullOrEmpty:好还是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事件在工作中,我滥用String.IsNullOrEmpty用Session变量之后,我的同事同事现在拒绝接受我String.IsNullOrEmpty的使用。经过一番研究,显然有一个为IsNullOrEmpty在MSDN(列出的错误链接)(底部读注):

After an incident at work where I misused String.IsNullOrEmpty with a Session variable, a fellow coworker of mine now refuses to accept my usage of String.IsNullOrEmpty. After some research, apparently there's a bug listed for IsNullOrEmpty on MSDN (link) (read note at the bottom):

截至2006年4月4日,有一个bug
(有可能在JIT),使得优化时
开启此
方法失败。众所周知,影响两个
C#和VB。

As of April 4, 2006, there is a bug (possible in the JIT) that makes this method fail when optimizations are turned on. It is known to affect both C# and VB.

更多信息可以在这里找到(的链接)。微软的错误是所谓固定后的逆戟鲸,但遗憾的是我的雇主仍然使用VS2005。但是,如果问题在2008+就这样吧。这只是我没意见。

More information can be found here (link). Microsoft the bug is 'supposedly' fixed post-Orcas, but unfortunately my employer still uses VS2005. But if the problem is fixed in 2008+ so be it. That's just fine with me.

虽然我的代码以IsNullOrEmpty我,我的同事的拒绝是盲目无知(IMO),他当然不能告诉我为什么的的使用它比使用会话变量中的误操作等。我用IsNullOrEmpty遍布我们没有任何问题的代码。就个人而言,我觉得除了在一条语句做两件事情更具可读性。

While my colleague's refusal of my code with IsNullOrEmpty to me is blind ignorance (IMO) he certainly can't tell me why not to use it other than the misuse with the session variable. I've used IsNullOrEmpty all over our code with no issues whatsoever. Personally, I find it much more readable in addition of doing two things in one statement.

对于谷歌搜索关于这个问题的意见后,我发现,把利弊/ CON立场的网站。这里有一些我读过有关这个网站:

After googling for opinions on the subject, I've found sites that take the pros/con stance. Here are some of the sites I've read about this:

http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/06/22/1063.aspx

http://www.omegacoder.com/?p=105

一个站点( http://dotnetperls.com/isnullorempty )总结了方法(恕我直言)相当不错:

One site (http://dotnetperls.com/isnullorempty) sums up the method (IMHO) pretty well:

下面我们来看对字符串类型IsNullOrEmpty
方法,
为我们提供检查
字符串是否确定保存或使用的一个很好的和相对
有效的方法。然而,
性能,它可能是更好的
使用说明书null检查。空字符串
也可以以其他的方式进行测试,和
我的研究这里表明检查
长度是最快的。

Here we looked that IsNullOrEmpty method on the string type, which provides us with a good and relatively efficient method of checking whether a string is OK to save or use. However, for performance, it may be better to use manual null checks. Empty strings can also be tested in other ways, and my research here shows that checking length is fastest.

假设bug修复到位(与正常工作)在VS2008 / 2010 /等,是没有任何理由的的与VS2005及以后使用String.IsNullOrEmpty?我知道这看起来可能有点矫枉过正了这样一个愚蠢的小方法,但我想知道如果有更多的幕后事情的后面,如果任何人有不同的解释。

Assuming the bug fix is in place (and working correctly) in VS2008/2010/etc., is there any reason not to use String.IsNullOrEmpty with VS2005 and beyond? I realize this may seem a little overkill over such a silly little method, but I'd like to know if there's more behind the scenes going on and if anyone has alternative explanations.

推荐答案

此问题已修复的.NET 2.0 SP1。 。没有理由,以避免它的使用,现在

This issue was fixed in .NET 2.0 sp1. There is no reason to avoid its use now.

如果您正在使用.NET 2,你应该有很多其他原因,反正SP1的 - 我认为没有理由以避免本作不再存在一个bug。

If you're using .NET 2, you should have sp1 for many other reasons anyways - I see no reason to avoid this for a bug that no longer exists.

这篇关于C#String.IsNullOrEmpty:好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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