Is there a way to ReDim in C#?

  • Thread starter Thread starter mb
  • Start date Start date
M

mb

I have an array that starts with 99 index. I use a random amount of the
indexes, let's say 23. Is there a way to truncate the array to 23 elements
while preserving the 23 used? The ReDim statement in VB does this. Is
there an equivalent in C#?
 
mb said:
I have an array that starts with 99 index. I use a random amount of the
indexes, let's say 23. Is there a way to truncate the array to 23
elements
while preserving the 23 used? The ReDim statement in VB does this. Is
there an equivalent in C#?

Not explicitly. You can use something like this to get equivilent
functinoality however:

Array ResizeArray(Array array, Type type, params int dimensions)
{
Array newArray = Array.CreateInstance(type, dimensions);
Buffer.BlockCopy(array, 0, newArray, 0, array.Length);
return newArray;
}

Which will do the basic job for you(its untested, fyi). It appears
 
MB,

Luckily not, when you have to do something with a redim, than the best you
can do is to change that part of your upgraded VB6 program in VBNet as well
direct with an array which uses IList or Icontainer, the most properiate is
mostly the arraylist in that case.

I hope this helps?

Cor
 
As Daniel & Cor already mentioned.. luckily there is not ReDim in C#. Either
write a function that populates the new array with values from the old one
or consider using box standard ArrayList object. You can choose to specify
the size or you can leave it to take however many items you feed in.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
Thank you.

Hermit Dave said:
As Daniel & Cor already mentioned.. luckily there is not ReDim in C#. Either
write a function that populates the new array with values from the old one
or consider using box standard ArrayList object. You can choose to specify
the size or you can leave it to take however many items you feed in.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
Other people have mentioned the ArrayList, please note that this operates on
'object'.
If you have valuetypes like int or double, you will get boxing/unboxing.
This may not cause a performance problem, but it's worth knowing about.

Chris
 
As Chris mentioned keep in mind that ArrayList is very generic.. using
object type so you will incure boxing / unboxing won't have a much
performance effect but its good to know.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
As the others have mentioned, you may want to rethink your approach in
.NET.

However, there is a 4-line equivalent in C# which does what VB.NET
does behind the scenes (performance is identical), as our Instant C#
VB.NET to C# converter shows:

VB.NET:

Sub TestReDim()
Dim x() As Integer
ReDim x (2)
ReDim Preserve x(3)
End Sub

C#:

private void TestReDim()
{
int[] x = null;
x = new int [3];
//INSTANT C# NOTE: The following 4 lines reproduce what 'ReDim
Preserve' does behind the scenes in VB.NET:
//ORIGINAL LINE: ReDim Preserve x(3)
int[] Temp1 = new int[4];
if (x != null)
System.Array.Copy(x, Temp1, System.Math.Min(x.Length,
Temp1.Length));
x = Temp1;
}
 
Back
Top