I am relatively new to recursion. I am running into a weird situation. As you can see below I am populating employees for a complex department. A complex department can have one or more simple departments and complex departments under it. It also has a collection (list) of employees in it.When I debug through this the first department is complex so it does recursion and in there I see that the employees are getting correctly populated and the employees collection count is 2, but when it comes out of the recursive call the employees collection count is again set to zero. Any ideas as to what I might be doing wrong here?
private void PopulateEmployees(ComplexDepartment complex)
{
foreach (var dep in complex.Departments)
{
if (dep is SimpleDepartment)
{
var simple = dep as SimpleDepartment;
complex.employees.Add(GetEmployee(simple));
}
else if (dep is ComplexDepartment)
{
PopulateEmployees(dep as ComplexDepartment);
}
}
}
private Employee GetEmployee(SimpleDapartment simple)
{
var employee = new Employee();
// some code here
return employee;
}