繁体   English   中英

C# 是否有用于 List/IEnumerable 的 IsNullOrEmpty?

[英]Does C# have IsNullOrEmpty for List/IEnumerable?

我知道通常空列表比 NULL 更可取。 但我要返回 NULL,主要有两个原因

  1. 我必须明确地检查和处理空值,避免错误和攻击。
  2. 执行起来容易?? 之后进行操作以获得返回值。

对于字符串,我们有 IsNullOrEmpty。 C# 本身是否有任何东西对 List 或 IEnumerable 做同样的事情?

框架中没有任何内容,但它是一种非常直接的扩展方法。

看这里

/// <summary>
    /// Determines whether the collection is null or contains no elements.
    /// </summary>
    /// <typeparam name="T">The IEnumerable type.</typeparam>
    /// <param name="enumerable">The enumerable, which may be null or empty.</param>
    /// <returns>
    ///     <c>true</c> if the IEnumerable is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
    {
        if (enumerable == null)
        {
            return true;
        }
        /* If this is a list, use the Count property for efficiency. 
         * The Count property is O(1) while IEnumerable.Count() is O(N). */
        var collection = enumerable as ICollection<T>;
        if (collection != null)
        {
            return collection.Count < 1;
        }
        return !enumerable.Any(); 
    }

出于性能原因,Daniel Vaughan 采取了额外的步骤,即强制转换为 ICollection(在可能的情况下)。 我不会想到要做的事情。

后期更新:从 C# 6.0 开始,可以使用空传播运算符来表达简洁,如下所示:

if (  list?.Count  > 0 ) // For List<T>
if ( array?.Length > 0 ) // For Array<T>

或者,作为IEnumerable<T>的更清洁和更通用的替代方案:

if ( enumerable?.Any() ?? false )

注意 1:与 OP 问题(引用)相反,所有上层变体实际上都反映IsNotNullOrEmpty

由于运算符优先级, IsNullOrEmpty等效项看起来不那么吸引人:
if (!(list?.Count > 0))

注2: ?? false ?? false是必要的,因为以下原因( 此帖子的摘要/引用):

?. 如果子成员为null null 但是 [...] 如果我们尝试获取一个非Nullable成员,例如Any()方法,它返回bool [...] 编译器将在Nullable<>中“包装”一个返回值。 例如, Object?.Any()会给我们bool? (这是Nullable<bool> ),而不是bool [...] 因为它不能被隐式转换为bool ,所以这个表达式不能在if中使用

注 3:作为奖励,该语句也是“线程安全的”(引自 answer of this question ):

在多线程上下文中,如果 [ enumerable ] 可以从另一个线程访问(因为它是一个可访问的字段,或者因为它在暴露给另一个线程的 lambda 中被关闭),那么每次计算时值可能不同 [ ieprior null -检查]

没有内置任何东西。

这是一个简单的扩展方法:

public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
{
  if(enumerable == null)
    return true;

  return !enumerable.Any();
}
var nullOrEmpty = list == null || !list.Any();

将前面的答案放在 C# 6.0+ 的简单扩展方法中:

    public static bool IsNullOrEmpty<T>(this IEnumerable<T> me) => !me?.Any() ?? true;

正如其他人所说,框架中没有内置任何内容,但如果您使用的是 Castle,那么 Castle.Core.Internal 就有了。

using Castle.Core.Internal;

namespace PhoneNumbers
{
    public class PhoneNumberService : IPhoneNumberService
    {
        public void ConsolidateNumbers(Account accountRequest)
        {
            if (accountRequest.Addresses.IsNullOrEmpty()) // Addresses is List<T>
            {
                return;
            }
            ...

如果您需要能够在它不为空的情况下检索所有元素,那么这里的一些答案将不起作用,因为对不可回退的枚举的Any()调用将“忘记”一个元素。

您可以采取不同的方法并将空值变成空值:

bool didSomething = false;
foreach(var element in someEnumeration ?? Enumerable.Empty<MyType>())
{
  //some sensible thing to do on element...
  didSomething = true;
}
if(!didSomething)
{
  //handle the fact that it was null or empty (without caring which).
}

同样可以使用(someEnumeration ?? Enumerable.Empty<MyType>()).ToList()等。

我修改了 Matthew Vines 的建议以避免“IEnumerable 的可能多重枚举”问题。 (另见 Jon Hanna 的评论)

public static bool IsNullOrEmpty(this IEnumerable items)
    => items == null
    || (items as ICollection)?.Count == 0
    || !items.GetEnumerator().MoveNext();

...和单元测试:

[Test]
public void TestEnumerableEx()
{
    List<int> list = null;
    Assert.IsTrue(list.IsNullOrEmpty());

    list = new List<int>();
    Assert.IsTrue(list.IsNullOrEmpty());

    list.AddRange(new []{1, 2, 3});
    Assert.IsFalse(list.IsNullOrEmpty());

    var enumerator = list.GetEnumerator();
    for(var i = 1; i <= list.Count; i++)
    {
        Assert.IsFalse(list.IsNullOrEmpty());
        Assert.IsTrue(enumerator.MoveNext());
        Assert.AreEqual(i, enumerator.Current);
    }

    Assert.IsFalse(list.IsNullOrEmpty());
    Assert.IsFalse(enumerator.MoveNext());
}
var nullOrEmpty = !( list?.Count > 0 );

对我来说最好的 isNullOrEmpty 方法看起来像这样

public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
{
    return !enumerable?.Any() ?? true;
}
 public static class IEnumerableExtention
 {
     public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
     {
         if (enumerable == null)
         {
             return true;
         }
 
         using var enumerator = enumerable.GetEnumerator();
 
         var isEmpty = !enumerator.MoveNext();
 
         return isEmpty;
     }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM