C# 7.0-Local Functions
Local functions are a new feature in C# 7 that allows defining a function inside another function. Local functions are similar to anonymous methods. Sometimes it’s not necessary to create a named function to perform a specific action because the functionality is only local to a specific function, and a named function would only pollute the outer scope The main goal of Local Functions is encapsulation so the compiler is enforcing that such functions cannot be called from anywhere else in the class. Consider the below example:
- class SampleLocalFunction {
- {
- get {
- return $ @ “{GetFirstName()} {GetLastName()}”;
- string GetFirstName() => “Prasad”;
- string GetLastName() => “Raveendran”;
- }
- }
- }
Here, we have a getter property, which is composing a FullName comprised of two different sections. Each section is being retrieved through separate private methods. We have encapsulated methods usage by Local Functions. Common places where local functions are defined:
- Methods
- Constructors
- Property accessors
- Event accessor
- Lambda expressions
- Finalizers, a.k.a destructors
- Local functions (meaning local functions can be nested within each other)
Generic Local Function
Generic functions combine reusability, type safety, and efficiency. They are often used with collections and methods, which operate on them. Example below:
- class Program {
- static void Main(string[] args) {
- void MyGeneric < Value > (Value x) {
- Console.WriteLine($ “Value from generic message: {x}”);
- }
- MyGeneric < string > (“Next Year”);
- MyGeneric < int > (2021);
- Console.ReadKey();
- }
- }
Output Value from the generic message: Next Year Value from the generic message: 2021 You can also reference out parameters in local functions that are declared in the same scope
- class Program {
- static void Main(string[] args) {
- void MyOut(string x, out string s) {
- s = $ “Value from generic message using out parameter: {x}”;
- }
- string message = null;
- MyOut(“Next Year is 2021”, out message);
- Console.WriteLine($ “The value of out message: {message}”);
- Console.ReadKey();
- }
- }
Output The value of our message: Value from the generic message using the out parameter: The Next Year is 2021.Local Functions can be used with params.
- class Program {
- static void Main(string[] args) {
- void MyParams(params int[] array) {
- foreach(int element in array) {
- Console.WriteLine($ “Display the elements from paramArray: {element}”);
- }
- }
- int[] paramArray = new int[] {
- 1,
- 3,
- 5,
- 7,
- 9,
- 11,
- 13
- };
- MyParams(paramArray);
- Console.ReadKey();
- }
- }
Output Display the elements from paramArray: 1
Display the elements from paramArray: 3
Display the elements from paramArray: 5
Display the elements from paramArray: 7
Display the elements from paramArray: 9
Display the elements from paramArray: 11
Display the elements from paramArray: 13
We inspected use cases for local functions in C#. I hope this has been informative to you and you have found what you were looking for.