小编Alb*_*uez的帖子

优化List <string>

我对C#有点新意,我遇到了性能问题.在我的程序中,人们导入一个.txt列表,程序会从中列出一个列表; 问题是它消耗了太多内存,导致PC内存不足而崩溃.我想过使用'yield'而没有成功.有任何想法吗?

private List<string> ImportList()
{
    try
    {
        using (var ofd = new OpenFileDialog() { Filter = "Text files (*.txt) | *.txt" })
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
               return File.ReadAllLines(ofd.FileName).ToList();
            }
        }

        return null;
    }
    catch(OutOfMemoryException ex)
    {
        MessageBox.Show("The list is too large. Try using a smaller list or dividing it.", "Warning!");
        return null;

    }
}
Run Code Online (Sandbox Code Playgroud)

c# arrays list

0
推荐指数
1
解决办法
162
查看次数

标签 统计

arrays ×1

c# ×1

list ×1