Safely checking non-repeatable IEnumerables for emptiness

前端 未结 3 1606
既然无缘
既然无缘 2021-01-02 00:26

There are times when it\'s helpful to check a non-repeatable IEnumerable to see whether or not it\'s empty. LINQ\'s Any doesn\'t work well

3条回答
  •  轮回少年
    2021-01-02 00:57

    This is not an efficient solution if the enumeration is long, however it is an easy solution:

    var list = input.ToList();
    if (list.Count != 0) {
        foreach (var item in list) {
           ...
        }
    }
    

提交回复
热议问题