我喜欢绳子。IsNullOrEmpty方法。我想有一些东西,将允许相同的功能IEnumerable。有这样的东西吗?也许是一些收集助手类?我问的原因是,在if语句中,如果模式是(mylist != null && mylist. any()),代码看起来很混乱。使用Foo.IsAny(myList)会干净得多。

这篇文章没有给出答案:IEnumerable是空的?


当前回答

从c# 6开始,你可以使用空传播:myList?.Any() == true

如果你仍然觉得这个方法太麻烦或者更喜欢一个好的扩展方法,我会推荐Matt Greer和Marc Gravell的答案,但为了完整性,还需要一些扩展功能。

他们的答案提供了相同的基本功能,但各从不同的角度。Matt的答案使用了字符串。isnullorempty心态,而Marc的答案采用了Linq的. any()方法来完成工作。

我个人倾向于使用.Any()方法,但希望从该方法的其他重载中添加条件检查功能:

    public static bool AnyNotNull<T>(this IEnumerable<T> source, Func<T, bool> predicate = null)
    {
        if (source == null) return false;
        return predicate == null
            ? source.Any()
            : source.Any(predicate);
    }

所以你仍然可以做这样的事情: myList.AnyNotNull(项= >项目。AnswerToLife == 42);就像使用常规的.Any()一样,但添加了空检查

注意,使用c# 6的方式:myList?.Any()返回bool?而不是bool类型,这是传播null的实际效果

其他回答

public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable) {
    return enumerable == null || !enumerable.Any();
}

我根据@Matt Greer的回答构建了这个

他完美地回答了那位警官的问题。

我想要这样的东西,同时保持Any的原始功能,同时还检查null。我张贴这个以防其他人需要类似的东西。

具体来说,我希望仍然能够传递一个谓词。

public static class Utilities
{
    /// <summary>
    /// Determines whether a sequence has a value and contains any elements.
    /// </summary>
    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
    /// <param name="source">The <see cref="System.Collections.Generic.IEnumerable"/> to check for emptiness.</param>
    /// <returns>true if the source sequence is not null and contains any elements; otherwise, false.</returns>
    public static bool AnyNotNull<TSource>(this IEnumerable<TSource> source)
    {
        return source?.Any() == true;
    }

    /// <summary>
    /// Determines whether a sequence has a value and any element of a sequence satisfies a condition.
    /// </summary>
    /// <typeparam name="TSource">The type of the elements of source.</typeparam>
    /// <param name="source">An <see cref="System.Collections.Generic.IEnumerable"/> whose elements to apply the predicate to.</param>
    /// <param name="predicate">A function to test each element for a condition.</param>
    /// <returns>true if the source sequence is not null and any elements in the source sequence pass the test in the specified predicate; otherwise, false.</returns>
    public static bool AnyNotNull<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
        return source?.Any(predicate) == true;
    }
}

扩展方法的命名可能会更好。

只需添加使用系统。Linq和看到魔术发生时,你试图访问可用的方法在IEnumerable。添加它将使您能够访问名为Count()的方法,就像这样简单。只要记住在调用count():)之前检查空值即可。

下面是@Matt Greer的有用答案的修改版本,它包括一个静态包装器类,这样你就可以将它复制粘贴到一个新的源文件中,不依赖于Linq,并添加了一个泛型IEnumerable<T>重载,以避免非泛型版本会发生的值类型装箱。[编辑:注意,使用IEnumerable<T>并不能阻止对枚举器进行装箱,鸭子类型不能阻止这一点,但至少值类型集合中的元素不会每个都被装箱。]

using System.Collections;
using System.Collections.Generic;

public static class IsNullOrEmptyExtension
{
    public static bool IsNullOrEmpty(this IEnumerable source)
    {
        if (source != null)
        {
            foreach (object obj in source)
            {
                return false;
            }
        }
        return true;
    }

    public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
    {
        if (source != null)
        {
            foreach (T obj in source)
            {
                return false;
            }
        }
        return true;
    }
}

从c# 6开始,你可以使用空传播:myList?.Any() == true

如果你仍然觉得这个方法太麻烦或者更喜欢一个好的扩展方法,我会推荐Matt Greer和Marc Gravell的答案,但为了完整性,还需要一些扩展功能。

他们的答案提供了相同的基本功能,但各从不同的角度。Matt的答案使用了字符串。isnullorempty心态,而Marc的答案采用了Linq的. any()方法来完成工作。

我个人倾向于使用.Any()方法,但希望从该方法的其他重载中添加条件检查功能:

    public static bool AnyNotNull<T>(this IEnumerable<T> source, Func<T, bool> predicate = null)
    {
        if (source == null) return false;
        return predicate == null
            ? source.Any()
            : source.Any(predicate);
    }

所以你仍然可以做这样的事情: myList.AnyNotNull(项= >项目。AnswerToLife == 42);就像使用常规的.Any()一样,但添加了空检查

注意,使用c# 6的方式:myList?.Any()返回bool?而不是bool类型,这是传播null的实际效果