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. 5 3183
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
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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:
...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |