Skip to content

Instantly share code, notes, and snippets.

@FernandoVezzali
Last active December 14, 2015 12:59
Show Gist options
  • Save FernandoVezzali/5090603 to your computer and use it in GitHub Desktop.
Save FernandoVezzali/5090603 to your computer and use it in GitHub Desktop.
Extensions with delegates
class Program
{
static void Main()
{
IEnumerable<string> names = new string[] { "Ireland", "Brazil", "Iceland" };
foreach (var name in names.Filter(predicate: StringThatStartWithI))
{
Console.WriteLine(name);
}
}
static bool StringThatStartWithI(string s)
{
return s.StartsWith("I");
}
}
public static class MyExtensions
{
public static IEnumerable<T> Filter<T>(this IEnumerable<T> input, Predicate<T> predicate)
{
foreach (var item in input)
{
if (predicate(item))
{
yield return item;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment