Learned something from Resharper: Enumerable.OfType<TResult>

A couple of weeks ago i discovered Enumerable.OfType<TResult> 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.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>