Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LINQ All returns True if the size of the collection is 0. #110433

Closed
EvgenyMarchuk opened this issue Dec 5, 2024 · 6 comments
Closed

LINQ All returns True if the size of the collection is 0. #110433

EvgenyMarchuk opened this issue Dec 5, 2024 · 6 comments

Comments

@EvgenyMarchuk
Copy link

Description

I'm getting true for the zero size collection, using LINQ All to check that all items have a value

Reproduction Steps

var list = new List<string>();
var isTrue = list.All(x => x == "a");
Console.WriteLine(isTrue);

Expected behavior

False

Actual behavior

True

Regression?

No response

Known Workarounds

No response

Configuration

.net8.0.404
Win10, x64

Other information

If I'm not mistaken, the same is in .net9

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 5, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.

@EvgenyMarchuk EvgenyMarchuk changed the title LINQ All return True if the cooliction size is 0 LINQ All returns True if the size of the collection is 0. Dec 5, 2024
@stephentoub
Copy link
Member

stephentoub commented Dec 5, 2024

Thanks, but this is by design. Every item meets the criteria. There just aren't any items.

Per the docs:
"true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false."

@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale Dec 5, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 5, 2024
@maikschott
Copy link

@EvgenyMarchuk If you don’t want that behavior, you can use list.DefaultIfEmpty().All(x => x == "a"); which turns an empty enumeration to a non-empty enumeration filled with one element of value default(T).

@EvgenyMarchuk
Copy link
Author

I was confused about this. It's very strange to 'say' true if item is empty.
I hadn't checked the documentation before the ticket was created.
Thanks for your help!

@ufcpp
Copy link
Contributor

ufcpp commented Dec 12, 2024

That question is probably better suited to StackOverflow.
https://stackoverflow.com/questions/7884888/why-does-enumerable-all-return-true-for-an-empty-sequence

@julealgon
Copy link

I was confused about this. It's very strange to 'say' true if item is empty. I hadn't checked the documentation before the ticket was created. Thanks for your help!

@EvgenyMarchuk this goes much further than C#: it is a logical "rule". Here is another question related to it from a Math perspective:

It may sound strange at first, but it is the most correct way to implement this logical operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants