Re: Array.Clear vs List<>.Clear

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Lee Crabtree" <lcrabtree@xxxxxxxxx> ha scritto nel messaggio news: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.
So if you know how much you must expand, you sould set the List<T>.Capacity in advance to the max you need as you cannot shrink a List<T> capacity.
The performance of simple System.Array could result better.


Lee Crabtree

Vadym Stetsiak wrote:
Hello, Lee!

What's the point using List<T> like Array ?

List<T> internally maintains array of type T, so when you call List<T>.Clear all internal array items will be set to 0,
and List<T> size will be reset.
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Thu, 04 Oct 2007 10:17:20 -0500:

LC> This seems inconsistent and more than a little bizarre.

LC> Array.Clear sets all elements of the array to their default values
LC> (0, null, whatever), whereas List<>.Clear removes all items from
LC> the list.
LC> That part makes a reasonable amount of sense, as you can't actually
LC> take items away from an Array. However, there doesn't seem to be a
LC> way to perform the same operation in one fell swoop on a List<>.

LC> For example:

LC> byte[] byteArr = new byte[10];

LC> ...things happen and bytes get set...

LC> Array.Clear(byteArr, 0, 10);

LC> Now all the bytes are set to 0.


LC> But if you use a List<byte>:

LC> List<byte> byteList = new List<byte>(new byte[10]);

LC> ...things happen and bytes get set...

LC> There's no way to reset all the bytes, so you're forced to iterate
LC> over the list. Now, I'm sure that the performance hit of having to
LC> run a for loop across the list isn't incredible. But aside from
LC> the apparent inconsistency, I have to wonder if there isn't some
LC> mechanism to do the same thing to a generic List.

LC> Lee Crabtree

.



Relevant Pages

  • Re: Array.Clear vs List<>.Clear
    ... and recreating the array then copying elements across ... a new array, copy the elements, add the new item and then destroying the ... The unfortunate thing with Listis that the capacity does grow up one ... The list doubles....Reflect the Add and EnsureCapacity method. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array.Clear vs List<>.Clear
    ... The major reason I use a Listin 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. ... LC> There's no way to reset all the bytes, ... LC> the apparent inconsistency, I have to wonder if there isn't some ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array.Clear vs List<>.Clear
    ... What's the point using Listlike Array? ... With best regards, Vadym Stetsiak. ... LC> There's no way to reset all the bytes, ... LC> the apparent inconsistency, I have to wonder if there isn't some ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Changind array dimension
    ... Array has Resize method. ... With best regards, Vadym Stetsiak. ... Blog: http://vadmyst.blogspot.com ...
    (microsoft.public.dotnet.framework)