You gotta love the little things in .NET Framework 2.0: File.ReadAllLines()
I was recently reviewing some samples and I ran across one that made me really appreciate the work we did in BCL in .NET Framework 2.0... it maybe a very trivial thing, but it really improves the code.
The code sample in question looked something like:
if (autoCompleteWordList == null)
{
List<String> words = new List<string>();
FileStream file = new
FileStream(Server.MapPath("~/App_Data/words.txt"), FileMode.Open,
FileAccess.Read);
StreamReader reader = new StreamReader(file);
String word;
while ((word = reader.ReadLine()) != null)
{
words.Add(word);
}
file.Close();
autoCompleteWordList = words.ToArray();
Array.Sort(autoCompleteWordList, new CaseInsensitiveComparer());
}
I was able to cut the lines of code by more than 1/2 by using the File.ReadAllLines() method added in .NET Framework 2.0
if (autoCompleteWordList == null)
{
string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/words.txt"));
Array.Sort(temp, new CaseInsensitiveComparer());
autoCompleteWordList = temp;
}
What do you think? What other timesaving gems have you found in .NET Framework 2.0?