The use of local functions might seem a little strange at first. They are in fact quite often used in most functional languages. C# 7.0 now allows us to do the same. So what exactly is a local function? Well, think of it as a helper method for a specific method. This helper method only really makes sense when used from the specific method and will not be useful for other methods in your application. It, therefore, makes sense to use it inside your existing method. Some might think that an extension method might be just as well suited, but extension methods should really be used to extend the functionality of many other methods. The usefulness of local functions will become evident in the following code example.
Local functions
Getting ready
There is nothing you need to specifically get ready or set up beforehand to be able to use local functions. To illustrate the use of local functions, I will create a method that calculates the floor space of a building after the common area space has been subtracted from the total floor space.
How to do it...
- Create a method called GetShopfloorSpace() that takes three parameters: for the common area space, the building width, and the building length.
public Building GetShopfloorSpace(int floorCommonArea,
int buildingWidth, int buildingLength)
{
}
- We are returning a Building type, so create a class called Building that has a single property called TotalShopFloorSpace.
public class Building
{
public int TotalShopFloorSpace { get; set; }
}
- Our local function will simply take the width and length of the building to calculate the total floor area and then subtract the common area from that to get the usable floor space for shops. The local function will look as follows:
int CalculateShopFloorSpace(int common, int width, int length)
{
return (width * length) - common;
}
- This is where it gets interesting. Add the local function inside the GetShopfloorSpace() method and add the rest of the code in the following code example:
public Building GetShopfloorSpace(int floorCommonArea,
int buildingWidth, int buildingLength)
{
Building building = new Building();
building.TotalShopFloorSpace = CalculateShopFloorSpace(
floorCommonArea, buildingWidth, buildingLength);
int CalculateShopFloorSpace(int common, int width, int length)
{
return (width * length) - common;
}
return building;
}
- In the calling code, inside the static void Main method, call the method as follows:
Chapter1 ch1 = new Chapter1();
Building bldng = ch1.GetShopfloorSpace(200, 35, 100);
WriteLine($"The total space for shops is
{bldng.TotalShopFloorSpace} square meters");
- Run your console application and see the output displayed as follows:
How it works...
The beauty of local functions is that you can call them from anywhere inside your method. To illustrate this, add the following line of code just before the return statement of the GetShopfloorSpace() method. This essentially overrides whatever we passed to the method initially.
building.TotalShopFloorSpace = CalculateShopFloorSpace(10, 9, 17);
The modified method will now look like this:
public Building GetShopfloorSpace(int floorCommonArea, int buildingWidth, int buildingLength)
{
Building building = new Building();
building.TotalShopFloorSpace = CalculateShopFloorSpace(
floorCommonArea, buildingWidth, buildingLength);
int CalculateShopFloorSpace(int common, int width, int length)
{
return (width * length) - common;
}
building.TotalShopFloorSpace = CalculateShopFloorSpace(10, 9, 17);
return building;
}
Run your console application again. This time you will see that the values are totally different. The second call to the local function overrode the first call and illustrates that the local function can be called throughout the method containing it.
I can think of a few instances where I might have been able to use this in the past. It isn't something I think that I'll use often. It is however a very nice addition to the C# language and great that it is available to developers.