Re: Array.Clear vs List<>.Clear
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Thu, 4 Oct 2007 20:44:34 +0200
"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:%23oPFVRrBIHA.4956@xxxxxxxxxxxxxxxxxxxxxxx
"Doug Semler" <dougsemler@xxxxxxxxx> wrote in message news:1191519587.047316.183650@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxOn Oct 4, 11:54 am, "Laura T." <LT_s...@xxxxxxxxx> wrote:"Lee Crabtree" <lcrabt...@xxxxxxxxx> ha scritto nel messaggionews:ul6KAxpBIHA.4476@xxxxxxxxxxxxxxxxxxxxxxx
> The major reason I use a List<T> in a place where an Array would > normally
> be more common is that the line length has to be extended or shortened > at
> certain times, and recreating the array then copying elements across > seems
> unnecessarily ridiculous.
Well, that's what you are going to get with List<T> anyway, copying and more
(ridiculous) copying.
As Vadym said, the List<T> internally manages the list as an Array,[], so
every time you add one element over the List<T>.Capacity, the it will create
a new array, copy the elements, add the new item and then destroying the old
array.
The unfortunate thing with List<T> is that the capacity does grow up one by
one. I'd like to see something like "expand it by 10%", so that the next Add
would not incur reallocations.
It *doubles* the internal array size whenever you add an item that
would grow the array, not one by one. Starting at whatever your
initial capacity was (4 is default).
No Laura is right, a new array is created with a size which is equal to the previous size + 1 (size is an int) whenever there is an overflow.
Consider following sample:
List<byte> testList = new List<byte>();
for(int i = 0; i < 8; i++)
testList.Add((byte)i);
Here the List starts with an array of size 0, at the first insert a new array gets created with a size of 1 (int), that means that we can add 4 bytes before the array overflow, when that happens a new array gets created with a size of 2, and again 4 bytes can be added.
For the above sample that means creating 3 array's :-(
Willy.
I see I'm not (completely) right of course, point is that the default size is zero, and is doubled when there is an overflow.
So in the previous sample, the sequence is:
array size 0
add 1 byte -> create array with size 1 (int) , say n
add byte
add byte
add byte
add byte -> create array with size n = n* 2, from now on the new array is doubled in size.
....
Sorry for the confusion.
Willy.
.
- References:
- Array.Clear vs List<>.Clear
- From: Lee Crabtree
- Re: Array.Clear vs List<>.Clear
- From: Vadym Stetsiak
- Re: Array.Clear vs List<>.Clear
- From: Lee Crabtree
- Re: Array.Clear vs List<>.Clear
- From: Laura T.
- Re: Array.Clear vs List<>.Clear
- From: Doug Semler
- Re: Array.Clear vs List<>.Clear
- From: Willy Denoyette [MVP]
- Array.Clear vs List<>.Clear
- Prev by Date: Re: C# books
- Next by Date: Re: Ideas for a C# development project?
- Previous by thread: Re: Array.Clear vs List<>.Clear
- Next by thread: Re: Array.Clear vs List<>.Clear
- Index(es):
Relevant Pages
|