Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Created May 23, 2013 07:09
Show Gist options
  • Save vendettamit/5633219 to your computer and use it in GitHub Desktop.
Save vendettamit/5633219 to your computer and use it in GitHub Desktop.
/// <summary>
/// Custom function to perform looping operation.
/// </summary>
/// <typeparam name="T">type of ienumerable</typeparam>
/// <param name="list">The list.</param>
/// <param name="action">The action.</param>
/// <returns>index value</returns>
/// <exception cref="System.ArgumentNullException"> if the supplied action is null</exception>
public static int ForEach<T>(this IEnumerable<T> list, Action<int, T> action)
{
if (action == null)
{
throw new ArgumentNullException("action");
}
var index = 0;
foreach (var elem in list)
{
action(index++, elem);
}
return index;
}
/// <summary>
/// Fors the each.
/// </summary>
/// <typeparam name="T">type of ienumerable</typeparam>
/// <param name="enumeration">The enumeration.</param>
/// <param name="action">The action.</param>
public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (T item in enumeration)
{
action(item);
}
}
@vendettamit
Copy link
Author

Usage:

        // Remove extra leading and trailing spaces
        var tempVals = argument.FilesToDownload;
        tempVals.ForEach((i, item) => argument.FilesToDownload[i] = item.Trim());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment