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

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.

主站蜘蛛池模板: 德州市| 大埔县| 镇雄县| 绥芬河市| 溧水县| 苏州市| 霍山县| 广昌县| 广丰县| 望都县| 米泉市| 新晃| 中宁县| 北海市| 奉化市| 永和县| 甘德县| 宜州市| 古田县| 和平县| 库尔勒市| 绥江县| 东丽区| 长沙市| 漾濞| 萝北县| 德格县| 四子王旗| 峨山| 闸北区| 渭南市| 图片| 团风县| 兴海县| 丽江市| 普安县| 南涧| 宁蒗| 齐河县| 南漳县| 腾冲县|