Lesson 11 of 34 · Methods
Recursion in C#: Methods That Call Themselves
A recursive method is a method that calls itself. That sounds like a trick that could never end — and if you write it carelessly, it doesn't. Written well, recursion turns problems that contain smaller copies of themselves (folders inside folders, branches of a tree, a number made of digits) into a few elegant lines.
The two parts of every recursive method
Factorial is the classic example: 5! = 5 × 4!, and 4! = 4 × 3!, and so on down to 0! = 1.
public static long Factorial(int n)
{
if (n == 0)
{
return 1; // base case: no further calls
}
return n * Factorial(n - 1); // recursive case: a smaller problem
}
- The base case answers the smallest version of the problem directly.
- The recursive case reduces the problem and calls the method again, trusting it to solve the smaller version.
Each call must move towards the base case. If it does not, the method calls itself forever.
What happens on the call stack
Every method call gets a frame on the call stack holding its parameters and local variables. Factorial(3) waits for Factorial(2), which waits for Factorial(1), which waits for Factorial(0). When Factorial(0) returns 1, the waiting calls finish one after another: 1 × 1, then 2 × 1, then 3 × 2 = 6.
Factorial(3)
└ 3 * Factorial(2)
└ 2 * Factorial(1)
└ 1 * Factorial(0) → 1
The stack is not infinite. A recursion without a working base case fills it up and crashes the program with a stack overflow — in this course the runtime reports "most likely a stack overflow caused by infinite recursion" and restarts itself. Very deep recursion (hundreds of thousands of calls) can overflow even when it is correct; loops have no such limit.
Recursion with numbers and strings
The last digit of n is n % 10, and n / 10 drops it. That makes digit problems naturally recursive:
public static int CountDigits(int n)
{
if (n < 10) return 1;
return 1 + CountDigits(n / 10);
}
Strings shrink too — s.Substring(1) is everything except the first character:
public static int CountChar(string s, char c)
{
if (s.Length == 0) return 0;
int here = s[0] == c ? 1 : 0;
return here + CountChar(s.Substring(1), c);
}
Recursion or a loop?
Anything recursive can also be written with a loop, and loops are usually faster. Recursion shines when the data itself is recursive — nested folders, menus inside menus, game trees — or when the recursive version is much clearer. Beware of recursions that repeat work: the naive Fib(n) = Fib(n - 1) + Fib(n - 2) recomputes the same values exponentially often and becomes painfully slow around n = 40.
Common mistakes
- Missing or unreachable base case → stack overflow.
- Not shrinking the problem:
return n * Factorial(n);never gets closer to 0. - Forgetting to use the result:
Factorial(n - 1);on its own computes a value and throws it away.
Assignment
Write these methods recursively (no loops):
Power(baseValue, exponent)returnsbaseValueraised toexponent(exponent≥ 0).Power(2, 10)is1024, and anything to the power0is1.SumDigits(n)returns the sum of the digits of a non-negative number:SumDigits(4096)is19.Reverse(text)returns the text backwards:Reverse("forge")is"egrof".IsPalindrome(text)returnstruewhen the text reads the same in both directions:"level","noon","a"and""are palindromes;"forge"is not.
Premium lesson
Unlock the full Learn C# course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Recursion in other languages
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .