Convert BindingList <T> to an array from T

What is the easiest way to convert BindingList<T>to T[]?

EDIT: I'm on 3.0 for this project, so LINQ.

+3
source share
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
source

In .Net 2 you must use the method .CopyTo()on yourBindingList<T>

+3
source

, , .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
source

Source: https://habr.com/ru/post/1704325/