Thursday, October 23, 2008

Why oh why streamreader

While I was going through some playing around with python i was really impressed with the following bit of functional joy.

inList = open(fileLoc, 'rU').readlines();


I was so happy to find that little tidbit and sad to not have used such a thing in C# ever. I always used the awful:


List<String> strings = new List<String>();
using(StreamReader reader = new StreamReader(fileloc))
{
while(line = reader.ReadLine()) != null
{
strings.Add(line);
}
}


This is the type of code you see in MSDN and in various "Professional C# 2.0" type books, they'll even show you TextReader to boot. However for the vast majority of cases the least bug free and easy to use appears to be something I totally missed for the past couple of years:


string[] readText = File.ReadAllLines(fileloc);


This has been around since .net 2.0, opens the file, reads it and closes it. At this point I'd argue that the shorter option should be shown primarily to newer students and the remaining features left in an appendix somewhere.

EDIT: Jon Skeet pointed out his simple class for doing the same above but with better performance. http://csharpindepth.com/ViewChapterNotes.aspx?Chapter=6