When you are @ the linq expression, you may wonder why it is not offered to you to do the empty() method which can tell if a collection is empty?
well, Surely that Microsoft has someway to allow you to achieve it.
the key lies in the Any()'s void argument variant.
Here is the code.
class Program
{
private static void Main(string[] args)
{
IEnumerable<string> strings = new List<string>()
{
"A",
"B",
"C",
};
if (!strings.Any())
{
Console.WriteLine("IEnumerable is empty!");
}
else
{
Console.WriteLine("IEnumerale is not empty!");
}
IEnumerable<string> strings2 = new List<string>();
if (!strings2.Any())
{
Console.WriteLine("IEnumerable is empty!");
}
else
{
Console.WriteLine("IEnumerable is not empty");
}
}
}
So you'd better stop to use the slow/stupid Count() > 0 predicate to find if an Collection is empty or not..

被折叠的 条评论
为什么被折叠?



