Recursion in C#

Last Updated : 16 Oct, 2026

Recursion is a process in which a method calls itself directly or indirectly. The method that calls itself is called a recursive method. Recursion is useful for solving problems by breaking them into smaller sub-problems.

  • A recursive method must have a base condition to stop the recursion.
  • Recursion is commonly used in problems such as factorial, Fibonacci, tree traversal, and DFS.
frame_1000001916
Recursion Working

Syntax

returnType MethodName(parameters){
if (baseCondition)
return result;
return MethodName(updatedParameters);
}

Base Condition in Recursion

The base condition is the condition that stops a recursive method from calling itself further.

  • It prevents the recursion from continuing indefinitely.
  • The recursive calls continue until the base condition is reached.

Example: The following example calculates the factorial of a number using a base condition.

C#
using System;

class Program {
    // Recursive method to calculate factorial
    static int Factorial(int n)
    {
        if (n <= 1)
            return 1;

        return n * Factorial(n - 1);
    }

    static void Main()
    {
        int num = 5;

        int result = Factorial(num);

        Console.WriteLine("Factorial of " + num + " = "
                          + result);
    }
}

Output
Factorial of 5 = 120

Examples of Recursion

1. Factorial Using Recursion

The following example calculates the factorial of 3, 4, and 5 using recursion.

C#
using System;

class GFG
{
    static int Factorial(int n)
    {
        if (n == 1)
            return 1;

        return n * Factorial(n - 1);
    }

    static void Main()
    {
        Console.WriteLine("Factorial of 3 is " + Factorial(3));
        Console.WriteLine("Factorial of 4 is " + Factorial(4));
        Console.WriteLine("Factorial of 5 is " + Factorial(5));
    }
}

Output
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120

Explanation

  • The Factorial() method calls itself with n - 1 until n becomes 1, which is the base condition.
  • Each recursive call calculates n × Factorial(n - 1) and returns the result to the previous call.
  • For example, Factorial(5) is calculated as 5 × 4 × 3 × 2 × 1 = 120.

2. Fibonacci Series

The Fibonacci series is a sequence where each number is the sum of the previous two numbers.

fibonnacci-series
Fibonacci Series
C#
using System;

class GFG
{
    static int Fib(int n)
    {
        if (n == 0 || n == 1)
            return n;

        return Fib(n - 1) + Fib(n - 2);
    }

    static void Main()
    {
        Console.WriteLine("Fibonacci of 3 is " + Fib(3));
        Console.WriteLine("Fibonacci of 4 is " + Fib(4));
        Console.WriteLine("Fibonacci of 5 is " + Fib(5));
    }
}

Output
Fibonacci of 3 is 2
Fibonacci of 4 is 3
Fibonacci of 5 is 5

Explanation

  • The Fib() method calls itself with n - 1 and n - 2 until n becomes 0 or 1, which is the base condition.
  • Each recursive call adds the previous two Fibonacci values to calculate the current value.
  • For example, Fib(5) is calculated as Fib(4) + Fib(3) = 3 + 2 = 5.

Stack Overflow Error

If the base condition is not reached or is incorrectly defined, a StackOverflowException can occur.

Example: The following example has an incorrect base condition, so the recursive calls never reach 100.

C#
static int Fact(int n)
{
    if (n == 0 || n == 1)
        return 1;

    return n * Fact(n - 1);
}

Output

StackOverflowException

Memory Allocation in Recursion

Each recursive method call creates a new stack frame containing its local variables and execution information.

  • A new stack frame is created for every recursive call.
  • When the base condition is reached, the calls return one by one and their stack frames are removed.

Example: The following program prints numbers before and after the recursive call.

C#
using System;

class GFG
{
    static void PrintFun(int test)
    {
        if (test < 1)
            return;

        Console.Write(test + " ");

        PrintFun(test - 1);

        Console.Write(test + " ");
    }

    static void Main()
    {
        PrintFun(3);
    }
}

Output
3 2 1 1 2 3 

Explanation: PrintFun(3) calls PrintFun(2), which calls PrintFun(1), and finally PrintFun(0). After reaching the base condition, the methods return in reverse order.

How Recursion Works?

When a method calls itself:

  • Each call is stored in stack memory (call stack)
  • New calls are added (stack grows)
  • When base condition is reached: calls start returning (stack shrinks)

The following example demonstrates both the descending phase (going deeper into recursion) and the ascending phase (returning back from recursion):

C#
using System;

class Program
{
    static void F(int n)
    {
        Console.WriteLine($"F({n})'s Stack Frame Pushed");

        if (n > 1)
        {
            F(n - 1);
            F(n - 1);
        }

        Console.WriteLine($"F({n})'s Stack Frame Removed");
    }

    static void Main()
    {
        F(3);
    }
}

Output:

F(3)'s Stack Frame Pushed
F(2)'s Stack Frame Pushed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(2)'s Stack Frame Removed
F(2)'s Stack Frame Pushed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(2)'s Stack Frame Removed
F(3)'s Stack Frame Removed

Below is the illustration of function call stack of the above code:

Applications of Recursion

Recursion is commonly used in:

  • Factorial and Fibonacci problems
  • Searching & Sorting (Binary Search, QuickSort)
  • Tree and Graph Traversal
  • Backtracking problems
  • Dynamic Programming
Comment

Explore