-
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]: Linq method for getting value and index #95563
Comments
Tagging subscribers to this area: @dotnet/area-system-linq Issue DetailsBackground and motivationThere is an existing Linq method for returning an enumerable's value and index, but it was added pre-tuples which makes it clunky as it forces the caller to create a type representation for the combination of the value and index, for example using this: IEnumerable<string> lines = GetLines();
foreach (var (line, lineIndex) in lines.Select((l, i) => (l, i)))
{
// ...
} API Proposalnamespace System.Linq;
public static partial class Enumerable
{
public static IEnumerable<(T Value, int Index)> SelectIndex<T>(this IEnumerable<T> source);
}
public static partial class Queryable
{
public static IQueryable<(T Value, int Index)> SelectIndex<T>(this IQueryable<T> source);
} API UsageUsing IEnumerable<string> lines = GetLines();
foreach (var (line, lineIndex) in lines.SelectIndex())
{
// ...
} Alternative DesignsNo response RisksNone.
|
How about |
I also want to point out that name |
How about just This is an extension that I have in almost all the code I write, so I'd love it if it were just ambiently available. |
@eiriktsarpalis that makes sense. Personally, I'd prefer @marklio's suggestion |
Methods starting with |
|
The same method in MoreLinq and SuperLinq is just |
What about |
namespace System.Linq;
public static partial class Enumerable
{
public static IEnumerable<(int Index, T Item)> Index<T>(this IEnumerable<T> source);
}
public static partial class Queryable
{
public static IQueryable<(int Index, T Item)> Index<T>(this IQueryable<T> source);
} |
Background and motivation
There is an existing Linq method for returning an enumerable's value and index, but it was added pre-tuples which makes it clunky as it forces the caller to create a type representation for the combination of the value and index, for example using this:
API Proposal
API Usage
Using
WithIndex
, one can express this idea much more succinctly:Alternative Designs
No response
Risks
None.
The text was updated successfully, but these errors were encountered: