且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查 IEnumerable 是 null 还是空?

更新时间:2023-11-29 15:51:46

当然你可以写:

public static class Utils {
    public static bool IsAny<T>(this IEnumerable<T> data) {
        return data != null && data.Any();
    }
}

但是,请注意并非所有序列都是可重复的;通常我更喜欢只走一次,以防万一.

however, be cautious that not all sequences are repeatable; generally I prefer to only walk them once, just in case.