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

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); } }
主站蜘蛛池模板: 雷波县| 子洲县| 连州市| 万源市| 英吉沙县| 徐闻县| 福鼎市| 沽源县| 开封县| 嘉祥县| 泸溪县| 莱芜市| 左贡县| 九龙县| 五家渠市| 永济市| 北川| 陇西县| 松滋市| 中西区| 当阳市| 开化县| 襄樊市| 宜兴市| 巴林右旗| 黔西| 图们市| 静宁县| 曲靖市| 巴塘县| 株洲县| 平武县| 页游| 墨江| 张家口市| 门源| 正阳县| 安新县| 灵台县| 夏河县| 民权县|