I do recursion to find a long value within a List with multiple children that also can have children.
following method:
public TaxonomyData getTaxonomyData(long taxId, List<TaxonomyData> TaxonomyTree, TaxonomyData output)
{
//find taxid in the taxonomy tree list and return the taxonomydata
foreach (TaxonomyData td in TaxonomyTree)
{
if (td.TaxonomyId == taxId)
{
output = td;
//return td; => when doing a return here means I already found a match so it is not necessary to do all the recursion.
}
else if (td.Taxonomy.Length > 0)
{
getTaxonomyData(taxId, td.Taxonomy.ToList(), output);
}
}
return output;
}
Is it possible when I do return td; (see commented row) that my whole recursion stops?
Thanks
I suspect you want something like:
Each
returnonly returns one level, but by checking the return value in theelseclause, we can return all the way up the stack when we find the right value.The final result returned to the caller will be a null reference if it hasn’t been found.
Note that I’ve removed the “output” parameter, which wouldn’t have been effective anyway as it wasn’t a
refparameter, and isn’t as clear as just using the return value.