This matches the original:
public static List<DateTime> GetHolidays(string holidaysFile)
{
return File.ReadAllLines(holidaysFile).Select(d => Convert.ToDateTime(d)).ToList();
}
But it kills me to use a List like that unless there's a real need for it. The code below will allow you to do lazy evaluation, supports only keeping one line in RAM at a time, and often still works with no changes to the calling code — many of the same benefits as using a StreamReader. Note that it's also shorter, and can still be easily converted to a List if needed. While I'm here, since you're coming from a string, you might do better using the DateTime.Parse()
method:
public static IEnumerable<DateTime> GetHolidays(string holidaysFile)
{
return File.ReadLines(holidaysFile).Select(d => DateTime.Parse(d));
}
As a general rule, you're almost always better off writing code to return an IEnumerable<T>
instead of a List<T>
. You have very little to lose, because you can always just append .ToList()
to the end of such a function when you need, and much to gain in terms of easier refactoring from one underlying collection type to another and potential performance improvements by reducing RAM use or avoiding looping over items in the List it later turns out you never needed.