Created
May 15, 2015 22:27
-
-
Save vquaiato/f12fdbfdfb3a9d7c2745 to your computer and use it in GitHub Desktop.
foreach with index in C#
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
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