【发布时间】:2016-03-05 04:24:38
【问题描述】:
考虑以下代码:
static void Main(string[] args)
{
List<string> items = new List<string>();
string result = null;
if(items.All(o => o == "ABC"))
{
result = "All";
}
else if(items.Any(o => o == "XYZ"))
{
result = "Any";
}
Console.WriteLine(result);
Console.Read();
}
这会打印“全部”。
为什么 empty 列表满足 o == "ABC"
【问题讨论】:
-
看源码here
-
为什么是主观的,不是吗?杯子是半满还是半空?
All满足您的条件,因为没有。如果列表为空,您始终可以先检查。 -
我认为您需要以相反的方式考虑它。
.All(...)是true,只要不存在false。如果列表为空,则不存在false,所以它是true。