dynamically sized arrays

  • Thread starter Thread starter Carlos
  • Start date Start date
C

Carlos

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.
 
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
 
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/issues/1100/GCI/default.aspx

http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/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/default.asp?url=/library/en-us/dncscol/html/deepc10192000.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 said:
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 said:
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 said:
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.
 
Back
Top