.Net学习难点讨论系列16 - 索引器
索引器是一个我们经常打交道的特性,在编程过程中,多多少少都会用到索引器。而关于索引器一些高级话题,如给自定义的类添加索引器等也是本文着重介绍的。索引器本质上是一组get和set访问器, []中提供的是get访问器查找元素所要的参数,以及查找set访问器所要设置的元素时使用的参数。一个类或结构中只能有一个索引器且名称只能为this(但是索引器可以重载,重载方法就是提供不同类型的索引参数,这也是特别值得注意的是一点,索引器可以有多个索引参数,即下面语法结构的[]中可以提供多个参数(当然这样使用索引器时也要提供相应数量的参数))。
索引器的语法结构
1 public type this[type index] 2 { 3 get 4 { 5 GetAccessor 6 } 7 set 8 { 9 SetAccessor 10 } 11 }
第一个type是索引器(返回值)的类型,正如定义属性中的类型。而第二个type是索引的类型。类似于属性,定义的索引器也不会被分配命名空间,且get,set可以只实现一个或者都实现,但属性通常用于访问单个数据成员,索引用于访问多个数据成员。
在索引器中应该对索引器进行范围检查防止出现异常。另外不可以声明static的索引器。示例:下列自定义类型提供了一个索引器以使我们用更方便的方法来操作整型中的位。
1 struct IntBits
2 {
3 public IntBits(int initialBitValue)
4 {
5 bits = initialBitValue;
6 }
7
8 private int bits;
9
10 public bool this[int index]
11 {
12 get
13 {
14 return (bits & (1 << index)) != 0;
15 }
16 set
17 {
18 //如果value为true,就开启比特,否则把它关闭
19 if (value)
20 bits |= (1 << index);
21 else
22 bits &= -(1 << index);
23 }
24 }
25 }
这个类的使用方式:
1 int adapted = 63; 2 IntBits bits = new IntBits(adapted); 3 //获取索引位置6的bool值 4 bool peek = bits[6]; 5 //将索引0的比特设为true 6 bits[0] = true; 7 //将索引31的比特设为false 8 bits[31] = false;
索引器的下标可以是字符串等非整数。索引器不可以作为ref或者out参数。
下面再提供另一个索引器定义的示例:
1 class Employee 2 { 3 public string LastName; 4 public string FirstName; 5 public string CityOfBirth; 6 7 public string this[int index] 8 { 9 set 10 { 11 switch (index) 12 { 13 case 0: 14 LastName = value; 15 break; 16 case 1: 17 FirstName = value; 18 break; 19 case 2: 20 CityOfBirth = value; 21 break; 22 default: 23 throw new ArgumentOutOfRangeException("index"); 24 } 25 } 26 get 27 { 28 switch (index) 29 { 30 case 0: 31 return LastName; 32 case 1: 33 return FirstName; 34 case 2: 35 return CityOfBirth; 36 default: 37 throw new ArgumentOutOfRangeException("index"); 38 } 39 } 40 } 41 }
下面的代码展示了索引器重载的代码结构:
1 class MyClass 2 { 3 public string this[int index] 4 { 5 set { … } 6 get { … } 7 } 8 9 public string this[int index1, int index2] 10 { 11 set { … } 12 get { … } 13 } 14 15 public string this[float index] 16 { 17 set { … } 18 get { … } 19 } 20 }
索引器中访问器的访问修饰符与属性访问器的遵守一致的规则:
Ø 当属性或索引既有get访问器也有set访问器时才能给访问器添加访问修饰符。
Ø 只能给两个访问器中的一个添加访问修饰符。
Ø 访问器的修饰符需要比成员的访问级别有更严格的限制。
另外,像是HashTable等字典类内部使用了索引器,从而可以使下列代码1用代码2这种更易懂的方式来表示:
方式1:
1 Hashtable ages = new Hashtable(); 2 ages.Add("John", 23);
方式2:
1 ages["Green"] = 36;
接口中的索引器
类似于接口中定义的属性的方式,在索引器中也定义索引器的get与set访问器也不写实现而是直接给出一个分号。
1 interface IRawInt 2 { 3 bool this[int index] { get; set; } 4 }
接口的实现分隐式与显式,隐式实现时,可以标记public等,也可以标记virtual已被其子类重写。如果是显式实现则不能标记public,virtual等。
索引器中还结合泛型的使用,如Dictionary<T,U>中索引器的实现形如:
1 public virtual U this[T key]{ get; set; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2009-05-31 [hystar整理]Entity Framework 教程