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

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.

主站蜘蛛池模板: 额济纳旗| 巧家县| 柏乡县| 新营市| 禹州市| 宁陵县| 婺源县| 延庆县| 夹江县| 义乌市| 仁布县| 瓦房店市| 新野县| 昌平区| 定安县| 巨野县| 洛南县| 甘南县| 蓬溪县| 正镶白旗| 信阳市| 丹江口市| 克拉玛依市| 婺源县| 绍兴县| 江都市| 湘潭市| 梁山县| 彭泽县| 庐江县| 泸水县| 贡嘎县| 宁南县| 松潘县| 鹿邑县| 遵化市| 浪卡子县| 乌兰县| 宜州市| 浦县| 孟州市|