Local Functions in C#

Local Functions in C# with Examples

In this article, I am going to discuss the Local Functions in C# which is introduced as part of C# 7. The Local Functions in C# means a function is declared and defined inside another function. Please read our previous article before proceeding to this article where we discussed how to split a tuple in C# with examples.

What are Local Functions in C#?

The Local Functions in C# are the special kind of inner function or you can say sub-function or function within a function that can be declared and defined by the parent function. These methods or functions are the private methods for their containing type and are only called by their parent method. 

Why do we need Local Functions in C#?

If you want to execute some piece of code multiple times within a method then you can put those code as an inner function or you can say local function within that method. Then call that local function whenever required from the parent method. Some of the examples where we can create local functions are as follows

  1. Small helper function to be used several times within the main or parent method.
  2. Parameter validation functions for any iterators or asynchronous method.
  3. An alternate to recursive functions as local function comparatively takes less memory due to the reduced call stack.
Let’s understand Local Functions in C# with one example.
class Program
    static void Main()
        int a = 10, b = 5;
        int sum = Sum(a, b);
        int difference = Difference(a, b);

        Console.WriteLine($"The Sum of {a} and {b} is {sum}");
        Console.WriteLine($"The Difference of {a} and {b} is {difference}");

        int Sum(int x, int y)
            return x + y;

        int Difference(int x, int y)
            return x - y;

        Console.WriteLine("Press any key to exit.");

When we run the application, it will give us the following output.

Local Functions in C#

As you can see in the above example, we have created two nested functions “Sum” and “Difference”. These two local functions can be called from anywhere by the parent main function only.

Points to Remember while working with Local Functions in C#:

The following points you need to keep in mind while working with the Local Functions in C#.

  1. You can not overload a Local Function in C#
  2. The Accessibility modifiers such as public, private, protected are not allowed.
  3. The compiler will issue a warning if the local function is not used by the parent function as there is no meaning of defining a local function in C# if it is not being used by the parent method.
  4. All variables in the enclosing scope, including local variables, can be accessed

Let us understand the use of Local Functions in C# with one real-time example.

Real-Time Example of Local Functions in C#. The Parameter validation scenario

In the following example, the IsRequestValid local function is used to validate the parameters of AddEmployee function.

When we run the application, it will give us the following output.

Local Functions in C#

In the next article, I am going to discuss Ref Returns and Ref Locals in C# with some examples.


In this article, I try to explain the Local Functions in C# step by step with some simple examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

1 thought on “Local Functions in C#”

Leave a Reply

Your email address will not be published. Required fields are marked *