如何在 C# 中声明 int table 列表?

How to declare list of int table in c#?

我声明如下:

IList<int[]> populacja = new List<int[]>();

但我还想声明一个常量大小的 int table。所以我想要这样的东西

IList<int[2]> populacja = new List<int[2]>();

怎么做?什么是制作 int table 列表的好解决方案?

如果确保 populacja 始终为 int[2] 对您很重要,您可以将其包装在 class 中,然后列出 class。内置选项包括 Tuple,例如:

IList<Tuple<int,int>> populacja = new List<Tuple<int,int>>();

仅供参考。

您不能在 IList 中声明 int[2] 的原因是它需要一个类型。在这种情况下,类型是一个 int 数组 (int[])。 T 不关心长度或类似的东西。如果你确实想使用 int[] 你需要做这样的事情 --

        IList<int[]> populacja = new List<int[]>();
        populacja.Add(new int[2]); // empty int array of size 2
        populacja.Add(new [] { 3211,3212 }); // non-empty int array of size 2

每次添加新的 int 数组时,您都需要显式实例化它,大小为 2,因为没有限制。