By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,212 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,212 IT Pros & Developers. It's quick & easy.

dynamically resizing arrays

P: n/a
As there is no ReDim in C# , what is the preferred technique for dynmically
increasing the size of any array ?

Does the answer depend on whether the array holds primitives versus
instances of classes ?
Mar 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
As there is no ReDim in C# , what is the preferred technique for dynmically
increasing the size of any array ?


To use a List<T> or ArrayList instead, unless it has to be an actual
array. Otherwise you have to implement the array "resizing" yourself
by allocating a new array and copying the exisitng elements.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 29 '06 #2

P: n/a
No resizing.

You have to enumerate or use methods like Array.Copy.

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:ew****************@TK2MSFTNGP10.phx.gbl...
As there is no ReDim in C# , what is the preferred technique for
dynmically increasing the size of any array ?

Does the answer depend on whether the array holds primitives versus
instances of classes ?

Mar 29 '06 #3

P: n/a

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OE**************@TK2MSFTNGP11.phx.gbl...
As there is no ReDim in C# , what is the preferred technique for
dynmically
increasing the size of any array ?


To use a List<T> or ArrayList instead, unless it has to be an actual
array. Otherwise you have to implement the array "resizing" yourself
by allocating a new array and copying the exisitng elements.


Unfortunately a large number of framework methods take arrays as parameters.
Even more irritatingly, many of them don't have overloads to specify array
ranges which often forces you to make at least one "unnecessary" copy - in
which case you might as well use List<T>.ToArray() anyway.

Mar 29 '06 #4

P: n/a
For one dimensional arrays in C# 2, just use Array.Resize:
Array.Resize(ref YourArray, NewLength);
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"John A Grandy" wrote:
As there is no ReDim in C# , what is the preferred technique for dynmically
increasing the size of any array ?

Does the answer depend on whether the array holds primitives versus
instances of classes ?

Mar 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.