我对这段代码有问题,代码需要打印这个结果:
56789 56789
但结果是:99999 99999
为什么?
class Program
{
static int m_k = 0;
static Func<int>[] P=new Func<int>[5];
static void F(int n,Func<int>[] T)
{
if (n>0)
{
P[m_k] = () => T[m_k]();
m_k++;
F(n-1,T);
}
else
{
P[m_k] = () => T[m_k]();
}
}
static void Main(string[] args)
{
Func<int>[] T = new Func<int>[5];
Func<int>[] S = new Func<int>[5];
T[0] = () => 5;
T[1] = () => 6;
T[2] = () => 7;
T[3] = () => 8;
T[4] = () => 9;
for (int i = 0; i < 4; i++)
{
S[i] = () => T[i]();
}
for (int i = 0; i < 4; i++)
{
Console.WriteLine(S[i]());
}
F(4,T);
for (int i = 0; i < 4; i++)
{
Console.WriteLine(P[i]());
}
Console.ReadKey();
}
}