开发者

Optional delegates in C# [duplicate]

This question already has answers here: Default value on generic predicate as argument (3 answers) Closed 9 years ago.

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 m开发者_高级运维y alwaysTrue function is not constant, but hey, compiler knows better :)

Is there any way to make the delegate parameter optional?


It's not constant because you've created a delegate from a method group... that's not a compile-time constant as far as the C# language is concerned.

If you don't mind abusing the meaning of null slightly you could use:

private static readonly Predicate<int> AlwaysTrue = ignored => true;

public static List<int> Even(this List<int> numbers,
                             Predicate<int> predicate = null)
{
    predicate = predicate ?? AlwaysTrue;
    return numbers.Where(num=> num % 2 == 0 && predicate(num));
}

(You could still make AlwaysTrue a method and use a method group conversion, but the above approach is very slightly more efficient by creating the delegate instance just once.)


What you have to do is allow it to be null, and then treat that as always true.

You have two options for this, double the code to leave out the delegate call, this will perform faster in the cases where you don't pass the delegate.

public static List<int> Even(this List<int> numbers, Predicate<int> predicate = null)
{
    if (predicate == null)
        return numbers.Where(num=> num % 2 == 0).ToList();
    else
        return numbers.Where(num=> num % 2 == 0 && predicate(num)).ToList();
}

Or, provide a dummy implementation as you wanted:

public static List<int> Even(this List<int> numbers, Predicate<int> predicate = null)
{
    predicate = predicate ?? new Predicate<int>(alwaysTrue);
    return numbers.Where(num=> num % 2 == 0 && predicate(num)).ToList();
}

Also, consider whether you really want to do this. The effect optional parameters have on compiled code is that the calling code now provides the default value, which means it will always call the overload which take a list and a delegate.

If you later on want to go back, you need to ensure all code that calls the method is recompiled, since it won't magically start using the method that doesn't provide a delegate.

In other words, this call:

var even = list.Even();

Will look like it was written like this:

var even = list.Even(null);

If you now change the method to be overloaded yet again, if the above call isn't recompiled, then it will always call the one with a delegate, just providing null for that parameter.


You could use a null-default value:

public static class Extended 
{
    public static IEnumerable<int> Even(this IEnumerable<int> numbers, 
                                        Predicate<int> predicate = null)
    {
        if (predicate==null)
        {
            predicate = i=>true;
        }

        return numbers.Where(num => num % 2 == 0 && predicate(num));
    }
}


public static List<int> Even(this List<int> numbers, Predicate<in> predicate = null)
{
    return numbers.Where(num=> num % 2 == 0 && (predicate == null || predicate(num)));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜