I’m designing a class with several functions returning lists of objects. I noticed that debugging becomes easier when my IEnumerable<T> is converted to a List<T> since the objects can be viewed in the Watch window. However, I’m not sure if this is best practice when it comes to IEnumerable.
Q: Which is better?
public IEnumerable<MyData> GetData()
{
return Foo();
}
public IEnumerable<MyData> GetData()
{
return Foo().ToList();
}
Good practice – don’t do work unless there are good reasons/required. So unless you have special reasons – just return
IEnumerable<MyData>without calling ToList()/ToArray() on it.Potential reasons: