What’s the difference between Static local functions and local functions in C#?.

In this post. I’m going to explain the differences between static local functions and local functions in C#.

What’s a local function?

Well, is a function declared inside a method or function. In C·, when you create a local function, you’re working with a Func<T> or Action<T> object. It’s syntactic sugar.

What are the differences?

Any local function you declare in a static method will be static, which means that it only can capture arguments or static values in the context.

If you declare a local function in an instance context. Then, the local function could capture arguments, instance members, and static values by default. But, if you mark a local function inside a non-static method as STATIC. It only will capture arguments and static values.

Having static local functions could help you to reduce the side effects of the function because you explicitly set a group of values that can’t be reached.

Example.

Gustavo Sánchez