System.Collections.Generic.List<int> myList = new System.Collections.Generic.List<int>(1

System.Collections.Generic.List<int> myList = new System.Collections.Generic.List<int>(1

am 09.04.2008 21:05:33 von DR

System.Collections.Generic.List myList = new
System.Collections.Generic.List(100);

Does this preallocate 100 integers?

Also, is there any way to preallocate an array of objects all at once?

MyOb[] al= new MyOb[100];

for (int z = 0; z < nCount; z++)

{

al[z] = new MyOb();

}



Can i avoid all these new and just allocate an array of MyOb all at once?

RE: System.Collections.Generic.List<int> myList = new System.Collectio

am 09.04.2008 21:29:04 von PRSoCo

It creates a list with a capacity of 100. This, in reality, creates an array
with 100 elements in it. Because the type is int (a value type) it does
pre-allocate 100 ints.

I you already have an array of values you can pass it into the List
constructor; but it will copy the values.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"DR" wrote:

> System.Collections.Generic.List myList = new
> System.Collections.Generic.List(100);
>
> Does this preallocate 100 integers?
>
> Also, is there any way to preallocate an array of objects all at once?
>
> MyOb[] al= new MyOb[100];
>
> for (int z = 0; z < nCount; z++)
>
> {
>
> al[z] = new MyOb();
>
> }
>
>
>
> Can i avoid all these new and just allocate an array of MyOb all at once?
>
>
>