Array

  • Thread starter Thread starter Aleksey
  • Start date Start date
A

Aleksey

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
 
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
 
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 said:
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/
 
Back
Top