For Each Extension

i don't know if this is a repost.

But did you know that we can do a little extension with the for each that we always do.

we always use for each for enumerate the linq results right?

 we can do some extensions with it:

public static class Extensions

{

   public static void ForEach<T>(this Ienumerable<T> set,Action<T> action)

  {

    foreach(var t in set)

    {

    action(t);

    } 

  } 

 

if you notice Action<T> is a delegate . the delegate for void or not return anything method. it also extend to accept one param,two param and so on(T1,T2,T3).

 and then on your application side.

public static void Main

 int[] b=Enumerable.range(0,10).ToArray();

 Action<int> someactiondelegate=calculate;

b.ForEach<int>(someactiondelegate); 

public void calculate(int angka)

{

Debug.writeline((angka*angka).ToString());

 

Wala.... 

Share this post: | | | |
Published Sunday, June 22, 2008 8:42 AM by cipto
Filed under:

Comments

No Comments