Why does Enumerable.All return true for an empty sequence?
It's certainly not a bug. It's behaving exactly as documented:
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
Now you can argue about whether or not it should work that way (it seems fine to me; every element of the sequence conforms to the predicate) but the very first thing to check before you ask whether something is a bug, is the documentation. (It's the first thing to check as soon as a method behaves in a way other than what you expected.)
LINQ Enumerable.All always return True if collection is empty
That's what the Documentation says.
true if every element of the source sequence passes the test in the
specified predicate, or if the sequence is empty; otherwise, false.
Implementation:
public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
foreach (TSource element in source) {
if (!predicate(element)) return false;
}
return true;
}
So it's obvious, if there is no item in the IEnumerable
the foreach
loop will be skipped and it directly returns true
.
Linq .All returning true for empty collection
According to MSDN:-
Enumerable.All
Return true if every element of the source sequence passes the
test in the specified predicate, or if the sequence is empty;
otherwise, false.
So in your case since items
is an empty collection it is returning true.
C# Linq All & Any working differently on blank array
Seems logical to me.
All
: Are all numbers inarr
greater than zero (meaning there is no number not greater than zero) =>true
Any
: Is there any number inarr
that is greater than zero =>false
But more important, according to Boolean Algebra:
arr.All(n => n > 0);
gives true
, because it should be the logical opposite of
arr.Any(n => !(n > 0));
which gives false
(actually this is what the above two points say).
Why does IQueryable.All() return true on an empty collection?
If my driveway is empty, I cannot assert that all cars parked there are red.
Consider the following statements.
S1
: My driveway is empty.
S2
: All the cars parked in my driveway are red.
I claim that S1
implies S2
. That is, the statement S1 => S2
is true. I will do this by showing that its negation is false. In this case, the negation of S1 => S2
is S1 ^ ~S2
; this is because S1 => S2
is false only when S1
is true and S2
is false. What is the negation of S2
? It is
~S2
: There exists a car parked in my driveway that is not red.
What is the truth value of S1 ^ ~S2
? Let's write it out
S1 ^ ~S2
: My driveway is empty and there exists a car parked in my driveway that is not red.
The only way that S1 ^ ~S2
can be true is if both S1
and ~S2
are true. But S1
says that my driveway is empty and S2
says that there exists a car in my driveway. My driveway can not be both empty and contain a car. Thus, it is impossible for S1
and ~S2
to both be true. Therefore, S1 ^ ~S2
is false so its negation S1 => S2
is true.
Therefore, if your driveway is empty you can assert that all cars parked there are red.
So now let's consider an IEnumerable<T> elements
and a Predicate<T> p
. Let us suppose that elements
is empty. We wish to discover the value of
bool b = elements.All(x => p(x));
Let's consider its negation
bool notb = elements.Any(x => !p(x));
For notb
to be true, there must be at least one x
in elements
for which !p(x)
is true. But elements
is empty so it is impossible to find an x
for which !p(x)
is true. Therefore notb
can not be true so it must be false. Since notb
is false, its negation is true. Therefore b
is true and elements.All(x => p(x))
must be true if elements
is empty.
Here's one more way to think of this. The predicate p
is true if for all x
in elements
you can not find any for which it is false. But if there are no items in elements
then it is impossible to find any for which it is false. Thus, for an empty collection elements
, p
is true for all x
in elements
Now, what about elements.Any(x => p(x))
when elements
is an empty IEnumerable<T>
and p
is a Predicate<T>
as above? We already know the result will be false because we know its negation is true, but let's reason through it anyway; the intuition is valuable. For elements.Any(x => p(x))
to be true there must be at least one x
in elements
for which p(x)
is true. But if there aren't any x
in elements
it is impossible to find any x
for which p(x)
is true. Therefore, elements.Any(x => p(x))
is false if elements
is empty.
Finally, here's a related explanation on why s.StartsWith(String.Empty)
is true when s
is a non-null instance of string
:
Why does IEnumerable.Any return True for a collection of False-booleans?
Any()
doesn't do what you think it does. Without a lambda expression in Any()
, it will just check if there is any element in the enumerable it is called on.
You either want:
types.Select((x, i) => values[i].GetType() != x).Any(x => x)
or maybe
types.Where((x, i) => values[i].GetType() != x).Any()
What the value of x within lambda in `Enumerable.All` when the List is empty?
The reason why you get true
is provided by the documentation:
true
if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise,false
.
This makes sense logically, too, because All
is supposed to mean the same thing as !Any(...)
. Since Any
would evaluate to false
for an empty collection, it follows that All
should evaluate to true
under the same circumstances.
why this condition returns true?
Enumerable.All
true if every element of the source sequence passes the test in the
specified predicate, or if the sequence is empty; otherwise, false.
Related Topics
Finding All References to a Method with Roslyn
Determine What Control the Contextmenustrip Was Used On
How to Unload an Assembly from the Primary Appdomain
How to Set Timer Resolution from C# to 1 Ms
What Is the Worst Gotcha in C# or .Net
Why Are Private Fields Private to the Type, Not the Instance
Should C# Methods That *Can* Be Static Be Static
Is String.Format as Efficient as Stringbuilder
Raise Event Thread Safely - Best Practice
Async Process Start and Wait for It to Finish
How to Restart a Wpf Application
Select Parsed Int, If String Was Parseable to Int
How to Implement a Custom Razorviewengine to Find Views in Non-Standard Locations
The Maximum Number of Characters a Textbox Can Display
C# "Internal" Access Modifier When Doing Unit Testing