Casting a ListBox.SelectedObjectCollection to a ListBox.ObjectCollection?

ChristianLinnell picture ChristianLinnell · Nov 2, 2010 · Viewed 8.9k times · Source

Is it possible to cast a ListBox.SelectedObjectCollection to a ListBox.ObjectCollection in C#? If so, how would I go about it?

Answer

jhoanna picture jhoanna · Sep 28, 2012

I have a function that accepts List<string>.

I can pass both SelectedItems and Items by casting them.

Try this:

SelectedItems.Cast<string>().ToList()
Items.Cast<string>().ToList()

<string> could be replaced with some other object type.