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 What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos