官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 丰县| 饶平县| 安福县| 兴城市| 邻水| 屏东县| 莱西市| 仪陇县| 昌吉市| 温州市| 望谟县| 景宁| 肃南| 买车| 晋中市| 塔河县| 南汇区| 丽水市| 弋阳县| 寻乌县| 瓦房店市| 鄂托克旗| 康定县| 东安县| 新乡市| 宝应县| 建瓯市| 黄浦区| 济阳县| 郯城县| 嫩江县| 万安县| 福贡县| 邛崃市| 绥江县| 甘孜县| 兰溪市| 招远市| 崇信县| 大悟县| 徐水县|