Recursion with Func
Posted
by
David in Dakota
on Stack Overflow
See other posts from Stack Overflow
or by David in Dakota
Published on 2011-01-06T03:47:46Z
Indexed on
2011/01/06
3:53 UTC
Read the original article
Hit count: 269
Is it possible to do recursion with an Func delegate? I have the following, which doesn't compile because the name of the Func isn't in scope...
Func<long, long, List<long>, IEnumerable<long>> GeneratePrimesRecursively = (number, upperBound, primeFactors) =>
{
if (upperBound > number)
{
return new List<long>();
}
else
{
if (!primeFactors.Any(factor => number % factor == 0)) primeFactors.Add(number);
return GeneratePrimesRecursively(++number, upperBound, primeFactors); // breaks here.
}
};
© Stack Overflow or respective owner