Created
February 23, 2022 20:41
-
-
Save isaacabraham/6be22d39f751e2814b6f973ad5bc9f32 to your computer and use it in GitHub Desktop.
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
namespace ClassLibrary1; | |
public static class LinqExtensions | |
{ | |
public static IEnumerable<TOut> Choose<T, TOut>(this IEnumerable<T> input, Func<T, TOut?> chooser) | |
{ | |
foreach (var i in input) | |
{ | |
var c = chooser(i); | |
if (c is { } q) | |
{ | |
yield return q; | |
} | |
} | |
} | |
public static string? EvenNumbers(int number) | |
{ | |
if (number % 2 == 0) | |
{ | |
return number.ToString(); | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
public static void Foo() | |
{ | |
var numbers = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
var x = numbers.Choose(EvenNumbers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment