C# 有 List/IEnumerable IsNullOrEmpty 吗?
我知道通常空列表比 NULL 更受欢迎。但我将返回 NULL,主要有两个原因,
- 我必须显式检查和处理空值,避免错误和攻击。
- 之后很容易执行
??
操作来获取返回值。
对于字符串,我们有 IsNullOrEmpty。 C# 本身是否有任何东西可以为 List 或 IEnumerable 做同样的事情?
I know generally empty List is more prefer than NULL. But I am going to return NULL, for mainly two reasons
- I have to check and handle null values explicitly, avoiding bugs and attacks.
- It is easy to perform
??
operation afterwards to get a return value.
For strings, we have IsNullOrEmpty. Is there anything from C# itself doing the same thing for List or IEnumerable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
框架中没有任何内容,但它是一种非常简单的扩展方法。
参见此处
Daniel Vaughan 采取了额外的步骤:出于性能原因,强制转换为 ICollection(如果可能)。有些事是我没想过要做的。
nothing baked into the framework, but it's a pretty straight forward extension method.
See here
Daniel Vaughan takes the extra step of casting to ICollection (where possible) for performance reasons. Something I would not have thought to do.
最新更新:从 C# 6.0 开始,空传播运算符可用于表达简洁,如下所示:
或者,作为
IEnumerable
IEnumerable
的更清晰、更通用的替代方案。 T>
:注1:所有上面的变体实际上反映了
IsNotNullOrEmpty
,与OP问题(引用):注释 2:
?? false
是必要的,因为以下原因(来自 这篇文章):注释 3: 作为奖励,该语句也是“线程安全的”(引用 这个问题):
Late update: since C# 6.0, the null-propagation operator may be used to express concise like this:
or, as a cleaner and more generic alternative for
IEnumerable<T>
:Note 1: all upper variants reflect actually
IsNotNullOrEmpty
, in contrast to OP question (quote):Note 2:
?? false
is necessary, because of the following reason (summary/quote from this post):Note 3: as a bonus, the statement is also "thread-safe" (quote from answer of this question):
没有内置任何内容。
不过它是一个简单的扩展方法:
There is nothing built in.
It is a simple extension method though:
将前面的答案汇总到 C# 6.0+ 的简单扩展方法中:
Putting together the previous answers into a simple extension method for C# 6.0+:
如果您需要能够在不为空的情况下检索所有元素,那么此处的某些答案将不起作用,因为在非对象上调用
Any()
可回滚枚举将“忘记”一个元素。您可以采取不同的方法,将空值转换为空值:
同样可以使用
(someEnumeration ?? Enumerable.Empty()).ToList()
等。If you need to be able to retrieve all of the elements in the case of it not being empty, then some of the answers here won't work, because the call to
Any()
on a non-rewindable enumerable will "forget" an element.You could take a different approach and turn nulls into empties:
Likewise
(someEnumeration ?? Enumerable.Empty<MyType>()).ToList()
etc. can be used.正如其他人所说,框架中没有内置任何内容,但如果您使用 Castle,那么 Castle.Core.Internal 就拥有它。
As everyone else has said, nothing is built into the framework, but if you are using Castle then Castle.Core.Internal has it.
我今天注意到 .Net Core 7.6.2 在 Microsoft.Identitymodel.Tokens.CollectionUtilities。
恕我直言,这似乎是一个非常奇怪的地方,但是如果您正在使用该软件包,那么它就可用......
I noticed today that .Net Core 7.6.2 has the extension
CollectionUtilities.IsNullOrEmpty<T>(IEnumerable<T>) Method
in Microsoft.Identitymodel.Tokens.CollectionUtilities.This seems a very odd place to keep it IMHO, but if you're using that package then it is available...
我修改了 Matthew Vines 的建议,以避免“IEnumerable 的可能多重枚举”问题。
(另请参阅 Jon Hanna 的评论)
...以及单元测试:
I modified the suggestion from Matthew Vines to avoid the "Possible multiple enumeration of IEnumerable" - problem.
(see also the comment from Jon Hanna)
... and the unit test:
对我来说最好的 isNullOrEmpty 方法看起来像这样
for me best isNullOrEmpty method is looked like this
具有可为空支持的一行:
One-line with nullable support: