Convert non-generic type to generic type in c#

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

Just today when mussing with List controls and their SelectedItems property came to a point where I needed to convert their SelectedItems property to a generic list (because god knows why it is non-generic).

I needed this because I wanted to query over the selected items and LINQ doesn’t work with non-generic lists so…

Turns out the solution is quite simple:

IEnumerable<string> genericizedItems = itemsListBox.SelectedItems.OfType<string>();

Works like a charm, hope this helps!

Cheers!


Filed under: .NET, C#
Written on: 12 Jun 2008 ·

kick it on DotNetKicks.com

Leave a Reply