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

[API Proposal]: Add bool TryGetElementAt<T>(this IEnumerable<T>, out T element) #110338

Closed
alrz opened this issue Dec 2, 2024 · 1 comment
Closed
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Comments

@alrz
Copy link
Member

alrz commented Dec 2, 2024

Background and motivation

This as opposed to checking for bounds each time performing an unguaranteed random access.

Note this could help with any enumerables e.g. arrays, list, so there could be more optimized overloads available.

API Proposal

class Enumerable
{
+  public static bool TryGetElementAt<T>(this IEnumerable<T> source, out T element);
}

API Usage

if (seq.TryGetElementAt(2, out var item)) {

}

Alternative Designs

No response

Risks

No response

@alrz alrz added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 2, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 2, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 2, 2024
@PranavSenthilnathan
Copy link
Member

Closing as a duplicate of #16362. Please continue the conversation there.

Just a note, if your enumerable doesn't have default values, then ElementAtOrDefault could work for your use case.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 3, 2024
@PranavSenthilnathan PranavSenthilnathan closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

No branches or pull requests

2 participants