Suppose we have a domain in which we can generalise some operation by passing in a function, like the Select
\ map
functions. Now suppose that the function we pass in has some domain specific name, and fixed parameter types. Let's call it a BazFunction
We can approach defining a BazFunction
in two ways:
- As a delegate:
Bar delegate BazFunction(int x)
- As a lambda:
Func<int, Bar>
Their usage when calling the operation is identical (Foo((x) => x)
) but the function definition is different
public Bar Foo(Func<int, Bar> baz)
public Bar Foo(BazFunction baz)
The obvious benefit of using a delegate is the readability, at least to somebody familiar with the domain.
What are the cons of such an approach? Both technical and non-technical.
Func<T, U>
is also a delegate, and not a lambda - it just happens to be one which is defined in the BCL. A lambda is a function object defined using the lambda syntax - i.e.x => x + 5;
. I think what you actually mean is "Should I use a delegate from the BCL or define my own?"