上一篇: C# 属性(Property)

下一篇: C# 委托(Delegate)

C# 索引器(Indexer)

在本教程中,您将学习如何在 C# 中使用索引器。索引器是一种特殊的成员,允许您使用数组语法访问对象的内部集合。

基本概念

以下是与索引器相关的基本概念:

  • 索引器 :一种使类、结构或接口的对象可以像数组一样使用索引进行访问的成员。
  • 关键字 this :用于创建索引器的特殊关键字。

创建索引器

首先,创建一个简单的自定义集合类,名为 StringCollection。然后,为该类添加一个索引器。

                public class StringCollection
                {
                    private List<string> _items = new List<string>();

                    public string this[int index]
                    {
                        get { return _items[index]; }
                        set { _items.Insert(index, value); }
                    }

                    public int Count => _items.Count;

                    public void Add(string item)
                    {
                        _items.Add(item);
                    }
                }
                

在这个例子中,使用关键字 this 定义了一个索引器,并提供了 getset 访问器。get 访问器用于返回指定索引处的元素,而 set 访问器用于插入新元素。

使用索引器

现在可以像使用数组一样访问 StringCollection 对象的元素。

                class Program
                {
                    static void Main(string[] args)
                    {
                        StringCollection collection = new StringCollection();

                        // 使用 Add 方法添加元素
                        collection.Add("Item 1");
                        collection.Add("Item 2");

                        // 使用索引器访问和修改元素
                        Console.WriteLine("First item: " + collection[0]);
                        collection[1] = "Updated Item 2";
                        Console.WriteLine("Second item: " + collection[1]);

                        Console.ReadLine();
                    }
                }
                

输出结果:

                First item: Item 1
                Second item: Updated Item 2
                

创建多维索引器

还可以创建具有多个参数的索引器,以支持多维数组语法。

                public double this[int row, int column]
                {
                    get { return _data[row, column]; }
                    set { _data[row, column] = value; }
                }
                public class Matrix
                {
                    private double[,] _data;

                    public Matrix(int rows, int columns)
                    {
                        _data = newdouble[rows, columns];
                    }
                }
                

在这个例子中,Matrix 类使用一个二维数组 _data 存储数据。索引器接受两个参数(行和列),并通过 getset 访问器访问对应的元素。

您可以像使用多维数组一样访问 Matrix 对象的元素。

                class Program
                {
                    static void Main(string[] args)
                    {
                        Matrix matrix = new Matrix(3, 3);

                        // 使用多维索引器设置元素值
                        matrix[0, 0] = 1;
                        matrix[1, 1] = 2;
                        matrix[2, 2] = 3;

                        // 使用多维索引器获取元素值
                        Console.WriteLine("Matrix diagonal:");
                        for (int i = 0; i < 3; i++)
                        {
                            Console.Write(matrix[i, i] + " ");
                        }

                        Console.ReadLine();
                    }

                

输出结果:

                Matrix diagonal:
                1 2 3
                

总结:在本教程中,您学习了如何在 C# 中使用索引器,创建单维和多维索引器,以及如何使用它们访问对象的内部集合。现在,您可以使用索引器为您的自定义集合类添加更直观的访问方式。