Skip to content

Instantly share code, notes, and snippets.

@yuchiki
Last active October 19, 2018 07:05
Show Gist options
  • Save yuchiki/92091fff068d2eb3931201c67ce5fc09 to your computer and use it in GitHub Desktop.
Save yuchiki/92091fff068d2eb3931201c67ce5fc09 to your computer and use it in GitHub Desktop.
[C#] Enumerate IEnumerable with integer indexes
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));
}
@yuchiki
Copy link
Author

yuchiki commented Oct 19, 2018

Enumerate() function in this code zips an IEnumerable value and the sequence of the natural number.

Result of the execution of the code above:
kekka

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment