I am a little new to C# and I'm having performance issues with this. In my program, people import a .txt list and the program makes a list out of it; the problem is its consuming too much RAM, crashing PC's with low memory. I thought of using 'yield' without success. Any ideas?
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;
}
}