Created
June 27, 2022 09:29
-
-
Save laserpez/e744c180870429c14fc3efb8a47ff929 to your computer and use it in GitHub Desktop.
Python's "enumerate" C# implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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