Why can't dotnet 1.1 be disabled after ArrayList.GetRange?

I would like to create an array from a range of values ​​inside an ArrayList, but I get the error message "At least one element in the source array cannot be dropped to the type of the target array."

Why should this happen with an error, and what would you do instead?

int[] ints = new int[] { 1, 2, 3 };
ArrayList list = ArrayList.Adapter(ints);
int[] mints = (int[])list.GetRange(0, 2).ToArray(typeof(int));
+3
source share
4 answers

This is a known bug in .NET 1.1 and is fixed in .NET 2.0.

GetRange . ToArray() ArrayList, GetRange, , .

2004 , Team BCL.

+3

, , Array.Copy:

    int[] ints = new int[] { 1, 2, 3 };
    int[] mints = new int[2];
    Array.Copy(ints, 0, mints, 0, 2);

, , /.

( , " 2.0", List<int>)

0

DotNet 2.0, Framework, , .

2.0 ArrayList.Adapter() ArrayList.IListWrapper( ArrayList), IList ( int []). ToArray IListWrapper IList.CopyTo .

, - 1.1, , 2.0, .

0

:

(int[])list.GetRange(0, 2).ToArray(typeof(int));

GetRange ArrayList.

, ArrayList ?

.NET 1.1, , : - arraylist , - , int. - ArrayList.Adapter .

Also, why don't you initialize the ArrayList as follows:

ArrayList l = new ArrayList ( new int[] {0, 1, 2, 3, 4, 5});

?

0
source

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


All Articles