What are local functions in C#

C# Local Functions is an interesting feature released with C# 7.0. Essentially they allow the specification of a functions within the body of another method or function and only 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

In C# 6.0 developers could replicate similar functionality in making use of Func delegate inside of a method.

In c# 7.0 we can now replace this implementation as follows

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 its 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 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

 

C# in Depth 3rd Edition

Third Edition has been thoroughly revised to cover the new features of C# 5, including the subtleties of writing maintainable asynchronous code.
Buy

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 their 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.




Run the application and it will work.

The issue with the code here is that no support team will thank you for writing this spaghetti code!

We’ve utilised some great functionality in C#, but have only leveraged it to create a support nightmare.

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 a lot of code trickery, but now is a relatively trivial task in C#

Important considerations of C# local functions

  • Scope of Local functions are only within the method they are defined and they should not contain system-wide logic.
  • Local functions are not visible to other classes
  • They are unit tested with methods that own them.
  • Use local functions only if treally needed.
  • over-use of local functions may lead to long methods that are hard to test.

Conclusion

It may not be immediately apparent in the above contrived examples, but 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

Bill Wagner blog post : C# 7 Feature Proposal: Local Functions is also worth reading for further background on this great language feature.

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
π