是否有可能或值得将数组发送到另一个方法c#

时间:2015-04-10 11:35:38

标签: c# arrays methods

在我的代码中,我有两个方法,一个是main方法,另一个是'MethodA'。在main方法中,我将完成所有显示MethodA中的其他功能。但是,在MethodA中,我试图将几个文件读入一个数组并将它们返回到main方法,以便可以使用它们。我问的问题是:(我已经将这个问题的主要方法浓缩了,它确实包含了其中没有链接的其他项目)

1:是否值得将文件读入数组?

2:我应该在main中使用'ReadFiles'而不是其他方法吗?

static void Main(string[] args) // THE MAIN INCLUDES MAINLY ONLY THE WRITELINES AND WHAT IS SEEN
        {
string value = MethodA();
           Console.WriteLine(value);

            Console.ReadKey();
        }

        public string[] MethodA() //FILE METHOD - READS IN THE FILES THEN RETURNS THEM TO MAIN METHOD
        {
            StreamReader dayFile = new StreamReader("c:..\\Files\\Day.txt"); StreamReader dateFile = new StreamReader("c:..\\Files\\Date.txt");
            StreamReader sh1Open = new StreamReader("c:..\\Files\\SH1_Open.txt"); StreamReader sh1Close = new StreamReader("c:..\\Files\\SH1_Close.txt");
            StreamReader sh1Volume = new StreamReader("c:..\\Files\\SH1_Volume.txt"); StreamReader sh1Diff = new StreamReader("c:..\\Files\\SH1_Diff.txt");
            StreamReader sh2Open = new StreamReader("c:..\\Files\\SH2_Open.txt"); StreamReader sh2Close = new StreamReader("c:..\\Files\\SH2_Close.txt");
            StreamReader sh2Volume = new StreamReader("c:..\\Files\\SH2_Volume.txt"); StreamReader sh2Diff = new StreamReader("c:..\\Files\\SH2_Diff.txt");

            string dayString = dayFile.ReadToEnd(); string dateString = dateFile.ReadToEnd(); string Sh1OpenString = sh1Open.ReadToEnd(); string Sh1CloseString = sh1Close.ReadToEnd();
            string Sh1VolumeString = sh1Volume.ReadToEnd(); string Sh1DiffString = sh1Diff.ReadToEnd(); string Sh2OpenString = sh2Open.ReadToEnd(); string Sh2CloseString = sh2Close.ReadToEnd();
            string Sh2VolumeString = sh2Volume.ReadToEnd(); string Sh2DiffString = sh2Diff.ReadToEnd();

            string[] fileArray = new string[] { dayString, dateString, Sh1OpenString, Sh1CloseString, Sh1VolumeString, Sh1DiffString, Sh2OpenString, Sh2CloseString, Sh2VolumeString, Sh2DiffString };
            return fileArray;
        }

2 个答案:

答案 0 :(得分:0)

这取决于文件往往是否相当小。如果它们是几千兆字节,那么你就会遇到问题。在这种情况下,您应该尝试按顺序读取文件。

  public static IEnumerable<string> ReadLines(Stream stream)
  {
     using (var reader = new StreamReader(stream))
     {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
           yield return line;
        }
     }
  }
BTW:StreamReader应始终与“using”关键字一起使用,否则会在garbace收集器完成超出范围的实例之前保留打开的文件。

答案 1 :(得分:0)

我认为可以全部阅读它们 - 除非您知道这些文件是否庞大。

我这样做:

    public string[] MethodA()
    {
        var filenames = new []
        {
            "Date.txt",
            "Day.txt",
            "SH1_Close.txt",
            "SH1_Diff.txt",
            "SH1_Open.txt",
            "SH1_Volume.txt",
            "SH2_Close.txt",
            "SH2_Diff.txt",
            "SH2_Open.txt",
            "SH2_Volume.txt",
        };

        return
            filenames
                .Select(fn => File.ReadAllText("c:..\\Files\\" + fn))
                .ToArray();
    }