{{announcement.body}}
{{announcement.title}}

# Local Functions in C# 7.0

DZone 's Guide to

# Local Functions in C# 7.0

### C# 7.0 brought the introduction of local functions. We take a look at what they are exactly, how to use them, and some considerations to keep in mind.

· Web Dev Zone ·
Free Resource

Comment (1)

Save
{{ articles[0].views | formatCount}} Views

One of the new features of C# 7.0 is support for local functions. Local functions are methods that are defined inside other methods to simplify more complex code and by example to support local recursion. This blog post shows how to use local functions in C# 7.0 and gives some advice in context of technical design of code.

To illustrate local functions I will use the example of finding a factorial.

``````class Program
{
static void Main(string[] args)
{
var i = 6;           int Factorial(int n)
{
if (n == 0)
return 1;
else
return n * Factorial(n - 1);
}           Console.WriteLine(Factorial(i));
}
}``````

Now, one may ask why I use a recursive call for a factorial as it is not as efficient as using a for-loop Let’s move to for-loop.

``````class Program
{
static void Main(string[] args)
{
var i = 6;           int Factorial(int n)
{
var result = n;               for (var counter = n - 1; counter >= 1; counter--)
{
result = result * counter;
}               return result;
}           Console.WriteLine(Factorial(i));
}
}``````

Now we have a more effective method for finding factorials. But it is a simple for-loop. Why should we put it in a  local function?

I think it’s a good idea in terms of technical design. This for-loop has one concrete meaning – find the factorial – and thus this for-loop helps to communicate this fact to the developer who is working on the code after me.

### How to Organize the Code

Now let’s see where we should put local functions in our methods. What I suggest here is my personal opinion. The most important part of every method is the method body. So, I'll do this part first. As we name local functions with informative and descriptive names we can keep these functions in the bottom part of the method.

To illustrate my idea, the modified version of factorial example is given below.

``````class Program
{
static void Main(string[] args)
{
// method body
if(args.Length == 0)
{
Console.WriteLine("Missing argument: input value as integer");
return;
}           var i = int.Parse(args[0]);

Console.WriteLine(Factorial(i));
int Factorial(int n)
{
var result = n;               for (var counter = n - 1; counter >= 1; counter--)
{
result = result * counter;
}               return result;
}
}
}``````

Now, we have code organized and we can benefit from this little trick if we use it everywhere in our codebase where local functions are used.

### Some Considerations of Local Functions

Some things to keep in mind when considering local functions:

1. Local functions are part of methods where they are defined and they should not contain any system-wide logic.
2. Local functions are not visible to other classes and therefore they are tested as a unit with methods that own them.
3. Use local functions only if there is a real need for them.
4. Don’t over-use local functions as you may end up with methods that are too long and that are hard to test.

### Wrapping Up

Local functions are nice features in C# 7.0 and there are situations where local functions help us to write cleaner code. Still, local functions are not something to use to shorten or reorganize code that doesn’t need it. This can happen when working with spaghetti code. Consider if a local function is needed before going with it, and try it out if it seems like a solution. Be aware that local functions are not testable as they are considered to be the same local unit with one containing method.

Topics:
.net ,c# ,functions ,web dev

Comment (1)

Save
{{ articles[0].views | formatCount}} Views

Published at DZone with permission of Gunnar Peipman , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.