-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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]: IndexOf extension method on IEnumerable<T> #110045
Comments
Tagging subscribers to this area: @dotnet/area-system-linq |
That already works today if you set |
Right, and how is that different from ElementAt, for non-indexable collections, which already exists? |
It'll work for any
ElementAt is problematic, for a variety of reasons, including that the data could change on subsequent enumeration and including that it can easily lead to unexpected O(N^2) evaluation. I don't think we should be doubling-down on it. |
For
How about |
Agree that we shouldn't be exposing this due to the index not being practical to use with arbitrary enumerables. |
@eiriktsarpalis what if you just want to know the position at that point in time for some other purpose, and you don't intend to try to use it to index back into the enumerable? |
Can you share an example where this might be the case? Is it common enough to warrant inclusion in System.Linq and not be a helper when necessary? |
To be honest I can't of the top of my head, no. I was playing devil's advocate on this one.
Fair point. It is probably not common enough. I really can't say as I personally never had the need for it. My argument was solely on the reasoning that "because you can't reliably index back into the |
“ My argument was solely on the reasoning that "because you can't reliably index back into the IEnumerable with the value, it is not a good idea" Of course, this is an argument against ElementAt. |
That may be true, but at the same we wouldn't want to consider an API unless there is evidence of it being impactful. |
Fair enough. Like all LINQ methods, this can be easily achieved, so if you don’t think this in relevant, then by all means close it. |
Sounds good. We can always revisit the proposal if we have more evidence of this being impactful enough for inclusion. |
Background and motivation
As the counterpart to ElementAt(), and Index(), I suggest the creation of an IndexOf() method, which would return the index of the passed element on a list, or -1 if the element is not found. This could even be implemented in LINQ to databases, as there are ways to obtain the index of a row.
API Proposal
API Usage
Alternative Designs
No response
Risks
No breaking changes, as it's a new method.
The text was updated successfully, but these errors were encountered: