Skip to content

Instantly share code, notes, and snippets.

@TangChr
Created November 21, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TangChr/f41e3e42fe8b9482cf27 to your computer and use it in GitHub Desktop.
Save TangChr/f41e3e42fe8b9482cf27 to your computer and use it in GitHub Desktop.
C#: Add condition to Extension Method .ForEach
using System;
using System.Collections.Generic;
using System.Linq;
namespace TangChr
{
public static class ExtendForEach
{
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action, Func<T, bool> condition)
{
for (int i = 0; i < collection.Count(); i++)
{
if (condition(collection.ElementAt(i)))
action(collection.ElementAt(i));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment