Skip to content

Instantly share code, notes, and snippets.

@vquaiato
Created May 15, 2015 22:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vquaiato/f12fdbfdfb3a9d7c2745 to your computer and use it in GitHub Desktop.
Save vquaiato/f12fdbfdfb3a9d7c2745 to your computer and use it in GitHub Desktop.
foreach with index in C#
foreach ( var it in someCollection.Select((x,i) => new { Value = x, Index=i }) )
{
if ( it.Index > SomeNumber) {// }
}
//or
public static void Each<T>( this IEnumerable<T> ie, Action<T, int> action )
{
var i = 0;
foreach ( var e in ie ) action( e, i++ );
}
var strings = new List<string>();
strings.Each( ( str, n ) =>
{
// hooray
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment