I’m sorry about the naive question. I have the following:
public Array Values
{
get;
set;
}
public List<object> CategoriesToList()
{
List<object> vals = new List<object>();
vals.AddRange(this.Values);
return vals;
}
But this won’t work because I can’t type the array. Is there any way to easily fix the code above or, in general, convert System.Collections.Array to System.Collections.Generic.List?
Make sure you’re
using System.Linq;and then change your line to:vals.AddRange(this.Values.Cast<object>());Edit: You could also iterate over the array and add each item individually.
Edit Again: Yet another option to simply cast your array as
object[]and either use theToList()function or pass it into theList<object>constructor:((object[])this.Values).ToList();or
new List<object>((object[])this.Values)