C# 7 in depth : Exploring local functions

Local functions enable you to define a function within the scope of another method to help in promoting encapsulation and bring local variables into scope

exploring local functions
Credit: IDG

The support for local functions is an excellent new feature that has been introduced in C# 7. Note that you can define local functions inside any method, the constructor of a class or inside a property -- both getter and setter. When it's compiled by the C# compiler, a local function gets transformed into a private method.

When developing applications, you might often need to create methods that aren't reused -- you need them just for modularity. You might not want your methods to be long as maintaining such methods would become a nightmare over time. However, you might end up having many private methods that would not be reused, right? This new feature in C# 7 comes to the rescue in such circumstances -- you can define functions that are local to another scope, or it can be inside another function or even inside a property (both getter and setter).

Imagine a situation where you would need a helper function that would be called just once. Before C# 7 was around, you could have achieved this using Func and Action types with anonymous methods. However, there were a few challenges. They did not support generics, params and ref and out parameters.

With C# 7 around, you can now declare such functions inside another the body of another function. Such functions are known as local functions. In other words, the support for local functions enables you to define a function within the scope of another function.

Implementing local functions in C#

Let’s write some code and see how local functions work. Consider the following code snippet. Note that the Sum method is defined inside the body of the Main method in the code snippet that follows.

 static void Main(string[] args)


            int Sum(int x, int y)


                return x + y;


            Console.WriteLine(Sum(10, 20));



In this example, the Sum method is a local function -- it's local to the Main method. In other words, the Sum method can be used only inside the Main method, i.e., the method inside which it has been defined.

Local functions can have all features of a regular method except that local functions cannot be static in nature. A local function can even be asynchronous and can have access to variables from the enclosing block as well. Parameters and local variables of the enclosing scope can be used inside a local function, similar to lambda expressions. It should be noted that captured local variables are passed to a local function by reference. Here's another example that illustrates how a local function can access the variables of its enclosing type.

public static void Display(string str)


            int ctr = 5;


            void DisplayText ()


                for(int i = 0; i < ctr; i++)




Now, refer to the code snippet given above. The Display method contains a string parameter and an integer variable inside it. Note that the local function defined inside the Display method (named DisplayText) can have access to the local variables and also the argument of the Display method. Nice feature, isn’t it?

One of the major benefits of local functions is encapsulation -- a local function can be called only from its enclosing type. Note that if you have a private method in your class, any member of the class can invoke the private method.

This article is published as part of the IDG Contributor Network. Want to Join?