Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redim Preserve in C#?

Tags:

arrays

c#

I was shocked to find out today that C# does not support dynamic sized arrays. How then does a VB.NET developer used to using ReDim Preserve deal with this in C#?

At the beginning of the function I am not sure of the upper bound of the array. This depends on the rows returned from the database.

like image 717
JL. Avatar asked Nov 29 '08 19:11

JL.


People also ask

What does ReDim preserve do?

The ReDim statement cannot change the rank (the number of dimensions) of the array. Resizing with Preserve. If you use Preserve , you can resize only the last dimension of the array. For every other dimension, you must specify the bound of the existing array.

What is the difference between ReDim and ReDim preserve?

Redim Statement is used to redefine the size of an Array. When the array is declared without any size, then it can be declared again using Redim with the feasibility of specifying the size of an array. The preserve keyword is used to preserve the contents of a current array when the size of an array gets changed.

Can I resize an array in C?

There is no way to resize an array. You can simply create a new array of size 2, then copy all the data from the previous one to the new one. realloc does it for you with dynamic memory.

How do I ReDim preserve a two dimensional array?

ReDim Preserve any dimension of a 2D Array If you'd like to ReDim Preserve a multidimensional array larger than two-dimensions, your best bet is to construct your array in such a way that only the number of elements in the last dimension will need to be preserved.


2 Answers

VB.NET doesn't have the idea of dynamically sized arrays, either - the CLR doesn't support it.

The equivalent of "Redim Preserve" is Array.Resize<T> - but you must be aware that if there are other references to the original array, they won't be changed at all. For example:

using System;  class Foo {     static void Main()     {         string[] x = new string[10];         string[] y = x;          Array.Resize(ref x, 20);         Console.WriteLine(x.Length); // Prints out 20         Console.WriteLine(y.Length); // Still prints out 10     } } 

Proof that this is the equivalent of Redim Preserve:

Imports System  Class Foo     Shared Sub Main()         Dim x(9) as String         Dim y as String() = x          Redim Preserve x(19)         Console.WriteLine(x.Length)         Console.WriteLine(y.Length)     End Sub End Class 

The two programs are equivalent.

If you truly want a dynamically sized collection, you should use List<T> (or something similar). There are various issues with using arrays directly - see Eric Lippert's blog post for details. That's not to say you should always avoid them, by any means - but you need to know what you're dealing with.

like image 168
Jon Skeet Avatar answered Sep 20 '22 09:09

Jon Skeet


Use ArrayLists or Generics instead

like image 23
Michael L Avatar answered Sep 22 '22 09:09

Michael L