Re: Array.Clear vs List<>.Clear



Lee,

There isn't a mechanism to do this, but writing your own is easy:

public static void ClearList<T>(List<T> list, int index, int length)
{
// Cycle through the list and set the item to the default.
for (; index < (index + length); ++index)
{
// Set the item in the list to the default value.
list[index] = default(T);
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Lee Crabtree" <lcrabtree@xxxxxxxxx> wrote in message
news:%2377wqmpBIHA.4568@xxxxxxxxxxxxxxxxxxxxxxx
This seems inconsistent and more than a little bizarre.

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

For example:

byte[] byteArr = new byte[10];

...things happen and bytes get set...

Array.Clear(byteArr, 0, 10);

Now all the bytes are set to 0.


But if you use a List<byte>:

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

...things happen and bytes get set...

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

Lee Crabtree


.



Relevant Pages

  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • rotate revisited
    ... Suppose we have an array A containing n equi-spaced elements. ... The cost of this method (excluding loop costs) is: ... Each cycle is of length n/d. ... I will use the term "small slide" for a data move that is less ...
    (comp.programming)
  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: COBOL myth busted, index versus subscript (MF on HP)
    ... curious about an array without an index defined, ... So, Question, what is exit perform cycle? ... When I first ran the test, the null loop was empty. ...
    (comp.lang.cobol)