StackOverflowException
C# Programming Language
Severity: CriticalWhat Does This Error Mean?
StackOverflowException means your program ran out of call stack space. This almost always happens because a method calls itself — directly or indirectly — with no end condition. Each method call takes up a small amount of memory on the stack. When too many calls pile up, the stack overflows and the program crashes immediately. This error cannot be caught with try-catch — it kills the process.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Unity (C# scripting)
- ASP.NET
Common Causes
- A recursive method has a missing or unreachable base case, so it calls itself forever
- Two methods call each other in an endless cycle — A calls B, B calls A, and so on
- A property getter accidentally calls itself — for example, returning the property instead of the backing field
- An operator overload or implicit conversion creates an unintended recursive call
- Extremely deep (but not infinite) recursion on very large data structures, exceeding the stack limit
How to Fix It
-
Look at the stack trace in the crash output. You will see the same method name repeated hundreds of times. That is the method causing the recursion.
The repeating method name is your starting point. Open that method and look for where it calls itself.
-
Find the recursive call in that method. Then find the condition that is supposed to stop the recursion — the base case. Check if it is missing, incorrect, or never reached.
Every recursive method needs a clear exit condition. Example: if (n <= 0) return; — without this, it runs forever.
-
Check all properties in the involved classes. A common mistake is writing 'get { return MyProperty; }' instead of 'get { return _myField; }' — the property returns itself, creating infinite recursion.
Property getters that call themselves are a very common source of StackOverflowException.
-
Check for indirect recursion — where MethodA calls MethodB which calls MethodA. Draw out the call chain if needed to spot the cycle.
Indirect recursion is harder to spot but the stack trace will show both method names alternating repeatedly.
-
If your recursion is intentional but deep, convert the algorithm to use an explicit stack (a Stack<T> data structure) instead of the call stack. This eliminates the stack depth limit.
Iterative algorithms with an explicit stack can handle any depth. Tree traversal and graph search algorithms are often written this way.
When to Call a Professional
StackOverflowException usually points to a clear logical error in your code. Look at the stack trace before the crash — it will show the same method repeating many times. If you need genuinely deep recursion (thousands of levels), consider converting the recursive algorithm to an iterative one using an explicit stack. For complex recursive algorithms, a code review can help identify the missing base case.
Frequently Asked Questions
Why can I not catch StackOverflowException with try-catch?
StackOverflowException is special — it is so severe that .NET cannot safely handle it. When the stack overflows, the process does not have enough stack space to even run the catch block. In .NET, StackOverflowException terminates the process immediately. The only fix is to prevent it from happening in the first place by correcting the recursive logic.
How do I know if my recursion is the problem?
Look at the output window or event log for the crash. You will see the same method name listed over and over in the stack trace — sometimes hundreds of times. That repeated method is the one you need to fix. If the recursion is intentional, count how deep it could go and consider switching to an iterative approach.
Is recursion bad in C#?
Recursion is not bad — it is a valid and sometimes elegant technique. The key is always having a clear, reachable stopping condition. For shallow recursion (tens or low hundreds of levels), it is fine. For very deep recursion (thousands of levels), iterative approaches are safer and often faster. C# does not optimize tail recursion like some other languages do.