Today we will learn something cool feature called Local Functions in our newly upgraded version of C# I.e., 7.0.

As we already know about Public and Private functions in our C# language. So, what exactly these local functions are? And how it is helpful? When should we use this feature?

Just recall what is public and private functions before digging in to local functions.

  • Public Functions are the accessible everywhere wherever respective class is accessed.
  • Private Functions are accessible only at class level functions and it cannot be visible or accessible to outer world.

Now, it’s time to know more about local Functions. Local Functions are used within the given function/method.

Where we can use these Local Functions?

Functions/Methods which contain a repeated set of statements need to execute but the scope of these functionality is set to this parent function.

For example, let’s take simple example of calculator suppose we have a method called calculate with input as a string like [1+(2*5)/10]. Here in this string you might expect many additions, multiplications and divisions.

As we know all these calculations are used only in this Calculate() method and no use at any part of the class. You can make these as other private functions and use these functions in Calculate method. But here we need to consider two points

  • These methods are nowhere useful except in our Calculate method.
  •  Unnecessarily overloading the stack with these method calls.

Local Functions will overcome these issues, as when we call Local functions CLR won’t push any method call to the calling task as this is part of the parent method itself.

Following are the example program which presents what we discussed in the above.

public static string Calculate(string expression) //[1+(2*5)/10]
        {
            string result = string.Empty;

            //Step-1: Assume here we decrypted the given expression
            //Step-2: based on decryption call the appropriate local methods

            //Following are the Local Functions who's scope is this method
            T Addition<T>(T a, T b)
            {
                return (a + b);
            }

            T Substration<T>(T a, T b)
            {
                return (a - b);
            }

            T Multiplication<T>(T a, T b)
            {
                return (a * b);
            }
            return result;
        }

 

Advantages:

  • The scope of these functions is limited to its parent method.
  • No overhead of adding additional stack push for these method calls when called as these calls are part of parent call.
  • Can use generic functions.

Note:

We can even achieve similar functionality in older versions of C# via. Func and Action but here we have some disadvantages like while calling more memory utilization and also it won’t allow using generics.

Hope you got some help in understanding this article about Local Functions. Please don’t forget to throw a comment which makes me more energetic to provide stuff here. 🙂

 

Happy Coding 🙂