在C#中为顺序搜索类型创建动态foreach循环

时间:2016-05-16 04:10:04

标签: c# arrays c#-4.0 foreach

首先分析我的C#代码。

        string[] shape = { "Round", "Square" };
        string[] Size = { "7", "9", "10" };
        string[] type = { "Chocklate", "Honey", "Vanila" };
        string[] top = { "Blank Cake", "Licenced Image Cake", "Personal Image Cake" };
        string[] msg = { "Happy Birthday", "Own Message", "Get Well Soon" };

        List<string> MyPatterns = new List<string>();

        for (int i = 0; i < shape.Length; i++)
        {
            MyPatterns.Add(shape[i]);
            for (int j = 0; j < Size.Length; j++)
            {
                MyPatterns.Add(shape[i] + " - " + Size[j]);
                for (int k = 0; k < type.Length; k++)
                {

                    string A = shape[i] + " - " + Size[j] + " - " + type[k];
                    MyPatterns.Add(A);

                    for (int l = 0; l < top.Length; l++)
                    {
                        string B = shape[i] + " - " + Size[j] + " - " + type[k] + " - " + top[l];
                        MyPatterns.Add(B);

                        for (int m = 0; m < msg.Length; m++)
                        {
                            string C = shape[i] + " - " + Size[j] + " - " + type[k] + " - " + top[l] + " - " + msg[m];
                            MyPatterns.Add(C);

                        }

                    }

                }


            }
        }

现在上面的示例有5个静态数组的形状,大小,类型,顶部,msg及以上代码显示这些数组之间的每个组合都是可能的。喜欢 回合 回合 - 7 Round - 7 - Chocklate就是这样所有可能的组合。

屏幕截图:

enter image description here

目前我有静态数组的形状,大小,类型等。所以我手动为上层数组中的每个数组编写foreach循环。

现在假设这些数组是动态的。我不知道计算遗嘱,例如,有时它的形状和大小必须是形状,大小和类型,有时它们完全不同。

那么如何为动态数组动态生成foreach循环?

2 个答案:

答案 0 :(得分:2)

将这些动态数组分组到一个数组中,因此你有一个数组数组(AKA Jagged Array。这样你就可以使用foreach遍历父数组。

string[][] arr = new string[5][];
arr[0] = new string [] { "Round", "Square" };
arr[1] = new string [] { "7", "9", "10" };
arr[2] = new string [] { "Chocklate", "Honey", "Vanila" };
arr[3] = new string [] { "Blank Cake", "Licenced Image Cake", "Personal Image Cake" };
arr[4] = new string [] { "Happy Birthday", "Own Message", "Get Well Soon" };

foreach (var a in arr) {
    Console.WriteLine("Array count:  " +  a.Length);
    //Now you can loop through each child array.
    foreach (string val in a) {
        //Do your stuff.
        Console.WriteLine("\t" + val);
    }
}

Here is a demo

答案 1 :(得分:0)

这是递归实现的典型候选者:

static void Main(string[] args)
{
    string[] shape = { "Round", "Square" };
    string[] size = { "7", "9", "10" };
    string[] type = { "Chocklate", "Honey", "Vanila" };
    string[] top = { "Blank Cake", "Licenced Image Cake", "Personal Image Cake" };
    string[] msg = { "Happy Birthday", "Own Message", "Get Well Soon" };

    IEnumerable<string[]> productRows = CartesianProductRecursive(new []{ shape, size, type, top, msg });

    foreach (var row in productRows)
    {
        Console.WriteLine("{0}, {1}, {2}, {3}, {4}", row[0], row[1], row[2], row[3], row[4]);
    }
}

public static IEnumerable<string[]> CartesianProductRecursive(IEnumerable<string[]> dimValues)
{
    if (!dimValues.Any())
        yield break;

    string[] firstDimValues = dimValues.First();
    IEnumerable<string[]> lastDimsProduct = CartesianProductRecursive(dimValues.Skip(1));

    if (!lastDimsProduct.Any())
        yield return firstDimValues;

    foreach (string firstDimValue in firstDimValues)
    {
        foreach (string[] lastDimsProductRow in lastDimsProduct)
        {
            yield return new[] {firstDimValue}.Concat(lastDimsProductRow).ToArray();
        }
    }
}

如果您正在执行测试,则可以使用NUnit cartesian product featureNCase test case generator。两者都允许通过生成成对产品而不是笛卡尔积来减少测试用例的数量。