Static Local Functions In C# 8.0

Introduction

 
In today’s article we will look at a new feature introduced with C# 8.0. This is the ability to declare static local functions inside another function. This will ensure that the local function does not reference any variables from the enclosing scope. C# 8.0 is supported on .NET Core 3.x and .NET Standard 2.1.

Creating a Simple Console Application to Demo

 
We start by creating a simple console application using Visual Studio 2019 community edition. The framework used is .NET Core 3.1.
 
In this application, we will create a simple function that has a local function. We will first declare this local function as non-static and see that it will be able to reference variables in the enclosing scope. Then, we will create another local function declared as static. This will enforce the rule that this function cannot reference variables in the enclosing scope. This is useful when we want to ensure that the local function is independent and does not reference variables in the enclosing scope. Below is the code:
  1. using System;  
  2. namespace CSharp8Features {  
  3.     class Program {  
  4.         static void Main(string[] args) {  
  5.             Console.WriteLine($ "FuncNonStatic() returns {FuncNonStatic()}");  
  6.             Console.WriteLine($ "FuncStaticWorks() returns {FuncStaticWorks()}");  
  7.             Console.ReadKey();  
  8.         }  
  9.         static int FuncNonStatic() {  
  10.             int someValue;  
  11.             ALocal Function();  
  12.             return someValue;  
  13.             void ALocal Function() => someValue = 100;  
  14.         }  
  15.         static int FuncStatic DoesNotWork() {  
  16.             int someValue;  
  17.             ALocal Function();  
  18.             return someValue;  
  19.             static void ALocal Function() => someValue = 100;  
  20.         }  
  21.         static int FuncStatic Works() {  
  22.             int a = 10;  
  23.             int b = 20;  
  24.             return Add Values(a, b);  
  25.             static int Add Values(int firstValue, int secondValue) => firstValue + secondValue;  
  26.         }  
  27.     }  
  28. }  
When we compile the code, we get the below,
 
Static Local Functions in C# 8.0 
Here we see that the first function “FuncNonStatic” is fine as the local function is not static and it can reference variables in the enclosing scope.
 
The third function “FuncStaticWorks” is also fine as the local function is declared as static but it does not reference any variable from the enclosing scope.
 
However, the second function “FuncStaticDoesNotWork” does not compile as a static local function tries to reference a variable in the enclosing scope.
 
Once we comment that function out, we can run the code and get the below result,
 
Static Local Functions in C# 8.0
 

Summary

 
In this article, we looked at how to use static local functions inside another function and the limitations of doing this in C#. This is another one of the new features that has been introduced in C# 8.0. I will be covering more of these features in future articles. Happy Coding!


Similar Articles