Skip to content

Instantly share code, notes, and snippets.

@jltrem
Created September 19, 2021 21:31
Show Gist options
  • Save jltrem/c905bbdf412f1461f2255dd48984b6b1 to your computer and use it in GitHub Desktop.
Save jltrem/c905bbdf412f1461f2255dd48984b6b1 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp
{
public static class Program
{
static void Main(string[] args)
{
var items = new List<string> { "kirk", "spock", "bones", "scotty" };
foreach ((int index, string item) in items.Indexed())
{
Console.WriteLine($"item[{index}] is {item}");
}
}
}
public static class Sequence
{
public static IEnumerable<(int, T)> Indexed<T>(this IEnumerable<T> sequence) =>
sequence.Select((value, index) => (index, value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment