I keep this extension method around for this:
public static void Each(this IEnumerable ie, Action action)
{
var i = 0;
foreach (var e in ie) action(e, i++);
}
And use it like so:
var strings = new List();
strings.Each((str, n) =>
{
// hooray
});
Or to allow for break
-like behaviour:
public static bool Each(this IEnumerable ie, Func action)
{
int i = 0;
foreach (T e in ie) if (!action(e, i++)) return false;
return true;
}
var strings = new List() { "a", "b", "c" };
bool iteratedAll = strings.Each ((str, n)) =>
{
if (str == "b") return false;
return true;
});