C# Can';t将ListBox.ObjectCollection转换为(类型化)数组

C# Can';t将ListBox.ObjectCollection转换为(类型化)数组,c#,string,collections,arrays,listbox,C#,String,Collections,Arrays,Listbox,我想将这些项转换为字符串数组或用于填充ListBox.DataSource的类型。该类型已重写ToString(),但我似乎无法将其转换为String[] String[] a = (String[])ListBox1.Items; Contacts[] b = (Contacts[])ListBox1.Items; 或者,如果您有办法将字符串转换为联系人,可以执行以下操作: IEnumerable<Contacts> c = ListBox1.Items.Cast<stri

我想将这些项转换为字符串数组或用于填充ListBox.DataSource的类型。该类型已重写ToString(),但我似乎无法将其转换为String[]

String[] a = (String[])ListBox1.Items;
Contacts[] b = (Contacts[])ListBox1.Items;
或者,如果您有办法将字符串转换为联系人,可以执行以下操作:

IEnumerable<Contacts> c = ListBox1.Items.Cast<string>().Select(s => StringToContact(s));
IEnumerable c=ListBox1.Items.Cast().Select(s=>StringToContact);

Cast方法似乎不再可用。我想出了另一个解决方案:

String[] array = new String[ListBox.Items.Count]
ListBox.Items.CopyTo(array, 0);
CopyTo
方法获取一个现有数组,并在给定的索引处插入项并转发


我不知道这是否非常有效,但它一致且易于编写。

我只需将其转换为参数传递给方法即可。它只接受字符串[]或联系人[]。非常有帮助,谢谢!要访问.Cast(),需要添加“using System.Linq;”
IEnumerable<Contacts> c = ListBox1.Items.Cast<string>().Select(s => StringToContact(s));
String[] array = new String[ListBox.Items.Count]
ListBox.Items.CopyTo(array, 0);