Visual C# 2005 Guide
Recursive Procedures

A procedure is recursive if the procedure contains a call to itself. This is perfectly legitimate in C# and most other high-level languages. A characteristic of recursive procedures is that there must be a stopping condition that can always be reached by the procedure.

The following procedure will calculate the factorial of a number (3! = 3 x 2 x 1 = 6) using a recursive technique.

static int RecursiveFactorial(int num)
{
   if (num == 1)
{
      return 1;
   }
   else
   {
      return num * RecursiveFactorial(num - 1);
   }
}

There is an obvious danger with recursion. In a more complex function, you really need to make sure that there is always stopping condition.

The need to use a stack to store return values means that recursive techniques can be heavy on the memory use. A stack is only so big and a stack overflow would cause the program to crash.

Non-recursive solutions are therefore likely to be more efficient when it comes to processing time and storage. However, there are still reasons why recursion is a desirable feature in a programming language. In some cases, the code is easier to write and understand for the programmer than an iterative solution. This, in some cases, is more important than performance.