I'm trying to achieve the following without using lambda function:
Func<bool> test = () => RunSomething("test"); <-- This work fine but creates lambda
Func<bool> test = bind(RunSomething, "test"); <-- Bind "test" to RunSomething
In other words I'm want to know if it is possible to somehow bind Function and arguments.
It is possible in C++ using std::bind, but is it possible in C# ?
Well it's easy to build such a method, but that would use a lambda expression for the implementation:
public Func<TResult> Bind<T, TResult>(Func<T, TResult> func, T arg)
{
return () => func(arg);
}
And likewise some overloads for functions with more arguments:
public Func<T2, TResult> Bind<T1, T2, TResult>
(Func<T1, T2, TResult> func, T1 arg)
{
return t2 => func(arg, t2);
}
public Func<T2, TResult> Bind<T1, T2, T3, TResult>
(Func<T1, T2, T3, TResult> func, T1 arg)
{
return (t2, t3) => func(arg, t2, t3);
}
Keep going as far as you want - possibly even adding methods to bind more than one argument in a call.
You can do all this without a lambda expression, but it would just be more work. For example:
public Func<TResult> Bind<T, TResult>(Func<T, TResult> func, T arg)
{
return new Binder<T, TResult>(func, arg).Apply;
}
private sealed class Binder<T, TResult>
{
private readonly T arg;
private readonly Func<T, TResult> func;
internal Binder(Func<T, TResult> func, T arg)
{
this.func = func;
this.arg = arg;
}
public TResult Apply()
{
return func(arg);
}
}
That's basically what the compiler would do for you automatically with a lambda expression, so why do it yourself?
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