索引器 C#
2024-08-22 19:41:57
概述
索引器允许类或结构的实例就像数组一样进行索引。 索引器类似于属性,不同之处在于它们的访问器采用参数。
在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分配和检索值的方法)。 Program 类为存储字符串创建了此类的一个实例。
Code
1 class SampleCollection<T>
2 {
3 // Declare an array to store the data elements.
4 private T[] arr = new T[100];
5
6 // Define the indexer, which will allow client code
7 // to use [] notation on the class instance itself.
8 // (See line 2 of code in Main below.)
9 public T this[int i]
10 {
11 get
12 {
13 // This indexer is very simple, and just returns or sets
14 // the corresponding element from the internal array.
15 return arr[i];
16 }
17 set
18 {
19 arr[i] = value;
20 }
21 }
22 }
23
24 // This class shows how client code uses the indexer.
25 class Program
26 {
27 static void Main(string[] args)
28 {
29 // Declare an instance of the SampleCollection type.
30 SampleCollection<string> stringCollection = new SampleCollection<string>();
31
32 // Use [] notation on the type.
33 stringCollection[0] = "Hello, World";
34 System.Console.WriteLine(stringCollection[0]);
35 }
36 }
索引器和属性比较
属性 |
索引器 |
---|---|
允许像调用公共数据成员一样调用方法。 |
允许对一个对象本身使用数组表示法来访问该对象内部集合中的元素。 |
可通过简单的名称进行访问。 |
可通过索引器进行访问。 |
可以为静态成员或实例成员。 |
必须为实例成员。 |
属性的 get 访问器没有参数。 |
索引器的 get 访问器具有与索引器相同的形参表。 |
属性的 set访问器包含隐式 value 参数。 |
除了值参数外,索引器的 set 访问器还具有与索引器相同的形参表。 |
支持对自动实现属性使用短语法。 |
不支持短语法。 |
接口中使用索引器
索引器可在接口上声明。 接口索引器的访问器与类索引器的访问器具有以下方面的不同:
接口访问器不使用修饰符。
接口访问器没有体。
因此,访问器的用途是指示索引器是读写、只读还是只写。
接口索引器访问器的示例:
1 public interface ISomeInterface
2 {
3 //...
4
5 string this[int index]
6 {
7 get;
8 set;
9 }
10 }
如何实现借口索引器示例
1 // Indexer on an interface:
2 public interface ISomeInterface
3 {
4 // Indexer declaration:
5 int this[int index]
6 {
7 get;
8 set;
9 }
10 }
11
12 // Implementing the interface.
13 class IndexerClass : ISomeInterface
14 {
15 private int[] arr = new int[100];
16 public int this[int index] // indexer declaration
17 {
18 get
19 {
20 // The arr object will throw IndexOutOfRange exception.
21 return arr[index];
22 }
23 set
24 {
25 arr[index] = value;
26 }
27 }
28 }
29
30 class MainClass
31 {
32 static void Main()
33 {
34 IndexerClass test = new IndexerClass();
35 System.Random rand = new System.Random();
36 // Call the indexer to initialize its elements.
37 for (int i = 0; i < 10; i++)
38 {
39 test[i] = rand.Next();
40 }
41 for (int i = 0; i < 10; i++)
42 {
43 System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
44 }
45
46 // Keep the console window open in debug mode.
47 System.Console.WriteLine("Press any key to exit.");
48 System.Console.ReadKey();
49 }
50 }
51 /* Sample output:
52 Element #0 = 360877544
53 Element #1 = 327058047
54 Element #2 = 1913480832
55 Element #3 = 1519039937
56 Element #4 = 601472233
57 Element #5 = 323352310
58 Element #6 = 1422639981
59 Element #7 = 1797892494
60 Element #8 = 875761049
61 Element #9 = 393083859
62 */
在上例中,可以通过使用接口成员的完全限定名来使用显式接口成员实现。 例如:
1 public string ISomeInterface.this
2 {
3 }
但是,只有当类使用同一索引器签名实现一个以上的接口时,为避免多义性才需要使用完全限定名。 例如,如果 Employee 类实现的是两个接口 ICitizen 和IEmployee,并且这两个接口具有相同的索引器签名,则必须使用显式接口成员实现。 即,以下索引器声明:
1 public string IEmployee.this
2 {
3 }
在 IEmployee 接口上实现索引器,而以下声明:
1 public string ICitizen.this
2 {
3 }
在 ICitizen 接口上实现索引器。
【本文由“白字先生”发布,2017年05月08日】
最新文章
- centos下编译安装lnmp
- a链接的四种状态
- python-getattr
- 用jsmooth + inno生成exe并制作简单安装包
- JavaScript Array 对象
- sql 查询每月的销售金额
- Word Ladder 未完成
- 笔记5:QQ群聊天机器人
- Understanding Design And Development Job Titles--reference
- C# 文件的读取、写入和删除
- Php官方指导安装与配置
- [转载]Silverlight实用窍门系列:71.Silverlight的Style
- HDU2114 Calculate S(n) (取模算术)
- vue.js+boostrap
- 基于模糊聚类和最小割的层次化网格分割算法(Hierarchical Mesh Decomposition)
- 利用Dijkstra算法实现记录每个结点的所有最短路径
- centos7中安装mysql5.6版本 + 主从复制
- Mysql 导入CSV文件,中文内容乱码问题
- 一句话的事儿,Head first 设计模式
- Fit项目图片上传和云存储的调通
热门文章
- STL 2&mdash;迭代器相关运算&mdash;&mdash;advance(),distance(),next(),prev()
- 使用filter对请求设置编码
- jq插件封装格式
- easyui validatebox textbox 使用例子
- Debian 鼠标左右手
- Android开发四大组件之Service(具体解释篇)
- WPF EventTrigger,BeginStoryboard
- 20155204《网络对抗》Exp8 Web基础
- 20155216 Exp6 信息搜集与漏洞扫描
- ES6 箭头函数易出错细节