“Necessary” Uses of Recursion in Imperative Languages

后端 未结 9 1055
独厮守ぢ
独厮守ぢ 2020-12-17 17:10

I\'ve recently seen in a couple of different places comments along the lines of, \"I learned about recursion in school, but have never used it or felt the need for it since

9条回答
  •  时光取名叫无心
    2020-12-17 17:39

    I have a List of reports. I am using indexers on my class that contains this list. The reports are retrieved by their screen names using the indexers. In the indexer, if the report for that screen name doesn't exist it loads the report and recursively calls itself.

    public class ReportDictionary
        {
            private static List _reportList = null;
    
            public ReportColumnList this[string screenName]
            {
                get
                {
                    Report rc = _reportList.Find(delegate(Report obj) { return obj.ReportName == screenName; });
    
                    if (rc == null)
                    {
                        this.Load(screenName);
                        return this[screenName]; // Recursive call
                    }
                    else
                        return rc.ReportColumnList.Copy();
                }
                private set
                {
                    this.Add(screenName, value);
                }
            }
    
        }
    

    This can be done without recursion using some additional lines of code.

提交回复
热议问题