added a ForEach extension for enumerables

This commit is contained in:
David Sparer
2018-11-01 16:36:56 -05:00
parent f560fb86d8
commit 2ebf654973

View File

@@ -1,9 +1,11 @@

using System;
using System.Collections.Generic;
using System.Linq;
namespace mRemoteNG.Tools
{
public static class Extensions
public static class Extensions
{
public static Optional<T> Maybe<T>(this T value)
{
@@ -50,5 +52,23 @@ namespace mRemoteNG.Tools
throw new ArgumentException("Value cannot be null or empty", argName);
return value;
}
/// <summary>
/// Perform an action for each item in the given collection. The item
/// is the pass along the processing chain.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <param name="action"></param>
/// <returns></returns>
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> action)
{
collection = collection.ToList();
foreach (var item in collection)
action(item);
return collection;
}
}
}