Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串方法插入字符串数组_C#_Arrays - Fatal编程技术网

C# 将字符串方法插入字符串数组

C# 将字符串方法插入字符串数组,c#,arrays,C#,Arrays,我试图对一组值进行选择,每个方法都返回一个字符串数组,我试图将该数组放入main中的字符串数组中。代码如下: string[] array = { MethodA1(), MethodA2(), MethodA3() }; 我正在尝试创建字符串[]数组,仅一项。这是有效的:(但它只允许用户显示/选择一个值) 有人能想出一种方法允许多个方法返回到一个数组中吗?类似的东西 string[] array = MethodA1().Concat(MethodA2()).Concat(MethodA3(

我试图对一组值进行选择,每个方法都返回一个字符串数组,我试图将该数组放入main中的字符串数组中。代码如下:

string[] array = { MethodA1(), MethodA2(), MethodA3() };
我正在尝试创建字符串[]数组,仅一项。这是有效的:(但它只允许用户显示/选择一个值)

有人能想出一种方法允许多个方法返回到一个数组中吗?

类似的东西

string[] array = MethodA1().Concat(MethodA2()).Concat(MethodA3()).ToArray();
private string[] SelectFiles(object userInput)
{
    string[] array;

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

    return array;
}
编辑:从您的评论中可以看出,您实际上是在尝试实现完全不同的目标:根据参数选择其中一种方法的结果。你是说像这样的事吗

string[] array = MethodA1().Concat(MethodA2()).Concat(MethodA3()).ToArray();
private string[] SelectFiles(object userInput)
{
    string[] array;

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

    return array;
}

您可以使用
SelectMany
将所有内容展平为单个
字符串[]

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

也许你应该考虑< /P>
List<string>

尝试搜索@LasseV.Karlsen-修复。感谢您的回复,是否可以选择一个特定的方法而不是显示所有方法?:)这不就是你的问题吗?我想你可能需要给出一些具体的例子来说明你想要实现的目标,因为我不确定我是否理解你的问题。对不起,字符串数组中的项目是文件,一旦放入字符串[]数组中,这些文件就会发送到另一个方法,该方法会根据用户的选择将它们按升序或降序排序。我尝试过类似“if(userInput==1){string[]array={MethodA1();}”这样的if语句,但我无法从if语句中返回数组。我希望这能有所帮助?我认为您应该编辑您的问题,以包含以下内容(&e)一些上下文(例如更多代码!)。您询问如何组合多个方法的结果,但听起来您实际上希望根据其他参数选择要执行的方法。问题中没有明确说明这一点。