C#: Func<> instead of methods?

mnsr picture mnsr · Aug 24, 2011 · Viewed 8.4k times · Source

This is a curiosity questions for you all in the know:

Is there any harm/downside to using a Func instead of a method? Simple example:

private static Func<int, int, DBContext, List<T>> Foo =
    (i1, i2, dbc) =>
        (i1 != 0) ? dbc.Bar(i2) : new List<T> { /*some default values ...*/ };

Vs

private static List<T> Foo(int i1, int i2, DBContext dbc)
{
    return i1 != 0 ? dbc.Bar(i2) : new List<T> { /*some default values ...*/ };
}

Answer

Carsten picture Carsten · Aug 24, 2011

I see severale downsides:

  • performance impact (delegate vs method) - small but it's there
  • no parameternames (hurts readability on calls)
  • the definition itself is less readable
  • no overload possible (thanks to xanatos)

as you gain nothing I would only to so in a local and small context and prefer the static method