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?
Most predicates take one, two, or three arguments. A predicate and its arguments form a predicate-argument structure.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With