- C# 7 and .NET Core Cookbook
- Dirk Strauss
- 190字
- 2021-07-03 00:11:53
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.
- Extending Jenkins
- Photoshop智能手機APP UI設計之道
- 跟老齊學Python:輕松入門
- Java 9 Programming Blueprints
- 新手學Visual C# 2008程序設計
- Mastering Ubuntu Server
- Mastering Scientific Computing with R
- Python程序設計
- Quarkus實踐指南:構建新一代的Kubernetes原生Java微服務
- STM32F0實戰:基于HAL庫開發
- Python Data Structures and Algorithms
- SQL 經典實例
- App Inventor創意趣味編程進階
- Learning YARN
- Learning Material Design