Do and Run

by Bill Forney 21. October 2010 22:03

A couple of quick extension methods for your amusement that I use a lot.

 

   1:          /// <summary>
   2:          /// Runs action against the specified source.
   3:          /// </summary>
   4:          /// <typeparam name="T">The type of the element.</typeparam>
   5:          /// <param name="source">The source.</param>
   6:          /// <param name="action">The action.</param>
   7:          public static void Run<T>(this IEnumerable<T> source, Function<T> action)
   8:          {
   9:              if (source == null)
  10:              {
  11:                  throw new ArgumentNullException("source");
  12:              }
  13:   
  14:              if (action == null)
  15:              {
  16:                  throw new ArgumentNullException("action");
  17:              }
  18:   
  19:              foreach (var element in source)
  20:              {
  21:                  action(element);
  22:              }
  23:          }
  24:   
  25:          /// <summary>
  26:          /// Does action against the specified source and returns the source.
  27:          /// </summary>
  28:          /// <typeparam name="T">The type of the element.</typeparam>
  29:          /// <param name="source">The source.</param>
  30:          /// <param name="action">The action.</param>
  31:          /// <returns>The source after the action is executed.</returns>
  32:          public static IEnumerable<T> Do<T>(this IEnumerable<T> source, Function<T> action)
  33:          {
  34:              if (source == null)
  35:              {
  36:                  throw new ArgumentNullException("source");
  37:              }
  38:   
  39:              if (action == null)
  40:              {
  41:                  throw new ArgumentNullException("action");
  42:              }
  43:   
  44:              return source.Select(
  45:                  element =>
  46:                  {
  47:                      action(element);
  48:                      return element;
  49:                  });
  50:          }
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: ,

Software Development | Technology | C#

Comments are closed

Powered by BlogEngine.NET 1.6.2.17
Theme by Extensive SEO

Recent Comments

Comment RSS

About the author

William L. Forney was born in Pennsylvania and relocated to Washington State in early 1999. His hobbies include all the usual things: movies, books, music, video games, etc. He writes short sci-fi/fantasy stories which will someday be published in the form of small novels.

Bill's Photograph

He loves computers and has worked in several different areas from web, multimedia, video and 3D animation to database and windows development.

Currently he works at Visible Reality, LLC as the lead developer and improvGroup, LLC as a networking consultant.

He also works with Padgett & Padgett, PLLC, the accounting firm where he setup shop after moving.

His other blog can be found here.