469,636 Members | 1,544 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

dynamically resizing arrays

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
4 5423
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
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

"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
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.

Similar topics

13 posts views Thread by Jon Yeager | last post: by
46 posts views Thread by Blue Ocean | last post: by
7 posts views Thread by Fabian Wauthier | last post: by
10 posts views Thread by junky_fellow | last post: by
1 post views Thread by Nikhil Patel | last post: by
7 posts views Thread by Kjell | last post: by
64 posts views Thread by Philip Potter | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.