I want to create an extension method which I can invoke on an object .
The return value will be defined by a function.
Something like this : ( this is just an example )
bool isMature= thePerson.Age.Apply<bool>(d => { if (d >18) return true;
return false;
})
and here is the extension method :
public static Tout Apply<Tout>(this Object obj, Func< Tout> f)
{
return f( );
}
The error : incompatible anonymous function signature
What am I doing wrong ?
Your method takes just a
Func<Tout>– which is a function taking no parameters, but returning a value.Your lambda expression has a parameter (
d) – and it looks like you’re assuming that’s an integer. It’s not clear what you’re trying to do, but if you want to use a parameter in the lambda expression, you’re going to have to change the signature fromFunc<TResult>toFunc<TArg, TResult>or something similar – and provide an argument in the invocation.