Open In App

Static Local Function in C# 8.0

Last Updated : 06 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Static local functions introduced in C# 8.0. It is a local function created using the static keyword. This is used to prevent variables to captured from the enclosing. If a method is marked as static then we cannot access variables from the enclosing method. It enhances the performance by reducing the overhead of creating a closure.

Example 1: Demonstration of static local function in C#.

C#
// Simple C# program to illustrate 
// the static local function
using System;

class Geeks
{
	public static void calc()
	{
		int val = 10;
      
		// local function 
		// can access val
		void local_greet()
		{
			Console.WriteLine("Hello from local function" 
                              + val);
		}

		// Static local function 
		// cannot access val
		static void static_greet()
		{
			Console.WriteLine("Hello from static local function" 
                              + val);
		}
		
		// Calling local functions
		local_greet();
      
        // calling static function 
		static_greet();
	}

	public static void Main(String[] args)
	{
		calc();
	}
}

Output:

StaticLocalFunctionOutput

Explanation: In the above code shows that the local function cannot directly access the variable val outside from the enclosing that prevents accidental captures of variables from the outer scope.


Example 2:

C#
// C# program to demonstrate regular and static local functions
using System; 

class Geeks
{
    // Method to demonstrate local and static local functions
    public static void calc()
    {
        int val = 10; // Local variable

        // Regular local function 
        // Can access 'val' directly from the enclosing method
        void local_greet()
        {
            Console.WriteLine("local function has val: " + val);
        }

        // Static local function 
        // Requires a parameter to access external variables
        static void static_greet(int v)
        {
            Console.WriteLine("static local function has val: " + v);
        }
        
        // Calling static function with the parameter 'val'
        static_greet(val);

        // Calling the regular local function that accesses 'val' directly
        local_greet();
    }

    // Main method to execute the calc function
    public static void Main(String[] args)
    {
        calc();
    }
}

Output:

StaticLocalFunctionWithOutput

Explanation: We can not directly access the val inside the static local function but we can pass the value as a parameter and then access it inside our static local function.

Local Function

The local function allows us to declare a method inside the body of an already-defined method. Local function is a private function of a function whose scope is limited to the function in which it is created. The type of local function is similar to the type of function in which it is defined. We can only call the local function from their container members.

Example:

C#
// Simple C# program to
// illustrate local function
using System;

class Geeks
{
	public static void Main()
	{
		// Here SubValue is the local
		// function of the main function
		void SubValue(int a, int b)
		{
			Console.WriteLine("Value of a is: " + a);
			Console.WriteLine("Value of b is: " + b);
			Console.WriteLine("final result: {0}", a - b);
			Console.WriteLine();
		}

		// Calling Local function
		SubValue(30, 10);
		SubValue(80, 60);
	}
}

Output:

LocalFunctionOutput

Local Function vs Static Local Function

Features

Local function

Static local function

Definition

A function defined inside another function.

A location function declare with static keyword.

Variable Access

Can access the variable outside from the enclosing.

Restricted to access the variable outside from the enclosing.

Performance

less optimized, create overhead while capturing variables.

Enhance performance because there is no overhead of closure.

Closures

It creates closure which results memory overhead.

It is not used to access the outer variables so It not create closure.



Next Article
Article Tags :

Similar Reads