Posted By

syampolskyhotmailcom on 05/04/10


Tagged

Net foreach Linq


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

cadaver
dbuksbaum
rageit


LINQ ForEach


 / Published in: C#
 

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ForEachExample
  5. {
  6. public static class Utility
  7. {
  8. public static void ForEach<T>(this IEnumerable<T> coll, Action<T> function)
  9. {
  10. IEnumerator<T> en = coll.GetEnumerator();
  11. while (en.MoveNext())
  12. function(en.Current);
  13. }
  14. }
  15.  
  16. class Program
  17. {
  18. static void Main(string[] args)
  19. {
  20. var array = new int[] { 1, 2, 3 };
  21. //Expexted output: 123
  22. array.ForEach(val => Console.Write(val));
  23. }
  24. }
  25. }

Report this snippet  

You need to login to post a comment.