6

I am working on a C# app that will calculate some values. I need to put those values in a x-by-x array of strings. If I knew that "x" was for example, I know that I could just do:

string[,] matrix = new string[3, 3]; 

Unfortunately, I do not know what "x" will be. Is there a way to dynamically grow a matrix in C#? If so, how?

user70192
  • 13,786
  • 51
  • 160
  • 240
  • 2
    Create a list of lists. See http://stackoverflow.com/questions/12628222/creating-a-list-of-lists-in-c-sharp – Tony Hinkle Mar 04 '16 at 13:58
  • Does it have to be an array? If not, consider a List or different type of collection. Array lengths can't change. See also http://stackoverflow.com/questions/683073/how-to-set-array-length-in-c-sharp-dynamically – Ronnie Mar 04 '16 at 13:59
  • http://stackoverflow.com/questions/6539571/how-to-resize-multidimensional-2d-array-in-c – Dmitry Bychenko Mar 04 '16 at 14:02

2 Answers2

5

you can define the size of the array with variables and then change their values at runtime

int arrayWidth = 3;
int arrayHeight = 3;
string[,] matrix = new string[arrayWidth, arrayHeight]; 

however, as darin pointed out, arrays cannot be resized; so be sure to leave the initialisation right until you size values are confirmed.

Souhaieb Besbes
  • 1,485
  • 17
  • 30
3

No, in C# arrays are of static size. They cannot be re-sized at runtime unless you declare a new array with the new size and then copy the elements from the old array to the new one (which depending on your specific needs might be feasible or not). So basically you could use some of the dynamic list structures in .NET such as IList<T> which allows you to add elements to it dynamically at runtime. Of course there are no miracles, under the covers a List<T> will use a .NET array for backing the data, except that it will re-size it intelligently as you are dynamically adding elements to this structure.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Nope. You can resize array in c# using static method Array.Resize – nicolas2008 Mar 04 '16 at 14:20
  • 1
    Of course. And did you read the [`documentation`](https://msdn.microsoft.com/en-us/library/hh159084(v=vs.110).aspx) of this method? In case you haven't lemme quote from it:`This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.array must be a one-dimensional array.` So let's repeat once again the answer I already provided here. In .NET arrays static size and cannot be re-sized at runtime unless you declare a new array with the new size and copy all the elements from the old to the new one. – Darin Dimitrov Mar 04 '16 at 14:24
  • So @nicolay.anykienko, could you please explain your downvote on this answer? How does in your opinion my answer provides wrong information (which is usually the case for downvoting). – Darin Dimitrov Mar 04 '16 at 14:33
  • You are right about behavior of Array.Resize method. Sorry, I'll remove downvote. However, information about Array.Resize would be not excessive in your answer. – nicolas2008 Mar 04 '16 at 16:50