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

feat(List): use TrueForAll instead of All #1433

Open
dylanvdmerwe opened this issue Apr 3, 2024 · 0 comments
Open

feat(List): use TrueForAll instead of All #1433

dylanvdmerwe opened this issue Apr 3, 2024 · 0 comments

Comments

@dylanvdmerwe
Copy link

dylanvdmerwe commented Apr 3, 2024

Product and Version Used:
4.12.0

Both the List.TrueForAll method and the IEnumerable.All method can be used to check if all list elements satisfy a given condition in a collection. However, List.TrueForAll can be faster than IEnumerable.All for List objects. The performance difference may be minor for small collections, but for large collections, it can be noticeable. Memory allocations are also much less with TrueForAll.

Original

public bool AreAllEven(List<int> data) =>
    data.All(x => x % 2 == 0);

public bool AreAllEven(int[] data) =>
    data.All(x => x % 2 == 0);

Fix

public bool AreAllEven(List<int> data) =>
    data.TrueForAll(x => x % 2 == 0);

public bool AreAllEven(int[] data) =>
    Array.TrueForAll(data, x => x % 2 == 0);

Note should also work for classes that inherit from List.

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

2 participants