Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new array using content of other arrays in C#

Tags:

arrays

c#

I would like to know the most efficient way of creating new arrays from the content of other arrays.

I have several arrays of strings of the type:

public readonly string[] Taiwan = { "TW", "TWO" };
public readonly string[] Vietnam = { "HM", "HN", "HNO" };
public readonly string[] Korea = { "KQ", "KS" };
public readonly string[] China = { "SS", "SZ" };
public readonly string[] Japan = { "T", "OS", "NG", "FU", "SP", "Q", "OJ", "JNX", "IXJ", "KAB", "JA", "JPx" };

It would be possible to create now a new array of string in a similar way to this?

public readonly string[] ASIA = { Taiwan, Vietnam, Korea, China, Japan};

That would contain all the strings included in the other arrays.

like image 655
lostborion Avatar asked Jul 19 '13 19:07

lostborion


People also ask

How do I assign contents of one array to another?

Ensure that the two arrays have the same rank (number of dimensions) and compatible element data types. Use a standard assignment statement to assign the source array to the destination array. Do not follow either array name with parentheses.

Can you create an array with different data types in C?

There are various ways in which an array can be declared and initialized in various ways. You can declare an array of any data type (i.e. int, float, double, char) in C.

How do I create a new array in C?

To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100};


1 Answers

string[] ASIA = new []{ Taiwan, Vietnam, Korea, China, Japan}
                      .SelectMany(s => s)  // not entirely sure what these abbreviations are...
                      .ToArray();
like image 112
Austin Salonen Avatar answered Oct 18 '22 07:10

Austin Salonen