Codeproject评分:

1. 递归方法一直会调用自己直到某些条件被满足

2. 递归方法会有一些参数，而它会把一些新的参数值传递给自己。

“写任何一个程序可以用赋值和if-then-else语句表示出来，而while语句则可以用赋值、if-then-else和递归表示出来。”（出自Ellis Horowitz的《数据结构基础（C语言版）》 - Fundamentals of Data Structure in C）

1. 阶乘

0! = 1
1! = 1
2! = 2 * 1! = 2
3! = 3 * 2! = 6
...
n! = n * (n - 1)!

public long Factorial(int n)
{
if (n == 0)
return 1;
long value = 1;
for (int i = n; i > 0; i--)
{
value *= i;
}
return value;
}

public long Factorial(int n)
{
if (n == 0)//限制条件，对该方法调用自己做了限制
return 1;
return n * Factorial(n - 1);
}

2. Fibonacci数列

Fibonacci数列是按以下顺序排列的数字：

0,1,1,2,3,5,8,13,21,34,55,…

public long Fib(int n)
{
if (n < 2)
return n;
long[] f = new long[n+1];
f[0] = 0;
f[1] = 1;

for (int i = 2; i <= n; i++)
{
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}

public long Fib(int n)
{
if (n == 0 || n == 1) //满足条件
return n;
return Fib(k - 2) + Fib(k - 1);
}
3. 布尔组合

true, true, true
true, true, false
true, false, true
true, false, false
false, true, true
false, true, false
false, false, true
false, false, false

public void CompositionBooleans(string result, int counter)
{
if (counter == 0)
return;

bool[] booleans = new bool[2] { true, false };

for (int j = 0; j < 2; j++)
{
StringBuilder stringBuilder = new StringBuilder(result);
stringBuilder.Append(string.Format("{0} ", booleans[j].ToString())).ToString();

if (counter == 1)
Console.WriteLine(stringBuilder.ToString());

CompositionBooleans(stringBuilder.ToString(), counter - 1);
}
}

CompositionBoolean(string.Empty, 3);

public void BooleanCompositions(int count)
{
BooleanCompositions(count - 1, "true");
BooleanCompositions(count - 1, "false");
}

private void BooleanCompositions(int counter, string partialOutput)
{
if (counter <= 0)
Console.WriteLine(partialOutput);
else
{
BooleanCompositions(counter - 1, partialOutput+ ", true");
BooleanCompositions(counter - 1, partialOutput+ ", false");
}
}

4. 获取内部异常

public Exception GetInnerException(Exception ex)
{
return (ex.InnerException == null) ? ex : GetInnerException(ex.InnerException);
}

return (ex.InnerException == null) ? ex : GetInnerException(ex.InnerException);

if (ex.InnerException == null)//限制条件
return ex;
return GetInnerException(ex.InnerException);//用内部异常作为参数调用自己

try
{
throw new Exception("This is the exception",
new Exception("This is the first inner exception.",
new Exception("This is the last inner exception.")));
}
catch (Exception ex)
{
Console.WriteLine(GetInnerException(ex).Message);
}

5. 查找文件

private Dictionary<string, string> errors = new Dictionary<string, string>();
private List<string> result = new List<string>();

private void SearchForFiles(string path)
{
try
{
foreach (string fileName in Directory.GetFiles(path))//Gets all files in the current path
{
}

foreach (string directory in Directory.GetDirectories(path))//Gets all folders in the current path
{
SearchForFiles(directory);//The methods calls itself with a new parameter, here!
}
}
catch (System.Exception ex)
{
errors.Add(path, ex.Message);//Stores Error Messages in a dictionary with path in key
}
}

James MaCaffrey博士认为尽量不要使用递归，除非实在没有办法。你可以读一下他的文章

A) 如果性能是非常重要的，请避免使用递归

B)如果递推方式不是很复杂的，请避免使用递归

C) 如果A和B都不满足，请不要犹豫，用递归吧。