List.RemoveAt(int index) returns void??

Would it have really hurt to return the value that is being removed   I mean, if you don't need it then just don't capture the return value.  If I want to drain a list from the head then I have to write goofy code like this:



List<int> primes = new List<int>();
int nextPrime = candidates[0];
candidates.RemoveAt(0);
primes.Add(nextPrime);

 


this was what I was expecting to be able to do:



List<int> primes = new List<int>();
int nextPrime = candidates.RemoveAt(0);
primes.Add(nextPrime);

 


Answer this question

List.RemoveAt(int index) returns void??

  • DavidLSNB

    Possibly but as this was an implementation of the Sieve of Eratosthenes, I need to remove numbers all through-out the candidates list.  It is just that everytime you come through the loop you always remove the first number and place in in the prime numbers list.
  • sxsoptic

    The RemoveAt method does not look at the value it removes. It doesn't need to, since it already has the index position.

    List is backed by an array, and RemoveAt(0) just does a memory block move of the array values 1...n down to position 0 when you remove the first element. The first element is never read from memory.

    So yes, it would actually hurt performance (a little tiny bit, depending on page boundaries and cache contents...) to add this functionality.

  • rwike

    You could implement candidates as a Generics.Stack<int> and use its Pop/Push members. Would probably be more performant...

  • List.RemoveAt(int index) returns void??