IEnumerable<T> extensions

I have written extension methods for IEnumerable<T> which allows you to execute a lambda expression statement body (Action) on the items.
 
The ForEach extension allows a certain lambda statement body to be executed against all the items.

This For extension allows you to control the items passed to the statement body using Start, End and Step value of the loop.

You can think of these extensions as lambda expression wrappers to a for loop on a list.

Extensions to IEnumerable<T> :


using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;

namespace
System.Collections.Generic
{
   public static class ExtensionMethods
   {
      public static void ForEach<T>(this IEnumerable<T> List, Action<T> Action)
      {
         foreach (T item in List)
         {
            Action.Invoke(item);
         }
      }
      public static void For<T>(this IEnumerable<T> List, int Start, int End, int Step, Action<T> Action)
      {
         for (int i = Start; i < End; i = i + Step)
         {
            Action.Invoke(List.ElementAt(i));
         }
      }
      public static void For<T>(this IEnumerable<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.ElementAt(i));
         }
      }
      public static void For<T>(this IEnumerable<T> List, int Start, Func<int, bool> End, int Step, Func<T, int, int> Action)
      {
         for (int i = Start; End.Invoke(i); i = i + Step)
         {
            i = Action.Invoke(List.ElementAt(i), i);
         }
      }
   }
}


Usage :

These extensions can be used anywhere IEnumerable is used like in LINQ to XML as shown below :


using
System.Xml.Linq;

XDocument
xDoc = XDocument.Parse(@"<Friends>
                                       <Friend><FirstName>Vijay</FirstName><LastName>Kodwani</LastName></Friend>
                                       <Friend><FirstName>Naveed</FirstName><LastName>Ahmed</LastName></Friend>
                                       <Friend><FirstName>Parvez</FirstName><LastName>Naqati</LastName></Friend>
                                       <Friend><FirstName>Shantanu</FirstName><LastName>Kush</LastName></Friend>
                                 </Friends>"
);

//This will execute a statement block against every friend in the xml.

xDoc.Descendants(
"Friend").ForEach(friend =>
{
   MessageBox.Show(friend.Element("FirstName").Value);
   MessageBox.Show(friend.Element("LastName").Value);
});

//This will execute a statement block against every second (step) friend in the xml.

xDoc.Descendants(
"Friend").For(0, xDoc.Descendants("Friend").Count(), 2, friend =>
{
   MessageBox.Show(friend.Element("FirstName").Value);
   MessageBox.Show(friend.Element("LastName").Value);
});


//This will execute a statement block against every second (step) friend in the list.

//The End condition passed is also a lambda statement block
xDoc.Descendants(
"Friend").For(0, i => i < xDoc.Descendants("Friend").Count() - 1, 2, (friend) =>
{
   MessageBox.Show(friend.Element("FirstName").Value);
   MessageBox.Show(friend.Element("LastName").Value);
});


Ebook Download
View all
Learn
View all