This is a simple example of two extension methods overloads
public static class Extended
{
public static IEnumerable<int> Even(this List<int> numbers)
{
return numbers.Where(num=> num % 2 == 0);
}
public static IEnumerable<int> Even(this List<int> numbers, Predicate<int> predicate)
{
return numbers.Where(num=> num % 2 == 0 && predicate(num));
}
}
I'd like to be able to merge them into one, by setting a delegate to be optional:
public static class Extended
{
public static IEnumerable<int> Even(this List<int> numbers, Predicate<in> predicate = alwaysTrue)
{
return numbers.Where(num=> num % 2 == 0 && predicate(num));
}
public static bool alwaysTrue(int a) { return true; }
}
However, compiler throws an error:
Default parameter value for 'predicate' must be a compile-time constant
I don't see how my alwaysTrue function is not constant, but hey, compiler knows better :)
Is there any way to make the delegate parameter optional?