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

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.

主站蜘蛛池模板: 隆德县| 高邮市| 新竹市| 灌南县| 陵川县| 乃东县| 台前县| 河西区| 饶河县| 河北区| 南京市| 长海县| 白朗县| 浏阳市| 修武县| 逊克县| 建湖县| 四子王旗| 通州区| 石屏县| 漳平市| 舟山市| 和静县| 凌云县| 贡山| 盐源县| 宜城市| 海兴县| 大方县| 麻阳| 沂水县| 盐津县| 武清区| 喀喇沁旗| 崇州市| 故城县| 鄂托克旗| 湘阴县| 莎车县| 石林| 云梦县|