Friday, October 09, 2009

Quick Linq tip

When you crete a construction such as

if (list.Count > 0)
{
// do stuff
}


its actually much more efficient to use LINQ as such:

if (list.Any())
{
// do stuff
}


Advantage: ANY will stop after the first hit, while COUNT will traverse the entire enumerator. This can be significant for larger lists.

No comments: