Recursive call return a List, return type causing me issues

前端 未结 5 714
遥遥无期
遥遥无期 2021-02-04 06:12

I have a recursive method that is return me categories, and checking for its sub categories.

So it looks like:

public List GetAllChildCat         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 06:43

    Here's a modified version of Jon Skeet's answer using a local method (C# 7) :

        public List GetAllChildCats(int rootId)
        {
            List list = new List();
            Traverse(rootId);
            return list;
    
            void Traverse(int categoryId)
            {
                Category c = Get(categoryId);
                list.Add(c);
    
                foreach (Category cat in c.ChildCategories)
                {
                    Traverse(cat.CategoryID);
                }
            }
        }
    

提交回复
热议问题