Method with a local function - CSharp Custom Type

CSharp examples for Custom Type:Local Method

Description

Method with a local function

Demo Code

using static System.Console;
using System;//from   w  w w . ja va  2 s . co m
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program
{
   static void Main(string[] args)
   {
      WriteLine($"5! is {Factorial(5)}");
   }
   // method with a local function
   public static int Factorial(int number)
   {
      if (number < 0)
      {
         throw new ArgumentException($"{nameof(number)} cannot be less than zero.");
      }
      return localFactorial(number);
      int localFactorial(int localNumber)
      {
         if (localNumber < 1) return 1;
         return localNumber * localFactorial(localNumber - 1);
      }
   }
}

Result


Related Tutorials