Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

17 Feb 2010

Learned something from Resharper: Enumerable.OfType<TResult>

A couple of weeks ago i discovered Enumerable.OfType when i let Resharper rewrite my code as a Linq statement. Here is the original code:

var selectedPersons = new List<personSelectItem>();
foreach(var selectedItem in selectedItems)
{
	var selectedPerson = selectedItem as PersonSelectItem;
	if (selectedPerson == null) continue;
	selectedPersons.Add(selectedPerson);
}

And here is how it looks after the rewrite:

var selectedPersons = selectedItems.OfType<personSelectItem>().ToList();

Yup, the Resharper license was definitely worth it’s money.