有人可以帮我分配多个字符串数组到一个2d字符串数组?

时间:2015-05-25 09:44:14

标签: c# arrays string multidimensional-array variable-assignment

在C#中,有人可以帮我分配多个字符串数组到二维字符串数组吗?

这是我的代码:

<form action="contact.php" method="post">
        Your name<br>
        <input type="text" name="cf_name" ><br>
        <br />
        Your e-mail<br>
        <input type="text" name="cf_email"><br>
        <br />
        Do you want to join my mailing list?<br>
        <input type="radio" name="mail-list" value="yes">Yes<br>
        <input type="radio" name="mail-list" value="no">No<br>
        <input type="radio" name="mail-list" value="I_am_already_in_it">I am   already in it<br>
        <br />
        Message<br>
        <textarea name="cf_message" rows="10" cols="100">

        </textarea><br>
            <input type="submit" value="Send">
            <input type="reset" value="Clear">
    </form>

我为每个2d分配收到以下错误:

  

[]内的索引数目错误;预期2

我在上面的代码中做错了什么?

提前致谢。

3 个答案:

答案 0 :(得分:2)

您必须为2D数组指定两个标记,例如

allTestStrings[0, 0] = test1[0];
allTestStrings[0, 1] = test1[1];

您可以在循环中提取一个方法来执行此操作:

for (var i = 0; i < test1.Length; i++)
{
    allTestStrings[0, i] = test1[i];
}

答案 1 :(得分:0)

您可以像这样初始化它:

string[,] arr = {
                    { "one", "two", "three", "four", "five" },
                    { "one", "two", "three", "four", "five" },
                    { "one", "two", "three", "four", "five" },
                };

MSDN: Multidimensional Arrays (C# Programming Guide)

答案 2 :(得分:0)

你可以使用锯齿状数组,像这样:

string[][] allTestStrings = new string[4][];
            allTestStrings[0] = test1;
            allTestStrings[1] = test2;
            allTestStrings[2] = test3;
            allTestStrings[3] = test4;