8

How do I initialize a C # array with the dimensions decided at run time?

 3 years ago
source link: https://www.codesd.com/item/how-do-i-initialize-a-c-array-with-the-dimensions-decided-at-run-time.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I initialize a C # array with the dimensions decided at run time?

advertisements

and thanks for looking.

I have a 2D c# array, that has 50 as one of its dimensions. The other dimension depends on the number of rows in a database somewhere and is decided at runtime. How would I go about initializing an array like this?

Currently my initialization for a single row looks like this, but I'm sure there's a better way to do it, more efficiently :)

temp = new Double[50,1] { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},
                                {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},
                                {0},{0},{0},{0},{0},{0},{0},{0},{0},{0}};


Simply initialize the array at runtime using an integer variable for the second dimension.

double[,] = new double[50, v];

C# will automatically initialize all doubles to zero, so in your specific circumstance, you don't need to explicitly initialize the values.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK