By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

How to generate a variable-length array in C#?

P: n/a
I intend to generate a variable-length array, similar to link lists in plain C. For example, I define the following array

int [] a

Initially I assign 5 elements to the array as

a = new int[5]
for( k = 0; k < 5; k++
a[k] = k

Then I want to expand the array to 7 elements without discarding the original 5 elements. How to do
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
=?Utf-8?B?bW9vbnJpdmVy?= <xi*******@yahoo.com> wrote in
news:78**********************************@microsof t.com:
I intend to generate a variable-length array, similar to link lists in
plain C. For example, I define the following array:
<snip> Then I want to expand the array to 7 elements without discarding the
original 5 elements. How to do?


Create a new array with the new size and copy over the existing elements.
Then replace the existing array with the new one.

Something like:

Int32[] b = new Int32[7];
Array.Copy(a, 0, b, 0, 5);
a = b;

You might want to look at the ArrayList class too.

--
Lasse Vågsæther Karlsen
la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.