In addition to Solution 1:
Volkan Paksoy meant the generic class
System.Collections.Generic.List<>
:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[
^].
Depending on what operations you really need to support, you may need other types of collections, but don't use obsolete
ArrayList
.
Now, closer to what you wanted to do you can do this,
but don't:
https://msdn.microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx[
^].
Why "big bold don't"? Read "Remarks" section. It would create a brand new array object each time you reallocate it, amd copy all data, but the effect would be miserable, at this cost, it is not even functionally acceptable. You have to decide how many elements to add; and it's bad to face a need for an arbitrary decision; then you add 10 and the user only uses 1, besides, you will have to maintain the number of actually uses elements, as opposed to allocated. Collections, collections, only collections.
By a similar reasons, never repeatedly concatenate "the same string", it's not the same. String is
immutable.
System.Text.StringBuilder
, which can be considered as a mutable analog of string.
—SA