Local Functions In C# 7.0

Definition

Local functions mean you can create a function within another function, and it encapsulates completely within that function.

Features Of Local Functions In C# 7.0

Parameters and local variables from the enclosing scope are visible within the local function.

Why I should think about Local functions?

Let's think about when you need a helper function, which will be called once.

In the old days, before C# 7, you could think in 2 ways.

  1. Create a private method inside the class and call it in the function, but that will end up having many private methods that wouldn’t be reused.
  2. Using Func and Actions types with the anonymous methods, this solution has a limitation, like you can’t use generics, out, ref, and params.

Now, in C# 7 world, you can declare a function inside another one.

Let's take some examples.

Example 1. In the code mentioned above, we created a function GetMyName() inside the Main Function.

using System;
class Program
{
    static void Main(string[] args)
    {
        // Method calling
        GetMyName();
    }
    // Method Declaration
    static void GetMyName()
    {
        Console.WriteLine("My Name Is Omar Maher");
    }
}

 

Let's run it and see the result.

result

Also, you can make a calling for the local function after the declaration, as shown below.

using System;
class Program
{
    static void Main(string[] args)
    {
        // Method Declaration
        void GetMyName()
        {
            Console.WriteLine("My Name Is Omar Maher");
        }
        // Method calling
        GetMyName();
    }
}

It will give you the same result.

result

Example 2. Also, you can declare the parameter into the local function, but it will be available in the scope of GetMyName(string name), as shown.

using System;
class Program
{
    static void Main(string[] args)
    {
        // Method calling
        GetMyName("Omar Maher");
    }
    // Method Declaration
    static void GetMyName(string name)
    {
        Console.WriteLine($"My Name Is {name}");
    }
}

When you run, you will get the same result.

result

Example 3. Another example is that if you want the variable available anywhere in the Main method, you can declare the variable outside the local function, as shown.

using System;
class Program
{
    static void Main(string[] args)
    {
        string name = "Omar Maher";
        // Method calling
        GetMyName();
        // Method Declaration
        void GetMyName()
        {
            Console.WriteLine($"My Name Is {name}");
        }
    }
}

In the example, the variable name is available inside GetMyName() and outside it.

The result will be the same.

result

Example 4. Also, you can use the local functions inside the constrictors.

Let's create a class MyData and create a Constructor inside it, as shown.

class MyData
{
    public MyData()
    {
    }
}

Now, let's create a local function GetMyName() inside the MyData Constructor, as shown.

using System;
class MyData
{
    public MyData()
    {
        // Method calling
        GetMyName();

        // Method Declaration
        void GetMyName()
        {
            Console.WriteLine($"My Name Is Omar Maher");
        }
    }
}

Let's create an object from the MyData Class in the Main Method, as shown. 

using System;
class MyData
{
    public MyData()
    {
        // Method Declaration
        void GetMyName()
        {
            Console.WriteLine($"My Name Is Omar Maher");
        }
    }
    static void Main(string[] args)
    {
        MyData data = new MyData();
    }
}

When we run the code, the Constructor will call GetMyName() and will continue.

The result will be the same.

result

Example 5. Also, you can send the parameters and throw the Constructor, as shown.

using System;
class MyData
{
    public MyData(string name)
    {
        // Method calling
        GetMyName();

        // Method Declaration
        void GetMyName()
        {
            Console.WriteLine($"My Name Is {name}");
        }
    }
}

Now, name the variable, and it will be available anywhere inside the Constructor.

Let's create another object.

using System;
class MyData
{
    public MyData(string name)
    {
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyData data1 = new MyData("Omar Maher");
    }
}

The result will be the same.

result

You can find the source code here.

https://github.com/omarmaher100/LocalFunctions.

In the end, I hope it will help you. Wait for me for more topics.


Similar Articles