Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass additional parameters to predicate functions?

Can I pass additional parameters to a predicate function?

I need it in a sorting process actually.

public void Sort(
    Comparison<T> comparison
)

where I would like to use the Comparison predicate in this form:

public delegate int Comparison<T>(
    T x,
    T y,
    object extraParameter
)

Is this possible?

like image 564
pencilCake Avatar asked Aug 20 '10 11:08

pencilCake


People also ask

Can predicate take multiple arguments?

Most predicates take one, two, or three arguments. A predicate and its arguments form a predicate-argument structure.

How to pass predicate as parameter in c#?

You are asking for default value, right? Otherwise, define NoFilter as Predicate<things> and pass it . You can have a default value of null and you'd have to check for that in your code. Alternatively you could overload the method with a second one that doesn't take a predicate.


1 Answers

No, but you can do this:

public Comparison<T> MakeComparison<T>(object extraParameter)
{
    return
        delegate(T x, T y) 
        {
            // do comparison with x, y and extraParameter
        }
}
like image 173
Tim Robinson Avatar answered Oct 19 '22 14:10

Tim Robinson