Sithered
Sithered

Reputation: 481

How to assign a single column of a multi dimensional array to a single dimensional one?

I am trying to create a one dimensional array from the first column of a two dimensional array, in C#. But this code does not seem to work

string[,] twoDArray;
//Some values are added to the array here

string[] oneDArray = twoDArray[,1];

How can I do it?

Upvotes: 0

Views: 239

Answers (1)

ASh
ASh

Reputation: 35730

you have to create a new array and fill it in a loop

string[,] twoDArray = new [,] {{"1","2"}, {"3","4"}, {"5","6"}};
int len = twoDArray.GetLength(0);
string[] oneDArray = new string[len];

for(int r=0;r<len; r++)
    oneDArray[r] = twoDArray[r,1];

Upvotes: 2

Related Questions