Local functions enable you to define a function within the scope of another method to help in promoting encapsulation and bring local variables into scope Credit: IDG The support for local functions is an excellent new feature that has been introduced in C# 7. Note that you can define local functions inside any method, the constructor of a class or inside a property — both getter and setter. When it’s compiled by the C# compiler, a local function gets transformed into a private method. When developing applications, you might often need to create methods that aren’t reused — you need them just for modularity. You might not want your methods to be long as maintaining such methods would become a nightmare over time. However, you might end up having many private methods that would not be reused, right? This new feature in C# 7 comes to the rescue in such circumstances — you can define functions that are local to another scope, or it can be inside another function or even inside a property (both getter and setter). Imagine a situation where you would need a helper function that would be called just once. Before C# 7 was around, you could have achieved this using Func and Action types with anonymous methods. However, there were a few challenges. They did not support generics, params and ref and out parameters. With C# 7 around, you can now declare such functions inside another the body of another function. Such functions are known as local functions. In other words, the support for local functions enables you to define a function within the scope of another function. Implementing local functions in C# Let’s write some code and see how local functions work. Consider the following code snippet. Note that the Sum method is defined inside the body of the Main method in the code snippet that follows. static void Main(string[] args) { int Sum(int x, int y) { return x + y; } Console.WriteLine(Sum(10, 20)); Console.ReadKey(); } In this example, the Sum method is a local function — it’s local to the Main method. In other words, the Sum method can be used only inside the Main method, i.e., the method inside which it has been defined. Local functions can have all features of a regular method except that local functions cannot be static in nature. A local function can even be asynchronous and can have access to variables from the enclosing block as well. Parameters and local variables of the enclosing scope can be used inside a local function, similar to lambda expressions. It should be noted that captured local variables are passed to a local function by reference. Here’s another example that illustrates how a local function can access the variables of its enclosing type. public static void Display(string str) { int ctr = 5; DisplayText(); void DisplayText () { for(int i = 0; i < ctr; i++) Console.WriteLine(str); } } Now, refer to the code snippet given above. The Display method contains a string parameter and an integer variable inside it. Note that the local function defined inside the Display method (named DisplayText) can have access to the local variables and also the argument of the Display method. Nice feature, isn’t it? One of the major benefits of local functions is encapsulation — a local function can be called only from its enclosing type. Note that if you have a private method in your class, any member of the class can invoke the private method. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos