Static Local Function in C# 8.0
Last Updated :
06 Feb, 2025
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:
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:
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:
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. |
---|
Similar Reads
Local Function in C# The local function feature is introduced in C# 7.0. It allows you to declare a method inside the body of an already defined method. Or in other words, we can say that a local function is a private function of a function whose scope is limited to that function in which it is created. The type of loca
4 min read
C# | Static Class In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members and static methods. It is not allowed to create objects of the static class and since it does not allow to create objects it means it does not allow instance constructor. Stat
3 min read
Stack.Count Property in C# This method(comes under System.Collections namespace) is used to get the number of elements contained in the Stack. The capacity is the number of elements that the Stack can store and the count is the number of elements that are actually in the Stack. The capacity is always greater than or equal to
2 min read
Static keyword in C# static is a modifier in C# which is applicable for the following: ClassesVariablesMethodsConstructorIt is also applicable to properties, event, and operators. To create a static member(class, variable, methods, constructor), precede its declaration with the keyword static. When a member is declared
4 min read
Default Interface Methods in C# 8.0 Before C# 8.0 interfaces only contain the declaration of the members(methods, properties, events, and indexers), but from C# 8.0 it is allowed to add members as well as their implementation to the interface. Now you are allowed to add a method with their implementation to the interface without break
5 min read