Convert non-generic type to generic type in c#
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!

Leave a Reply