Convert BindingList <T> to an array from T
3 answers
Well, if you have LINQ (C # 3.0 + or .NET 3.5 or LINQBridge ), you can use .ToArray()- otherwise, just create an array and copy the data.
T[] arr = new T[list.Count];
list.CopyTo(arr, 0);
+9
, , .net2.0. List, ToArray().
public T[] ToArray<T>(BindingList<T> bindingList) {
if (bindingList == null)
return new T[0];
var list = new List<T>(bindingList);
return list.ToArray();
}
Note. This is indeed a less effective solution than the CopyTo solution shown by other participants. Instead, use their solution, this creates two arrays, the result and one internal for the list instance.
+1