对 null IEnumerables 进行 Count() 返回零
我厌倦了使用这样的代码:
var count = 0;
if (myEnumerable != null)
{
count = myEnumerable.Count();
}
这有点迂腐:
var count = (myEnumerable ?? new string[0]).Count();
有没有更简洁的方法来做到这一点?我曾经在 IEnumerable<> 上有一个(名字很糟糕的)PhantomCount 扩展方法。它使用了我的第一个代码示例,但它有一些味道(除了名称之外)。
I'm getting tired of using code like this:
var count = 0;
if (myEnumerable != null)
{
count = myEnumerable.Count();
}
And this is a bit pedantic:
var count = (myEnumerable ?? new string[0]).Count();
Is there any tidier way of doing this? I once had a (badly named) PhantomCount extension method on IEnumerable<> that used my first code example, but it had something of a smell about it (besides the name).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
问题实际上在于创建这些可枚举的内容。除非您有充分的理由,否则任何生成可迭代集合的内容都应返回空集合,而不是
null
。这将与 Null-Object-Pattern 保持一致,因此好处是相同的。我的建议是修复生成
myEnumerable
的任何内容,或者如果您无法执行此操作,请提前添加一个检查方式以查看它是否为 null 并做出适当的反应。The problem is really in whatever is creating these enumerables. Unless you have a really good reason, anything that generates an iterable collection should return an empty collection instead of
null
. This would align with the Null-Object-Pattern, hence the benefits are the same.My suggestion would be to fix whatever produces
myEnumerable
, or if you can't do this, add a check way earlier to see if it's null and react appropriately.怎么样
How about
我不认为使用扩展方法是一个坏主意。
I don't think using extension method is a bad idea.
2019 年,最简洁的方法是 var count = myEnumerable?.Count() ?? 0;。
2021年编辑:
In 2019 the tidiest way to do this is
var count = myEnumerable?.Count() ?? 0;
.2021 edit:
我使用自定义扩展方法:
I use a custom extension method:
我还会编写自己的扩展方法
CountOrZeroForNull
,如其他答案所示。此外...而不是:
您可以写:
这并不能缓解您的特定问题,但它可以避免分配未使用的数组。 (
Enumerable.Empty
最有可能实现为简单的yield break
语句。)I would also write my own extension method
CountOrZeroForNull
, as shown in other answers.Besides... Instead of:
you could write:
This doesn't alleviate your specific problem, but it circumvents allocation of an unused array. (
Enumerable.Empty<T>
is most likely implemented as a simpleyield break
statement.)只需创建您自己的扩展方法来根据您的意愿处理空枚举。
然后您可以简单地使用:
这就是扩展方法的伟大之处。即使(您似乎)调用该方法的对象为
null
,它们仍然可以正常工作。Just create your own extension method that handles null enumerables as you wish.
You can then simply use:
That's the great thing about extension methods. They still work fine even if the object on which (you appear to be) calling the method is
null
.如果返回值为 0,您将采取什么措施?
如果这很有趣,也许您应该对
IEnumerable
使用 Haack 的IsNullOrEmpty
扩展方法,如下所示:链接为 http://haacked.com/archive/2010/06/10/checking-for-empty-enumerations.aspx
作为博客上的评论发布,您还会发现我为此编写的一个
Exception
类:What actions are you taking if the value returned is 0?
If that's what's interesting, maybe you should use Haack's
IsNullOrEmpty
extension method forIEnumerable
like so:The link is http://haacked.com/archive/2010/06/10/checking-for-empty-enumerations.aspx
Posted as a comment on the blog, you'll also find an
Exception
class I wrote to go with that:虽然它不像其他答案那么技术性,但它是最具可读性的。
While it's not as technical as the other answers, it's far the most readable.