Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Created February 23, 2022 20:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/6be22d39f751e2814b6f973ad5bc9f32 to your computer and use it in GitHub Desktop.
Save isaacabraham/6be22d39f751e2814b6f973ad5bc9f32 to your computer and use it in GitHub Desktop.
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