History of Idiom 7 > diff from v117 to v118
Edit summary for version 118 by programming-idioms.org:
[C#] Wrong demo URL
[C#] Wrong demo URL
↷
Version 117
2021-09-27, 09:42:29
Version 118
2021-09-27, 16:19:40
Idiom #7 Iterate over list indexes and values
Print each index i with its value x from an array-like collection items
Idiom #7 Iterate over list indexes and values
Print each index i with its value x from an array-like collection items
Variables
i,x,itemsVariables
i,x,itemsExtra Keywords
indices traverse traversalExtra Keywords
indices traverse traversalCode
foreach (var (item, index) in items.AsIndexed())
{
Console.WriteLine($"{index}: {item}");
}
// ---------------------------------------------------------------
public static class Extensions
{
public static IEnumerable<(T, int)> AsIndexed<T>(
this IEnumerable<T> source)
{
var index = 0;
foreach (var item in source)
{
yield return (item, index++);
}
}
}
Code
foreach (var (item, index) in items.AsIndexed())
{
Console.WriteLine($"{index}: {item}");
}
// ---------------------------------------------------------------
public static class Extensions
{
public static IEnumerable<(T, int)> AsIndexed<T>(
this IEnumerable<T> source)
{
var index = 0;
foreach (var item in source)
{
yield return (item, index++);
}
}
}
Comments bubble
Such an extension method isn't provided as part of the standard framework but can be easily added to a project
Comments bubble
Such an extension method isn't provided as part of the standard framework but can be easily added to a project