We already had the ability to create a short anonymous methods that assigned to a variable in C#. We could achieve this using delegate keyword or using Action and Func. But with release of C# 7 local functions got introduced. It has some similarity to lambdas but also some major differences. Local functions are functions that are declared inside another function without the need to create a delegate. Although these two has some similarities, they are indeed different both in terms of performance and how we can use them. In this post I’m going to compare these two features and where it’s best to use them.
Difference between lambdas and local functions
Despite their similarity lambdas and local function have some major differences.
- Lambdas are anonymous and they don’t have symbolical representation. They should be assigned to a variable first. Also they must be initialized with a value, otherwise we encounter a compiler error. But this is not the case for a local function. Local function does not follow the rule of local variables and they can be declared even after the return statement.
- All the local variable used inside lambdas must be initialized at the time of creation of the lambda statement. But local function work like a normal function in this regard.
- A Local function has name that can be used without any indirection.
- In order to create a lambda, a delegate needs to be created. But a local function is just functions, not delegate is needed.
- In order for a lambda to capture the local variables, it needs to create a class. But local function can do that using the struct. This means the heap allocation is unnecessary and this can improve performance in time critical applications.
Advantages of Using local functions
- In most scenarios, local function are more performant, due to how they capture local variables.
- Local function can be recursive without writing awkward code. Lambdas can be recursive also but they do not look as natural and readable as local functions.
- They can be generic.
- Can be used as an iterator using the yield return keyword.
- They are more readable. As an example consider this
int add(int x, int y) => x + y;
method to the lambda version.Func<int, int, int> add = (x, y) => x + y;
- Local function can be declared anywhere in the code even after the return statement.
- Has parameter name on the caller side, but lambdas do not. This image can demonstrate what I mean.
When to Use a local function
- A small helper function is needed that does something trivial.
- For methods that are being called often but not outside the current function.
- You want your function to be recursive.
- The function needs to be generic.
When to use lambdas
- You need to pass around a short piece of code.
- Passing a short piece of code into methods that expect a delegate.
- Your method is only used once as opposed to being called over and over again.
Further Reading
Local functions compared to lambda expressions
C# Local Functions vs. Lambda Expressions.
Summary
In this post I discussed what local functions are and how they are different from Lambdas. We also saw when it’s best to use local functions and the benefits of using them as opposed to lambdas.