Array.Clear vs List<>.Clear
- From: Lee Crabtree <lcrabtree@xxxxxxxxx>
- Date: Thu, 04 Oct 2007 10:17:20 -0500
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
.
- Follow-Ups:
- Re: Array.Clear vs List<>.Clear
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Array.Clear vs List<>.Clear
- From: Vadym Stetsiak
- Re: Array.Clear vs List<>.Clear
- From: Marc Gravell
- Re: Array.Clear vs List<>.Clear
- Prev by Date: Re: Why simple properties?
- Next by Date: Getting TypeLib ID of a COM dll
- Previous by thread: Opinions: Warn about online registration checks?
- Next by thread: Re: Array.Clear vs List<>.Clear
- Index(es):
Relevant Pages
|
Loading