arpit1611 arpit1611 - 1 year ago 221
C# Question

C# Convert ArrayList to List of string?

I have an

, need to add the items of ArrayList into a List of string. I tried the following:

listString.AddRange(new List<string>((string[])this.arrayList.ToArray()))

But this gives an exception of

Unable to cast object of type 'System.Object[]' to type 'System.String[]'

Please note :; I can't use .Cast as I am working in framework

Answer Source

Use ToArray(typeof(string)) to create an Array and then cast it to a more specific array type string[] before adding it to the variable of type List<string>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download