将字符串方法插入字符串数组中

Yrn*_*neh -5 c# arrays

我试图选择一组值,每个方法返回一个字符串数组,我试图将其放入主要的字符串数组中.这是代码:

string[] array = { MethodA1(), MethodA2(), MethodA3() };
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建string []数组,只有一个项目.这有效:(但它只允许用户显示/选择一个值)

string[] array = MethodA1();
Run Code Online (Sandbox Code Playgroud)

任何人都可以想到一种允许多个方法返回数组的方法吗?

Cha*_*ger 7

像这样的东西?

string[] array = MethodA1().Concat(MethodA2()).Concat(MethodA3()).ToArray();
Run Code Online (Sandbox Code Playgroud)

编辑:从您的评论中听起来,您实际上是在尝试实现完全不同的东西:根据参数选择其中一种方法的结果.你的意思是这样的吗?

private string[] SelectFiles(object userInput)
{
    string[] array;

    if (userInput == 1)
    {
        array = MethodA1();
    }
    else if (userInput == 2)
    {
        array = MethodA2();
    }
    else
    {
        array = MethodA3();
    }

    return array;
}
Run Code Online (Sandbox Code Playgroud)


Jus*_*ner 5

您可以使用SelectMany将所有内容展平为一个string[]:

string[] array = (new[] { MethodA1(), MethodA2(), MethodA3() })
    .SelectMany(a => a)
    .ToArray();
Run Code Online (Sandbox Code Playgroud)