Redim save in c #?

I was shocked to learn today that C # does not support dynamic size arrays. How then did the VB.NET developer use ReDim Preserve to handle this in C #?

At the beginning of the function, I'm not sure about the upper bound of the array. It depends on the rows returned from the database.

+30
arrays c #
Nov 29 '08 at 19:44
source share
7 answers

Use ArrayLists or Generics instead

+11
Nov 29 '08 at 19:47
source share

VB.NET has no idea about dynamically sized arrays: the CLR does not support it.

Equivalent to "Redim Preserve" Array.Resize<T> - but you should be aware that if there are other references to the original array, they will not 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 

Two programs are equivalent.

If you really need a collection with dynamic size, you should use List<T> (or something similar). There are various issues with using arrays directly - see Eric Lippert's blog post . This does not mean that you should always avoid them, by any means, but you need to know what you are dealing with.

+66
Nov 29 '08 at 20:12
source share

Use the <T> list. It will dynamically change as needed.

+10
Nov 29 '08 at 19:46
source share

You really shouldn't use ReDim, it can be expensive. I prefer List (Of T), but there are many options in this area.

However, you have a question, and here is your answer.

 x = (int[]) Utils.CopyArray((Array) x, new int[10]); 
+4
Nov 29 '08 at 19:58
source share

Just for fun, here is one way to use generics to redo / expand a one-dimensional array (add another β€œrow”):

 static T[] Redim<T>(T[] arr, bool preserved) { int arrLength = arr.Length; T[] arrRedimed = new T[arrLength + 1]; if (preserved) { for (int i = 0; i < arrLength; i++) { arrRedimed[i] = arr[i]; } } return arrRedimed; } 

And one, to add n lines (although this does not prevent the user from overriding the array, which will cause an error in the for loop):

 static T[] Redim<T>(T[] arr, bool preserved, int nbRows) { T[] arrRedimed = new T[nbRows]; if (preserved) { for (int i = 0; i < arr.Length; i++) { arrRedimed[i] = arr[i]; } } return arrRedimed; } 

I am sure you understand.

For a multidimensional array (two dimensions), there is one possibility:

 static T[,] Redim<T>(T[,] arr, bool preserved) { int Ubound0 = arr.GetUpperBound(0); int Ubound1 = arr.GetUpperBound(1); T[,] arrRedimed = new T[Ubound0 + 1, Ubound1]; if (preserved) { for (int j = 0; j < Ubound1; j++) { for (int i = 0; i < Ubound0; i++) { arrRedimed[i, j] = arr[i, j]; } } } return arrRedimed; } 

In your program, use this with or even without the specified type, the compiler will recognize it:

 int[] myArr = new int[10]; myArr = Redim<int>(myArr, true); 

or

 int[] myArr = new int[10]; myArr = Redim(myArr, true); 

Not sure if this is all true. = D Feel free to correct me or improve the code .;)

+2
Dec 18 '13 at 18:28
source share

I could not help but notice that none of the above answers fits the concept of multidimensional arrays. What is said here is an example. The array in question is predefined as x .

 int[,] temp = new int[newRows, newCols]; int minRows = Math.Min(newRows, x.GetUpperBound(0) + 1); int minCols = Math.Min(newCols, x.GetUpperBound(1) + 1); for (int i = 0; i < minRows ; ++i) for (int j = 0; j < minCols; ++j) temp[i, j] = x[i, j]; x = temp; 
+1
Nov 18
source share

Despite the fact that for a long time this could help someone find a simple solution - I found something different in another forum:

 //from Applied Microsoft.NET framework Programming - Jeffrey Richter public static Array RedimPreserve(Array origArray, Int32 desiredSize) { System.Type t = origArray.GetType().GetElementType(); Array newArray = Array.CreateInstance(t, desiredSize); Array.Copy(origArray, 0, newArray, 0, Math.Min(origArray.Length, desiredSize)); return newArray; } 

Source: https://social.msdn.microsoft.com/Forums/en-US/6759816b-d525-4752-a3c8-9eb5f4a5b194/redim-in-c?forum=csharplanguage

+1
Nov 12 '15 at 6:33
source share



All Articles