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]); } } }}
public class Friend{ public string FirstName { get; set; } public string LastName { get; set; }}
List<Friend> lst = new List<Friend>();Friend f1 = new Friend { FirstName = "Shantanu", LastName = "Kush" };Friend f2 = new Friend { FirstName = "Vijay", LastName = "Kodwani" };Friend f3 = new Friend { FirstName = "Naveed", LastName = "Ahmed" };Friend f4 = new Friend { FirstName = "Parvez", LastName = "Naqati" };lst.Add(f1);lst.Add(f2);lst.Add(f3);lst.Add(f4);//This will execute a statement block against every second (step) friend in the listlst.For(0, lst.Count - 1, 2, (friend) => { MessageBox.Show(friend.FirstName); MessageBox.Show(friend.LastName); });//This will execute a statement block against every second (step) friend in the list.//The End condition passed is also a lambda statement blocklst.For(0, i => i < lst.Count - 1, 2, (friend) => { MessageBox.Show(friend.FirstName); MessageBox.Show(friend.LastName); });