首页 > 解决方案 > C#访问与多维数组混合的锯齿状数组中的行

问题描述

我想ith通过使用简单地访问该行street[i]

    int[][,] street = new int[7][,] { 
        new int [,] { {0, 0}, {0, 0}},
        new int [,] { {0, 1 }, {0, -1} }, 
        new int [,] { {-1, 0 }, {1, 0} },
        new int [,] { {1, 0 }, {0, -1} }, 
        new int [,] { {1, 0 }, {0, 1} }, 
        new int [,] { {-1, 0 }, {0, -1} }, 
        new int [,] { {-1, 0 }, {0, 1} }, 
    };
    System.Console.WriteLine(street[0]); # print out System.Int32[,]

我本以为会得到[[0,0], [0,0]],但扔掉了System.Int32[,]。为什么?

标签: c#arrays

解决方案


您可以序列化对象并打印结果。
我将使用Newtonsoft.Json你可以使用任何你喜欢的序列化器。

Install-Package Newtonsoft.Json

创建数组后,只需序列化对象并打印结果。

using Newtonsoft.Json;
int[][,] street = new int[7][,] { 
        new int [,] { {0, 0}, {0, 0}},
        new int [,] { {0, 1 }, {0, -1} }, 
        new int [,] { {-1, 0 }, {1, 0} },
        new int [,] { {1, 0 }, {0, -1} }, 
        new int [,] { {1, 0 }, {0, 1} }, 
        new int [,] { {-1, 0 }, {0, -1} }, 
        new int [,] { {-1, 0 }, {0, 1} }, 
    };
Console.WriteLine(JsonConvert.SerializeObject(street[0]));
//Or you can print all the array
//Console.WriteLine(JsonConvert.SerializeObject(street));

推荐阅读