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

Array

P: n/a
Hi All!

Does anybody know how can I change length of existing array. In Delphi I
used dynamic array and procedure SetLength. Is any similar procedure in C#.
For instance, I have array with 100 elements, and I need change its lenght
to 20 without lost data in it.

Thanks a lot
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
>Does anybody know how can I change length of existing array. In Delphi I
used dynamic array and procedure SetLength. Is any similar procedure in C#.
For instance, I have array with 100 elements, and I need change its lenght
to 20 without lost data in it.


You can't change the length of an array in C#. You may want to use an
ArrayList instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

P: n/a
There is an equivalent - i.e., similar code to what VB is doing behind the
scenes and similar performance (produced with our Instant C# VB to C#
converter):
e.g.,
VB:
Dim YourArray() As Integer
....
ReDim Preserve YourArray(i)

C#:
int[] YourArray;
....
int[] temp = new int[i + 1];
if (YourArray != null)
Array.Copy(YourArray, temp, Math.Min(YourArray.Length, temp.Length));
YourArray = temp;

As the other poster said, ArrayList will very often be a better solution,
but at least you have the equivalent now.

--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
"Aleksey" wrote:
Hi All!

Does anybody know how can I change length of existing array. In Delphi I
used dynamic array and procedure SetLength. Is any similar procedure in C#.
For instance, I have array with 100 elements, and I need change its lenght
to 20 without lost data in it.

Thanks a lot

Nov 17 '05 #3

P: n/a
"Aleksey" <al************@il.quest.com> wrote in
news:uk**************@TK2MSFTNGP12.phx.gbl:
Does anybody know how can I change length of existing array. In Delphi
I used dynamic array and procedure SetLength. Is any similar procedure
in C#. For instance, I have array with 100 elements, and I need change
its lenght to 20 without lost data in it.


Be careful if using this in Delphi.NET. It supports it for compatibility, but it actually creates a new
array and copies the old one into it on .NET.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.