What are local functions in C#
C# Local Functions is an interesting feature released with C# 7.0. Essentially they methods that can be declared within the body of method and run within the context of that method.
C# Local Functions are designed to be methods that are used by only one other method to help keep each method small and focused in order to reduce class bloat. Local functions can be defined in the body of any method, constructor or property.
Nested functions in JavaScript
If you are a familiar with JavaScript, you’ll know doubt observe that this type of behaviour may be similar to
nested functions
. The interesting thing about nested functions in JavaScript is their variable scoping rules : they can access parameters and variables of the function or functions that are nested within.Inner Functions in Python
The Python Programming language also has the concept of
Inner Functions
Why Local Functions in C#
There are several advantages to local functions
Encapsulation
You use local functions to protect them from anything happening outside of the function, meaning that they are hidden from the global scope
Keeping it DRY
When writing functions you may possibly want to use the same block of code in numerous places. However, that block of code will only make sense in the context of the function, and will not be used anywhere else in your application.
It’s not worth the administrative overhead of extracting that out it’s own function at the class level. Using a local function you can extract the logic for reuse within the function.
Closure and Factory Functions
This is probably the most important reason to use local functions. Most local functions may be just ordinary functions that merely happened to be nested inside another function. However, the ideal case for local functions is to do with encapsulation of some behaviour and pass it around like any other object, and still have access to the context in which they were first declared.
What’s a closure
A closure simply causes the inner function to remember the state of its environment when called. The closure “closes” the local variable on the stack and this stays around after the the stack creation has finished executing.
Jon Skeet has a great blog post about the beauty of closures and I recommend reading his book C# in Depth
The use of closures and factory functions is the most common, and powerful, use for local functions. In most cases, when you see a decorated function, the decorator is a factory function which takes a function as argument, and returns a new function which includes the old function inside the closure.
C# Local Functions in action
We’ll create contrived example of local functions. Just to illustrate thier use.
A console application that generates some form of random hero name for a user.
There is nothing really complicated about this code. However, it does prove a couple of really important concepts of Local Functions.
- Local Functions don’t have to have a return type eg.
void
- Local Functions can have return types
- Local functions can call each other
- Local functions can accept parameters
Lets quickly add some additional logic, to cope with a user not liking his given Hero Name. We’re not going win any clean code points here, but I just wanted to highlight some important functionality to do with variables.
I just want to highlight here that if you declare a variable within the root function, it is accessible and writable by the nested functions.
Lets refactor the code and explore what else we can do with local functions.
We’ll make use of events and even look at creating a
self cleaning delegate
, which previously required alot of code trickery, but now is a relatively trvial task in C#Conclusion
Although probably not immediately apparent in the above contrived examples local functions are extremely useful in solving computer science algorithm challenges. Although it may be possible to make use of Lambda expression to do similar types of tasks there are a number of reasons to prefer using local functions instead of defining and calling lambda expressions.
For more info on this check out the Microsoft article – Local functions compared to Lambda expressions