Last active
April 18, 2017 10:42
-
-
Save tombatron/5b074b67587c10e66112299113345b7d to your computer and use it in GitHub Desktop.
Python's `enumerate` in C# 7.
This file contains hidden or 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
void Main() | |
{ | |
var items = new []{ "first", "second", "third" }; | |
foreach(var (item, index) in Enumerate(items)) | |
{ | |
Console.WriteLine($"{item} is index {index}."); | |
} | |
} | |
public static IEnumerable<(T, int)> Enumerate<T>(IEnumerable<T> enumerable) | |
{ | |
var currentIndex = 0; | |
foreach(var e in enumerable) | |
{ | |
yield return (e, currentIndex); | |
currentIndex++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment