質問

I want to convert the code below

string[][] mergeData = new string[][] {
                          new string[] { "phone", "date"},
                          new string[] { "0421 3359 129", DateTime.Now.ToString() } };

to

       Dictionary<string, string> list = new Dictionary<string, string>();

        list.Add("phone", "0421 3359 129");
        list.Add("date", DateTime.Now.ToString());


        string[][] mergeData = new string[2][];

        int i = 0;
        foreach(KeyValuePair<string, string> pair in list)
        {
            mergeData[0][i] = pair.Key;
            mergeData[1][i] = pair.Value;
        i++;
        }  

But i got error message

object reference not set to an instance of an object

I suspect it has something to do with my jagged array init. I don't know how to properly initiate jagged array. And I must used jagged array.

Please help. Thanks.

役に立ちましたか?

解決

You get this message because the two individual arrays are not set in the constructor of the jagged array. Here is what you can do:

string[][] mergeData = new[] {
    new string[list.Count]
,   new string[list.Count]
};

This would create an array of two elements, each of the elements with list.Count elements of type string.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top