In this article I am going to discuss about C# recursion with examples but before that if you had not read my previous article of C# call by value and call by reference to understand the functions more clearly. Many of times you must heard that to solve this problem using recursive function or recursion which is another approach for solving problem in less lines of code(LoC). So let start with C# recursion introduction.

Introduction to C# Recursion

In any programming language including C#, recursion refers to the method which calls itself repeatedly until the specified condition not met. Recursion is same like loops but the only difference between loops and recursion is that loop will executed until the condition is not false and in recursion method will call itself only if the given condition is true else it will not call itself. In recursion we use recursive functions to acheive the recursion. Let see one program to see the working of recursion.

C# Program to Explain C# Recursion

using System;
namespace FactorialUsingRecursion
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the Number to Find Factorial : ");
            int number = Convert.ToInt32(Console.ReadLine());
            double factorial = Factorial(number);
            Console.WriteLine("Factorial of "+number+ " = "+factorial.ToString());
        }
        public static double Factorial(int number)
        {
            if (number == 0|| number==1){
                return 1;
            }
            return number * Factorial(number-1);    //Recursive call
        

        }
    }
}
Output:
Enter the Number to Find Factorial : 
4
Factorial of 4 = 24

Working of Above C# Recursion Program

In above program we are finding the factorial of number using recursion. The same program we can also do with iterative approach( using loops) but recursion makes our program more easier. Sometimes it is very difficult to solve the problem using iterative approach therefore recursive approach comes into existence to solve. For example : Tower or hanoi problem is very common problem that is very difficult to solve using iterative approach but using recursive function we can solve this function in 5 to 6 lines of code. Therefore recursion is very important part of any programming language. Now let see the working of above program.

1. In above program we have created one static function named factorial which will take number as parameter and returning the double value.

2. Most of the times every recursion program has two conditions. One is base condition and other condition is that where the function will call itself repeatedly. Same like in above program there is one base condition which is if number is equal to 0 then return 1 because factorial of 0 is also 1. Otherwise it will go to else condition.

3. And the other condition is that where the function will call itself repeatedly. Let see the working of else condition with table.

S.NoNumberBase Condition ResultElse Condition
1.44==0|| 4==1 which is False4*Factorial(4-1)

Now in else condition we are again calling the factorial function but this time the number will be 4-1 which is 3. So now the number is 3.

S.NoNumberBase Condition ResultElse Condition
2.33==0 || 3==1 which is False3* Factorial(3-1)

Now in else condition we are again calling the factorial function but this time the number will be 3-1 which is 2. So now the number is 2.

S.NoNumberBase Condition ResultElse Condition
3.22==0 || 2==1 which is false2*Factorial(2-1)
4.11==0|| 1==1 which is true

Now this time the number will be 2-1 which is 1 and this time base condition will be true because our number is 1 and condition is if number is equal to 0 or equal to 1 then return 1. Now we clear if the number is 1 then factorial is 1. So Factorial(1) is equal to 1. Simply put the value of 1 in function.

S.NoNumberFunction ConditionFunction condition after evaluationReplace function with factorial ValueResult
5.22*Factorial(2-1)2*Factorial(1)2*12

So We get the Value of 2*Factorial(2-1) which is equal to 2. Now put the value of this to find the result of next number.

S.NoNumberFunction ConditionFunction Conditon after evaluationReplace function with factorial valueResult
6.33*Factorial(3-1)3*Factorial(2)3*26

So we get the value of 3* Factorial(3-1) which is equal to 6. Now put the value of this to find the result of next number.

S.NoNumberFunction ConditionFunction Condition after evaluationReplace function with factorial valueResult
7.44*Factorial(4-1)4*Factorial(3)4*624

Now we get the value of 4*Factorial(4-1) which is 24 and its corresponding number is 4 that means the factorial of number 4 is 24. So you can see we have find the factorial of 4 in 7 steps using recursion. Hope you clear how to trace the working of recursion in csharp.

Difference Between Iteration and Recursion

C# difference between iteration and recursion.

Conclusion

In this C# tutorials article we had discussed about C# recursion with examples and also discusses the difference between iteration and recursion so you can clearly understand what is the usecase of recursion. Hope you found this article helpful and like this article. In my next article I am going to discuss about command line argument in csharp programming.