I have written simple For extension methods for List<T> which allows you to execute a lambda statement body (Action) on the list items given the Start, End and Step value of the loop.
In another overload, the End (condition) can be specified in a lambda statement block too.
The ForEach method of the List allows a certain lambda statement body to be executed against all the items in the list.
This For one allows you to control the items passed to the statement body.
You can think of these extensions as lambda wrappers to a for loop on a list.
For extensions to List<T> :
using System; using System.Collections.Generic;
namespace System.Collections.Generic { public static class ExtensionMethods { public static void For<T>(this List<T> List, int Start, int End, int Step, Action<T> Action) { for (int i = Start; i < End; i = i + Step) { Action.Invoke(List[i]); } } public static void For<T>(this List<T> List, int Start, Func<int, bool> End, int Step, Action<T> Action) { for (int i = Start; End.Invoke(i); i = i + Step) { Action.Invoke(List[i]); } } } }
|
Join the conversation! Your thoughts help the community grow.