C# Local Functions are an interesting feature released with C# 7.0. C# local functions are methods declared in another method and run in the context of that method. They are methods that are used only by one other method and help to keep each method small and focused.

In this post we explore C# Local functions and learn how best to make use of them in .net core applications.

What are local functions in C#

Essentially C# local functions 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<T> 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


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

Suggested Reading

C# in Depth

C# in Depth

Jon Skeet

Price: £26.74

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.


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.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee