codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

16 / 60 topics
13Methods in C#14Parameters and Arguments15Return Values from Methods16Recursion in C#
Tutorials/C# Programming/Recursion in C#
🔷C# Programming

Recursion in C#

Updated 2026-05-15
10 min read

Recursion in C#

Introduction

In the world of programming, recursion is a powerful concept where a method calls itself to solve smaller instances of the same problem. This technique is particularly useful for problems that can be broken down into simpler, repetitive tasks. In this tutorial, we will explore how recursion works in C#, understand its benefits and potential pitfalls, and see it in action through practical examples.

Concept

Recursion involves a method making one or more calls to itself. Each recursive call typically operates on a smaller part of the problem until reaching a base case, which is a condition that stops further recursion. The base case prevents infinite loops and ensures that the recursion eventually terminates.

The general structure of a recursive method includes:

  1. Base Case: A condition that determines when the recursion should stop.
  2. Recursive Case: The part of the method where it calls itself with modified arguments to solve smaller subproblems.

Examples

Let's dive into some examples to understand how recursion works in C#.

Example 1: Factorial Calculation

The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. It can be defined recursively as:

  • Base Case: factorial(0) = 1
  • Recursive Case: factorial(n) = n * factorial(n-1)

Here's how you can implement this in C#:

csharp
1using System;
2
3class Program
4{
5 static void Main()
6 {
7 int number = 5;
8 Console.WriteLine($"Factorial of {number} is {Factorial(number)}");
9 }
10
11 static int Factorial(int n)
12 {
13 if (n == 0) // Base Case
14 return 1;
15 else
16 return n * Factorial(n - 1); // Recursive Case
17 }
18}

When you run this program, the output will be:

Output
Factorial of 5 is 120

Example 2: Fibonacci Sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. It can be defined recursively as:

  • Base Case: fibonacci(0) = 0, fibonacci(1) = 1
  • Recursive Case: fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Here's how you can implement this in C#:

csharp
1using System;
2
3class Program
4{
5 static void Main()
6 {
7 int number = 7;
8 Console.WriteLine($"Fibonacci of {number} is {Fibonacci(number)}");
9 }
10
11 static int Fibonacci(int n)
12 {
13 if (n == 0) // Base Case
14 return 0;
15 else if (n == 1) // Base Case
16 return 1;
17 else
18 return Fibonacci(n - 1) + Fibonacci(n - 2); // Recursive Case
19 }
20}

When you run this program, the output will be:

Output
Fibonacci of 7 is 13

Example 3: Directory Traversal

Recursion can also be used to traverse directories and subdirectories. Here's an example that lists all files in a directory and its subdirectories:

csharp
1using System;
2using System.IO;
3
4class Program
5{
6 static void Main()
7 {
8 string path = @"C:YourDirectoryPath";
9 TraverseDirectory(path);
10 }
11
12 static void TraverseDirectory(string path)
13 {
14 try
15 {
16 foreach (string dir in Directory.GetDirectories(path))
17 {
18 Console.WriteLine($"Entering directory: {dir}");
19 TraverseDirectory(dir); // Recursive call for subdirectories
20 }
21 foreach (string file in Directory.GetFiles(path))
22 {
23 Console.WriteLine($"File: {file}");
24 }
25 }
26 catch (Exception ex)
27 {
28 Console.WriteLine($"Error: {ex.Message}");
29 }
30 }
31}

This example demonstrates how recursion can be used to navigate through a directory structure.

What's Next?

Now that you have a good understanding of recursion in C#, the next step is to explore Classes and Objects. Classes are blueprints for creating objects, which encapsulate data and methods that operate on that data. Understanding classes and objects will help you build more complex and organized applications.

Keep practicing with different recursive problems to enhance your skills and deepen your understanding of this powerful programming concept.


PreviousReturn Values from MethodsNext Classes and Objects

Recommended Gear

Return Values from MethodsClasses and Objects