Search code examples
c#delegatesabstractionfunc

Best pratices for Func<TResp> and Func<TResp, T>


Let us suppose that I have two functions like:

public T Foo<T>(Func<T> func){
    return func();
}

public TResult Foo<T, TResult>(Func<T, TResult> func, T param){
    return func(param);
}

And I have the following code to execute:

someObj.someMethod(someParam);

Which approach is preferable, and what is the difference?

var foo = Foo(() => someObj.someMethod(someParam));

or

var foo = Foo(someObj.someMethod, someParam);

Solution

  • 1st approach seems to be more preferable.

    Foo(() => someObj.someMethod(someParam));
    

    As someParam is related to someMethod more than Foo.