Book Image

C# 7 and .NET Core Cookbook - Second Edition

Book Image

C# 7 and .NET Core Cookbook - Second Edition

Overview of this book

C# has recently been open-sourced and C# 7 comes with a host of new features for building powerful, cross-platform applications. This book will be your solution to some common programming problems that you come across with C# and will also help you get started with .NET Core 1.1. Through a recipe-based approach, this book will help you overcome common programming challenges and get your applications ready to face the modern world. We start by running you through new features in C# 7, such as tuples, pattern matching, and so on, giving you hands-on experience with them. Moving forward, you will work with generics and the OOP features in C#. You will then move on to more advanced topics, such as reactive extensions, Regex, code analyzers, and asynchronous programming. This book will also cover new, cross-platform .NET Core 1.1 features and teach you how to utilize .NET Core on macOS. Then, we will explore microservices as well as serverless computing and how these benefit modern developers. Finally, you will learn what you can do with Visual Studio 2017 to put mobile application development across multiple platforms within the reach of any developer.
Table of Contents (17 chapters)

Local functions

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.

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...

  1. 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)
{

}
  1. 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; }
}
  1. 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;
}
  1. 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;
}
  1. 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");
  1. 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.