Skip to content

Methods that return only lists or arrays should use 'IReadOnlyList<T>' instead of 'IEnumerable<T>' as return type #1322

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

Open
RalfKoban opened this issue May 20, 2025 · 0 comments

Comments

@RalfKoban
Copy link
Owner

If a method only returns a list or array, avoid using IEnumerable<T> as the return type. Instead, prefer returning IReadOnlyList<T>.

This makes it clear to consumers that:

  1. The result is a complete, finite collection, not a potentially lazy or infinite sequence.
  2. They can use the result directly without needing to call .ToList() to materialize it.

Using IReadOnlyList<T> improves clarity and usability, especially when the method is guaranteed to return a fully realized list.

Note: Do not report that issue in case the method is an interface implementation or an override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: To do
Development

No branches or pull requests

1 participant