Wednesday, September 30, 2009

Where is the List.Find() method in Silverlight?

For any .NET developers new to Silverlight, they may find it strange that they are not able to resolve the Find() method when working with Generic collections. This is a very useful method when searching a collection for a particular item.

The Find(*) methods on List were added in version 2.0 of the .NET framework before LINQ. LINQ (which stands for Language INtegrated Query) now provides the First() method, which can be used in a similar way to the List.Find() method.

Since LINQ is available in Silverlight, Microsoft decided not to add the Find methods to List to avoid adding duplicate functionality and to keep the size of Silverlight as small as possible.

LINQ can be used as a replacement for List.Find(Predicate):

using System.Linq;

var list = new List { 1, 2, 3, 4, 5 };
int item = list.First(i => i == 2); // same as calling list.Find(i => i == 2);

No comments:

Post a Comment

Could you do me a SOLID mate?

Wow, it’s been a while! Nearly nine years since my last blog post . A lot has changed since then – some for the better, some for the worse....