How best to read a File into List

后端 未结 10 1280
盖世英雄少女心
盖世英雄少女心 2020-11-27 17:34

I am using a list to limit the file size since the target is limited in disk and ram. This is what I am doing now but is there a more efficient way?

readonly         


        
10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 17:57

    Don't store it if possible. Just read through it if you are memory constrained. You can use a StreamReader:

    using (var reader = new StreamReader("file.txt"))
    {
        var line = reader.ReadLine();
        // process line here
    }
    

    This can be wrapped in a method which yields strings per line read if you want to use LINQ.

提交回复
热议问题