索引器Indexer是 C# 中非常重要的一个特性它允许对象像数组一样通过[]来访问数据而实际上访问的是对象内部的数据。例如Student stu new Student(); stu[0] 张三; // 看起来像数组 Console.WriteLine(stu[0]);实际上调用的是对象内部的方法而不是数组。一、为什么需要索引器先看一个没有索引器的例子。class Student { private string[] names new string[5]; public string GetName(int index) { return names[index]; } public void SetName(int index, string value) { names[index] value; } }使用Student stu new Student(); stu.SetName(0, Tom); Console.WriteLine(stu.GetName(0));虽然能实现功能但是写起来很麻烦。于是 C# 提供了索引器。二、索引器基本语法class 类名 { public 返回值类型 this[参数] { get { //读取数据 } set { //写入数据 } } }注意必须使用this没有名字使用中括号[]可以有 get可以有 set三、第一个索引器class Student { private string[] names new string[5]; public string this[int index] { get { return names[index]; } set { names[index] value; } } }调用Student stu new Student(); stu[0] Tom; stu[1] Jack; Console.WriteLine(stu[0]); Console.WriteLine(stu[1]);输出Tom Jack是不是和数组一模一样其实stu[0]编译器会转换成stu.get_Item(0)而stu[0] Tom转换成stu.set_Item(0,Tom)四、get 和 setget读取数据get { return names[index]; }例如Console.WriteLine(stu[0]);会调用getset写数据set { names[index] value; }这里有一个隐藏变量value例如stu[0] Tom;那么value Tom五、只读索引器只有 getpublic string this[int index] { get { return names[index]; } }不能写stu[0] Tom; //错误只能读Console.WriteLine(stu[0]);六、只写索引器只有 setpublic string this[int index] { set { names[index] value; } }可以stu[0] Tom;不能Console.WriteLine(stu[0]);很少使用。七、索引器可以有多个参数例如二维数组。class Matrix { private int[,] arr new int[3,3]; public int this[int row,int col] { get { return arr[row,col]; } set { arr[row,col]value; } } }使用Matrix m new Matrix(); m[1,2]100; Console.WriteLine(m[1,2]);输出100八、索引器参数不限于 int字符串也可以。例如字典class Student { Dictionarystring,int score new Dictionarystring,int(); public int this[string name] { get { return score[name]; } set { score[name]value; } } }使用Student stu new Student(); stu[Tom]95; stu[Jack]88; Console.WriteLine(stu[Tom]);输出95九、一个类可以有多个索引器重载class Test { int[] arr new int[10]; Dictionarystring,int dic new Dictionarystring,int(); public int this[int index] { get { return arr[index]; } set { arr[index]value; } } public int this[string key] { get { return dic[key]; } set { dic[key]value; } } }使用Test t new Test(); t[0]10; t[数学]98;这就是索引器重载。十、索引器和属性的区别属性(Property)索引器(Indexer)有名字没有名字使用.访问使用[]访问一个对象一般多个属性一个对象一般只有几个索引器不带参数可以带参数属性student.Name索引器student[0]十一、索引器和数组有什么区别数组int[] arr new int[5]; arr[0]100;索引器Student stunew Student(); stu[0]100;区别数组是真正存储数据。索引器只是访问对象内部数据的一种方式。对象里面可能存的是数组ListDictionary数据库文件网络数据用户都不知道。十二、实际开发案例——自定义图书馆class Library { private Liststring books new Liststring(); public string this[int index] { get { return books[index]; } set { books[index]value; } } public void Add(string book) { books.Add(book); } }使用Library lib new Library(); lib.Add(C#); lib.Add(Java); Console.WriteLine(lib[0]); Console.WriteLine(lib[1]); lib[1]Python; Console.WriteLine(lib[1]);输出C# Java Python十三、索引器常见面试题1. 索引器有名字吗没有固定使用this。2. 一个类可以定义多个索引器吗可以只要参数列表不同重载。3. 索引器必须有 get 和 set 吗不必须可以只有get或只有set。4. 索引器可以是静态的吗不能直接声明为static索引器。索引器属于对象实例因此通常通过实例访问。5. List 为什么可以这样写list[0]因为ListT内部实现了索引器public T this[int index] { get; set; }所以我们才能像访问数组一样访问List。十四、学习总结记住下面这个模板几乎所有索引器都可以由它演变而来class Demo { private int[] data new int[10]; public int this[int index] { get { return data[index]; } set { data[index] value; } } }核心要点索引器使用this[...]声明没有名称。本质是get/set方法的语法糖让对象可以像数组一样通过[]访问。可以重载参数不限于int也可以使用string、多个参数等。常用于封装ListT、DictionaryTKey,TValue等集合提高类的易用性和可读性。掌握索引器后建议继续学习运算符重载 → 委托Delegate→ 事件Event→ 泛型Generic→ LINQ这些都是 C# 面向对象和高级编程中的重要知识点