We all know that it’s good practice to create small methods that promote reuse, which inevitably will cause lots of methods being placed on the stack. However is it possible to reach the scenario where there are so many nested methods calls that a StackOverflow Exception occurs?
Would the accepted solution to be simply increase the stack size?
The documentation states that a such an exception will occur during “very deep or unbounded recursion” so it certainly seems possible, or does the .NET framework dynamically handle stack size for us?
My question can be summed up like so:
Is it possible to have such a well designed program (in
terms of small reusable methods) that is becomes necassary to increase the
stack size and hence use more resources?
Not really. I just did a very quick test, and a StackOverflowException occurs after 15,000 nested calls.
There’s no way you’ll be writing code that will non-recursively nest 15,000 times due to the sheer number of methods you have.
Obviously the exact number depends on many function-local variables you have allocated on the stack. But whatever that actual number may be, it is nowhere near enough to do what you’re suggesting.