How to convert an array to a BindingList

What is the easiest and fastest way to convert an array to a BindingList?

+4
source share
4 answers

Use the BindingList constructor, which takes an IList<T> .

 var binding = new BindingList<MyType>(myArray); 
+10
source

You are looking for a constructor:

 var bl = new BindingList<YourClass>(arr); 
+3
source

Be careful when using the BindingList (IList ..) constructor with an array, as IList will be read-only.

Any attempts to add / remove from the BindingList will raise a NotSupportedException, as IList will not be able to handle this functionality, since the collection is read-only.

To create an editable BindingList, you will need to convert it to a list before using the IList constructor.

A good description of why arrays are built from IList can be found here for some further reading: Why does an array implement IList?

+3
source

you can try foreach loop:

  public void AppenFromArray(T[] aSource) { if (aSource == null) { return; } foreach (T el in aSource) { this.Add(el); } } 
0
source

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


All Articles