【问题标题】:C# getting a jagged array from a method callC# 从方法调用中获取锯齿状数组
【发布时间】:2016-04-19 23:48:26
【问题描述】:

我有一个锯齿状数组,它在main 中运行良好,但如果我尝试将它放入一个方法中,我不确定该调用什么。我试过load(data) 并返回语句,但我没有任何运气。

public static void load() 
{
    try 
    {
        string[][] data = new[] {
            File.ReadAllLines(@"data\Month.txt"),
            File.ReadAllLines(@"data\Year.txt"),
            File.ReadAllLines(@"data\WS1_AF.txt"),
            File.ReadAllLines(@"data\WS1_Rain.txt"),
            File.ReadAllLines(@"data\WS1_Sun.txt"),
            File.ReadAllLines(@"data\WS1_TMin.txt"),
            File.ReadAllLines(@"data\WS1_TMax.txt"),
        };
        Console.WriteLine("Files have been found, press any key to continue");
        Console.ReadKey();
    }
    catch (Exception) {
        Console.WriteLine("Unable to find files... exiting");
        exit();
    }
}

【问题讨论】:

  • 你到底想完成什么?
  • 我正在尝试将数组从该方法获取到另一个方法。在其他方法之间转移它
  • 是的,但我们不明白您的问题。您是否正在尝试将数据返回到另一件事?那么你的代码中需要public static string[][] load()return data;。否则我们不知道你在问什么

标签: c# jagged-arrays


【解决方案1】:

只需从该方法返回您的数组,并将load 方法的返回类型更改为与您返回的数组相同的类型(void 表示您没有返回任何内容)。此外,最好在更高级别处理异常:

public static string[][] load()
{
    string[][] data = new[]
    {
        File.ReadAllLines(@"data\Month.txt"),
        File.ReadAllLines(@"data\Year.txt"),
        File.ReadAllLines(@"data\WS1_AF.txt"),
        File.ReadAllLines(@"data\WS1_Rain.txt"),
        File.ReadAllLines(@"data\WS1_Sun.txt"),
        File.ReadAllLines(@"data\WS1_TMin.txt"),
        File.ReadAllLines(@"data\WS1_TMax.txt"),
    };

    return data;
}

public static void test()
{
    try
    {
        var data = load();

        Console.WriteLine("Files have been found, press any key to continue");
        Console.ReadKey();
    }
    catch (Exception)
    {
        Console.WriteLine("Unable to find files... exiting");
        exit();
    }
}

【讨论】:

    【解决方案2】:

    将您的签名从 void 更新为您希望返回的返回类型并返回您的数组。

    public static string[][] load()
    {
        try
        {
            string[][] data = new[]
            {
                File.ReadAllLines(@"data\Month.txt"),
                File.ReadAllLines(@"data\Year.txt"),
                File.ReadAllLines(@"data\WS1_AF.txt"),
                File.ReadAllLines(@"data\WS1_Rain.txt"),
                File.ReadAllLines(@"data\WS1_Sun.txt"),
                File.ReadAllLines(@"data\WS1_TMin.txt"),
                File.ReadAllLines(@"data\WS1_TMax.txt"),
            };
            Console.WriteLine("Files have been found, press any key to continue");
            Console.ReadKey();
    
            return data;
        }
        catch (Exception)
        {
            Console.WriteLine("Unable to find files... exiting");
            return null;
        }
    }
    

    请注意,您还必须从 catch 块中返回一些内容,因为您不会重新抛出异常。在这种情况下,返回null

    【讨论】:

      【解决方案3】:

      考虑使用路径和文件名数组更模块化的方法

      class Program
      {
          public static string[][] ReadFiles(string folder, params string[] files)
          {
              return files.Select((fn) => File.ReadAllLines(Path.Combine(folder, fn))).ToArray();
          }
      
          static void Main(string[] args)
          {
      
              var data = ReadFiles(@"data", 
                  "Month.txt",
                  "Year.txt",
                  "WS1_AF.txt",
                  "WS1_Rain.txt",
                  "WS1_Sun.txt",
                  "WS1_TMin.txt",
                  "WS1_TMax.txt" );
          }
      }
      

      附录

      也尽量不要硬编码任何绝对路径,而是使用特殊的文件夹枚举。例如:

      var folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
      

      【讨论】:

        猜你喜欢
        • 2011-12-06
        • 2015-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-10
        • 1970-01-01
        相关资源
        最近更新 更多