C#接受索引的类型

时间:2017-02-22 17:10:15

标签: c# indexing

内置索引的所有类型是什么?例如,我知道ICollection和IEnumerable没有,但我想要列出所有这些。

除了列表之外,如果有人可以提供,我的具体问题是处理索引网格上的所有x,y点,所以我想通过索引访问x,y,如果可能的话。

由于

1 个答案:

答案 0 :(得分:1)

我已经对mscorlib进行了快速检查,结果如下; 在命名空间System.Collections;

ArrayList
BitArray
HashTable
IDictionary, hence all the implementations.
IList, hence all the implementations.
SortedList

在命名空间System.Collections.Generic

Dictionary<T, T>
IDictionary<T, T>, hence all the implementations.
IList<T>, hence all the implementations.
IReadOnlyDictionary<T, T>, hence all the implementations.
IReadOnlyList<T>, hence all the implementations.
List<T>, hence all the implementations.

你也有阵列。 但是,我建议您为自己的特定任务使用自定义类。 您需要提供有关x,y的网格元素或对象的信息。是由用户决定的吗?这是你提供的东西吗?

这是一个简单的通用方法。

class GridManager<T>
{
    private T[,] _matrix;

    public GridManager(int rows, int columns)
    {
        _matrix = new T[rows, columns];
    }

    public T this[int i, int j]
    {
        get
        {
            return _matrix[i, j];
        }
        set
        {
            _matrix[i, j] = value;
        }
    }
}
相关问题