欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 递归 小记

程序员文章站 2024-03-07 13:30:51
...
    public class Program
    {
        public static List<testclass> lists = getList();
        public static int j = 1;
        public static string str1="" ;
        public static void Main(string[] args)
        {
            string a = "";
            var t1 = lists.FirstOrDefault(o => o.To == "0");
            if (t1 != null)
            {
               a= DiGui(t1.From);
            }
            Console.WriteLine("最终结果:" + a);
            Console.ReadLine();
        }

        public static string DiGui(string str)
        { 
            var t2 = lists.FirstOrDefault(o => o.To == str);
            if (t2!=null)
            {
                Console.WriteLine(j+",From:" + t2.From + ",To:" + t2.To);
                j++;
                DiGui(t2.From);
            }
            else
            {
                var t3 = lists.FirstOrDefault(o => o.From == str);
                Console.WriteLine(j + ",t3:" + t3.From);
                str1 = t3.From;
            }
          
            return str1;

        }

        public static List<testclass> getList()
        {
            List<testclass> list = new List<testclass>();
            for (int i = 10; i > 0; i--)
            {
                testclass one = new testclass();
                one.From = i.ToString();
                one.To = (i - 1).ToString();
                list.Add(one);
            }
            return list;
        }
    }

    public class testclass
    {
        public string From { get; set; }
        public string To { get; set; }

    }