Table of contents >> Functions > Local methods and functions
Low importance article, or discussion

Local methods are methods that you define inside another method. They can be used to break down complex code into smaller, more manageable pieces. Think of them as helpers that perform specific tasks, making the parent method easier to understand.

They were introduced in C# version 7, and you can think of them as private functions of an already existing method or function, having their scope limited to the body of the method or function that encompases them. This means that you can only call them from within the body of their parent method/function.

Here is a simple example of a local method, and the way we can use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
 
namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            void PrintText(string text)
            {
                Console.WriteLine(text);
            }
            
            PrintText("Hello World!");
        }
    }
}

And this is an example of a local function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
 
namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int AddNumbers(int x, int y)
            {
                return x + y;
            }
            
            int result = AddNumbers(3, 7);
        }
    }
}

So, they really look and behave like normal methods and functions, but, of course, there are a few differences:

  • You can declare local methods and functions inside normal methods and functions, constructors, finalizers, properties accessors, events accessors, anonymous methods, lambda expressions, and even other local functions.
  • Local methods and functions allow the use of async and unsafe keywords.
  • As explained in the variable scope lesson, members are visible in the block of code where they are declared and inside any nested children blocks of code, but not outside the one where they are declared. This is true for local methods and functions too, and this also means that any parameter or local variable of the parent member that contains them is also visible inside them too.
  • They cannot be static, you are not allowed to specify any kind of access modifier, and, you guessed it, they are by default private.
  • They cannot have attributes, and nor do their parameters.
  • You cannot overload them.
  • When using top level statements, any declared method or function is automatically local.
  • They can be generic.
  • They can use ref and out parameters.

Just like normal methods and functions, local ones can be written using the expression bodied syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            void PrintText(string text) => Console.WriteLine(text);
            
            PrintText("Hello World!");
        }
    }
}

However, you are not allowed to declare local methods and functions inside expression bodied members themselves.