ILearnable .Net

March 10, 2010

ToList() on a list

Filed under: Uncategorized — andreakn @ 09:47

I usually add this little gem in my projects:

public static List<T> ToList<T>(this List<T> list)
{
return list;
}

so that calling ToList on something that is already a list doesn’t require looping through all the content (which is the default implementation of IEnumerable[t].ToList())

I also tend to put it into the System.Linq namespace so that it comes automatically when I get the original method. (i know it’s not the recommended way, but GOD it’s so practical)

premature optimization is the root of all evil, but small invisible ones isn’t (imho)

Advertisements

Create a free website or blog at WordPress.com.