Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C# ArrayList doesn't have Resize method?

Coming from C++, it's very weird to find that C# ArrayList doesn't have Resize(count) method?
Why? Am I missing something?

like image 897
Paulius Liekis Avatar asked Nov 03 '12 10:11

Paulius Liekis


1 Answers

There are three separate operations you might wish to perform:

  • Changing the capacity of the ArrayList. This is achievable through ArrayList.Capacity and List<T>.Capacity

  • Changing the actual count of the list by trimming some elements. This is achievable through ArrayList.RemoveRange and List<T>.RemoveRange.

  • Changing the actual count of the list by adding some elements. This is achievable through ArrayList.AddRange and List<T>.AddRange. (As of .NET 3.5, you can use Enumerable.Repeat to very easily come up with a sequence of the right length.)

(I mention List<T> as unless you're really stuck on .NET 1.1, you'd be better off using the generic collections.)

If you want to perform some other operation, please specify it. Personally I'm glad that these three operations are separate. I can't think of any cases in my own experience where I've wanted to add or remove elements without knowing which I'd actually be doing.

like image 160
Jon Skeet Avatar answered Sep 30 '22 15:09

Jon Skeet