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

Creating Local Functions

Functions that are created within a function are known as Local Functions. These are mainly used when defining helper functions that have to be in the scope of the function itself. The following example shows how the factorial of the number can be obtained by writing a Local Function and calling it recursively:

static void Main(string[] args) 
{ 
  Console.WriteLine(ExecuteFactorial(4));          
} 
 
static long ExecuteFactorial(int n) 
{ 
  if (n < 0) throw new ArgumentException("Must be non negative", 
nameof(n)); else return CheckFactorial(n); long CheckFactorial(int x) { if (x == 0) return 1; return x * CheckFactorial(x - 1); } }
主站蜘蛛池模板: 梁山县| 湘乡市| 庆城县| 革吉县| 阿拉善右旗| 龙岩市| 武清区| 元谋县| 平凉市| 江陵县| 浪卡子县| 项城市| 来凤县| 乌拉特后旗| 玉山县| 庆阳市| 平武县| 酉阳| 那曲县| 西乌珠穆沁旗| 南昌县| 温州市| 磐石市| 高陵县| 双牌县| 永丰县| 明光市| 张家界市| 侯马市| 东乡族自治县| 扶风县| 柳河县| 涡阳县| 南投市| 黄大仙区| 繁昌县| 兰考县| 廉江市| 尼玛县| 凤凰县| 昌平区|