473,379 Members | 1,245 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,379 software developers and data experts.

dynamically sized arrays

Hi all,

I am from the C++ world, and just making baby steps into C#. Just
wanted to ask you about the best way to create a dynamically sized
array, and maintain it effectively (yeah, I know about the wonders
of automatc garbage collection, and all that in C#), Any good code snippets
as examples would be wonderful.

Thanks in advance,

Carlos.
Nov 17 '05 #1
5 3187
Hello Carlos,
I am from the C++ world, and just making baby steps into C#. Just
wanted to ask you about the best way to create a dynamically sized
array, and maintain it effectively (yeah, I know about the wonders
of automatc garbage collection, and all that in C#), Any good code
snippets as examples would be wonderful.


I am sometimes use the following pattern :

ArrayList arr = new ArrayList();
arr.Add(new MyType()); arr.Add(new MyType()); // dynamically
MyType[] myType = arr.ToArray(typeof(MyType)) as MyType[];

class MyType { public int test = 42; }
ciao Frank
--
Dipl.Inf. Frank Dzaebel [MCP C#]
http://Dzaebel.NET
Nov 17 '05 #2

FYI - if you speak C++ {as I did for 11++ years} you can learn a lot about
the wonders of garbage collection from Jeffrey Richter's excellent 2 part
article:

http://msdn.microsoft.com/msdnmag/is...I/default.aspx

http://msdn.microsoft.com/msdnmag/is...2/default.aspx

The hard part to swallow for us C++-ees is the golden rule: do NOT create a
finalizer {destructor} unless you need to do so! And if you DO need a
finalizer it is a great idea to implement the IDispose pattern and use the
"using" keyword as described here:

http://msdn.microsoft.com/library/de...pc10192000.asp

And on a related note the .NET framework V2.0 implements generic {templated}
versions of ArrayList etc. in the System.Collections.Generic namespace...
"Carlos" wrote:
Hi all,

I am from the C++ world, and just making baby steps into C#. Just
wanted to ask you about the best way to create a dynamically sized
array, and maintain it effectively (yeah, I know about the wonders
of automatc garbage collection, and all that in C#), Any good code snippets
as examples would be wonderful.

Thanks in advance,

Carlos.

Nov 17 '05 #3
Carlos <ch******@yahoo.com> wrote:
I am from the C++ world, and just making baby steps into C#. Just
wanted to ask you about the best way to create a dynamically sized
array, and maintain it effectively (yeah, I know about the wonders
of automatc garbage collection, and all that in C#), Any good code snippets
as examples would be wonderful.


Arrays themselves are always fixed in size. However, you can wrap the
array in another class, and create a new array, populating it with the
contents of the old one, when it needs to change size.

This is already done for you in the framework, with ArrayList.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Jon Skeet [C# MVP] wrote:
Carlos <ch******@yahoo.com> wrote:
I am from the C++ world, and just making baby steps into C#. Just
wanted to ask you about the best way to create a dynamically sized
array, and maintain it effectively (yeah, I know about the wonders
of automatc garbage collection, and all that in C#), Any good code snippets
as examples would be wonderful.

Arrays themselves are always fixed in size. However, you can wrap the
array in another class, and create a new array, populating it with the
contents of the old one, when it needs to change size.

This is already done for you in the framework, with ArrayList.

Hm, however, the ArrayList is not a Wrapper for an Array with add and
remove methods, It works quite different inside (at least in java) and
is more a singel dimensional container for data ordered by an index as
keys (like an array ;) ), but with dynamical size (hm, however I don't
actually know the datastructur behind, I would bet simple dyn. pointer
on a field, or liked list...).

So what I meand is that they do not always create a new array and copy
the old values when the size changes.

;)
Jörg Simon
Nov 17 '05 #5
Joerg Simon <j_*****@sbox.tugraz.at> wrote:
Arrays themselves are always fixed in size. However, you can wrap the
array in another class, and create a new array, populating it with the
contents of the old one, when it needs to change size.

This is already done for you in the framework, with ArrayList.
Hm, however, the ArrayList is not a Wrapper for an Array with add and
remove methods, It works quite different inside (at least in java) and
is more a singel dimensional container for data ordered by an index as
keys (like an array ;) ), but with dynamical size (hm, however I don't
actually know the datastructur behind, I would bet simple dyn. pointer
on a field, or liked list...).


Nope, it does use an array internally - hence the name.
So what I meand is that they do not always create a new array and copy
the old values when the size changes.


Indeed - it keeps a buffer which is potentially larger than the size of
the list.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Kolber | last post by:
I did a little searching on this but couldn't find an answer... On my website, I have a section of stories (www.midlifeflight.com/stories) There are different stores on different pages that are...
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 =...
16
by: dylanthomasfan | last post by:
I want to know how one can use and manipulate byte-sized integers in C. Byte-sized integears are useful where one uses them in arrays declared in the heap, so it makes sense not to declare ints...
4
by: fabio | last post by:
Hello, zero-sized arrays (which I find very useful, since I do a lot of packet-style programming with circular buffers) cannot be declared if they're in a struct/class that later will be inherited...
4
by: marko.suonpera | last post by:
How to create a buffer of memory in C++, whose size can dynamically grow and shrink as needed? This is used for buffering input/output. Several variable types, such as int and double are read and...
7
by: Ole Nielsby | last post by:
I want to create a class with a variably-sized array inlined. The array size will be known at each instantiation but not at compile time. Once created, arrays won't grow or shrink and their...
6
by: bwaichu | last post by:
Is my understanding of the allocation of these correct? I used fixed sized allocations for the example below, so I realize there is some optimization that can be done to those. I would like to...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.