Last active
October 19, 2018 07:05
-
-
Save yuchiki/92091fff068d2eb3931201c67ce5fc09 to your computer and use it in GitHub Desktop.
[C#] Enumerate IEnumerable with integer indexes
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; | |
using static System.Console; | |
class Program { | |
static void Main() { | |
var PIString = Math.PI.ToString(); | |
foreach (var (c, i) in PIString.Enumerate()) | |
WriteLine($"{i + 1, 2}th letter: {c}"); | |
} | |
} | |
static class MyExtensions { | |
public static IEnumerable<(T, int)> Enumerate<T>(this IEnumerable<T> source) => source.Select((x, i) => (x, i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Enumerate() function in this code zips an IEnumerable value and the sequence of the natural number.
Result of the execution of the code above: