Skip to content

Instantly share code, notes, and snippets.

@laserpez
Created June 27, 2022 09:29
Show Gist options
  • Save laserpez/e744c180870429c14fc3efb8a47ff929 to your computer and use it in GitHub Desktop.
Save laserpez/e744c180870429c14fc3efb8a47ff929 to your computer and use it in GitHub Desktop.
Python's "enumerate" C# implementation
using System;
using System.Collections.Generic;
using System.Linq;
public static class IEnumerableExtension
{
public static IEnumerable<(int index, T valuee)> Enumerate<T>(this IEnumerable<T> coll)
{
return coll.Select((T val, int i) => (i, val));
}
}
public class Program
{
public static void Main()
{
var collection = new List<int> {4, 2, 3, 1, 8};
foreach (var (i, valuee) in collection.Enumerate())
{
Console.WriteLine($"collection[{i}] = {valuee}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment