473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5557
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Jon Yeager | last post by:
I need to display a bunch of pictures that are all of various dimensions into a fixed dimension space, like MSN Messenger does with its user photos in the chat windows. Forcing image dimensions...
8
by: Freek te Water | last post by:
Hi, Hope no-one is offended by my probably noob question... Context: I have a web page design, which always centres in the middle of the screen (using a 100%*100% HTML-table). Now I also use...
46
by: Blue Ocean | last post by:
How do I do it? I'm doing a practice problem which includes me implementing a set of ints as a class. I don't want to use vector because that would be cheating. ;) I don't want to spend...
9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
7
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
10
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr =...
1
by: Nikhil Patel | last post by:
Hi all, I dynamically create new controls and display them on the form. My problem is that the user has to resize form manually to see controls whose location property points to a point outside...
7
by: Kjell | last post by:
H I'm a former VB programmer and I have a issue with Arrays in C If I can't tell the size in advance how can I solve the issue since C# does not suppor resizing arrays like VB do (i.e. Redim...
64
by: Philip Potter | last post by:
Hello clc, I have a buffer in a program which I write to. The buffer has write-only, unsigned-char-at-a-time access, and the amount of space required isn't known a priori. Therefore I want the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.