❔ Best way to you Read lot of files?

I need something like a Filestream but where I can change the file thats read but each time I want to read another file I need to create a new FileStream which does extra allocation. See here:

    public void T2()
    {
        var fs = new FileStream(@"./File.txt", FileMode.Open);
        byte[] buffer = new byte[500];

        for (int i = 0; i < fs.Length; i++)
        {
            buffer[i] = (byte)fs.ReadByte();
        }

        // do somthing with bytes in buffer where I get another file location

        // need to do this cause we want another File but
        fs = new FileStream(@"./AnotherFile.txt", FileMode.Open);

        // Do someting with file

        // need to do this cause we want another File but
        fs = new FileStream(@"./YetAnotherFile.txt", FileMode.Open);

        // and so on

        fs.Close();

    }


Is there a better way which dont generate as much or even better, no(extra) garbage?
Was this page helpful?