Last active
December 14, 2015 12:59
-
-
Save FernandoVezzali/5090603 to your computer and use it in GitHub Desktop.
Extensions with delegates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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