小编Mr.*_*Pro的帖子

不使用递归 C# 遍历文件夹树

如何将这种递归循环改为非递归?我知道这种方法很简单,但我对此解决方案的非递归方式感兴趣。

using System;
using System.IO;

namespace NonRecursion {
    class NonRecursion {
        static void Main() {
            string createPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            string getPath = createPath + @"\folder";
            GetDirsPath(getPath);

            Console.ReadKey();
        }

        static void GetDirsPath(string getPath) {
            string[] dirs = Directory.GetDirectories(getPath);

            for (int i = 0; i < dirs.Length; i++) {
                Console.WriteLine(dirs[i]);
                GetDirsPath(dirs[i]);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以只更改这个功能吗?

static void GetDirsPath(string getPath) {
            string[] dirs = Directory.GetDirectories(getPath);

            for (int i = 0; i < dirs.Length; i++) {
                Console.WriteLine(dirs[i]);
                GetDirsPath(dirs[i]);
            }
        }
Run Code Online (Sandbox Code Playgroud)

c# recursion

3
推荐指数
1
解决办法
1772
查看次数

为什么我会收到错误"检测到无法访问的代码"?

using System;

namespace G09 {
    class Reverse {
        static void Main() 
        {
            Console.WriteLine(ReverseText(24));

            Console.ReadKey();
        }

        static string ReverseText(int n) {
            if (n < 1) 
            {
                return "";
            }

            string index = "1";
            string revIndex = "";
            int count = 1;
            string[] arr = new string[n];

            for (int i = 0; i < n; i++) 
            {
                arr[i] = index + revIndex;
                for (int j = 0; j <= i; j++) 
                {
                    if (count > 8) 
                    {
                        index = index + 0; …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
2
解决办法
1812
查看次数

标签 统计

c# ×2

recursion ×1