概述

索引器允许类或结构的实例就像数组一样进行索引。 索引器类似于属性,不同之处在于它们的访问器采用参数。

在下面的示例中,定义了一个泛型类,并为其提供了简单的 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日】

索引器 C#的更多相关文章

  1. 【.net 深呼吸】细说CodeDom(7):索引器

    在开始正题之前,先补充一点前面的内容. 在方法中,如果要引用方法参数,前面的示例中,老周使用的是 CodeVariableReferenceExpression 类,它用于引用变量,也适用于引用方法参 ...

  2. C# 索引器,实现IEnumerable接口的GetEnumerator()方法

    当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...

  3. C#基础回顾(三)—索引器、委托、反射

    一.前言                                                                                       ------人生路 ...

  4. C#索引器

    索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...

  5. C#之索引器

    实际中不使用这个东西,只做了解 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  6. C#属性-索引器-里氏替换-多态-虚方法-抽象-接口-泛型-

    1.属性 //属性的2种写法 public class person { private string _name; public string Name { get { return _name; ...

  7. 《精通C#》索引器与重载操作符(11.1-11.2)

    1.索引器方法结构大致为<modifier><return type> this [argument list],它可以在接口中定义: 在为接口声明索引器的时候,记住声明只是表 ...

  8. 描述一下C#中索引器的实现过程,是否只能根据数字进行索引?

    不是.可以用任意类型. 索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了 ...

  9. C# 索引器使用总结

    1.索引器(Indexer): 索引器允许类或者结构的实例按照与数组相同的方式进行索引.索引器类似于属性,不同之处在于他们的访问采用参数. 最简单的索引器的使用 /// <summary> ...

  10. C# 类中索引器的使用二

    索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便.直观的被引用.索引器非常类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用.定义 ...

随机推荐

  1. JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果

    JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果 今天是2014年第一篇博客是关于类似于我们的qq空间长图片展示效果,因为一张很长的图片不可能全部把他展示出来,所以外层用了一个容器给他一个高度,超 ...

  2. 浅拷贝与深拷贝的实现方式、区别;deepcopy如果你来设计,如何实现(一)

    浅拷贝与深拷贝的实现方式.区别:deepcopy如果你来设计,如何实现: copy浅拷贝:没有拷贝子对象,所以原始数据改变,子对象改变 deepcopy深拷贝:包含对象里面的子对象的拷贝,所以原始对象 ...

  3. Java安全通信:HTTPS与SSL

    转载地址:http://www.cnblogs.com/devinzhang/archive/2012/02/28/2371631.html Java安全通信:HTTPS与SSL 1. HTTPS概念 ...

  4. ubuntu14.04上设置默认python命令是执行python3而不是Python2

    update-alternatives --install /usr/bin/python python /usr/bin/python2 100 update-alternatives --inst ...

  5. Python3入门(四)——Python函数

    一.概述 python和其他高级语言一样,支持函数 注意和scala不一样,结果必须使用return,否则默认return None!这和scala最后一个值作为返回是不一样的! 二.函数调用 和其他 ...

  6. 基于TLS证书手动部署kubernetes集群(上)

    一.简介 Kubernetes是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernetes也叫K8S. K8S是Google内部一个叫Borg的容器集群管理系统衍生 ...

  7. 20155210 Exp7 网络欺诈防范

    Exp7 网络欺诈防范 SET工具建立冒名网站 首先利用lsof -i:80或者netstat -tupln |grep 80查询80端口的使用情况(我的电脑80端口没有被占用,如果被占用,则用kil ...

  8. 20155217《网络对抗》Exp06 信息搜集与漏洞扫描

    20155217<网络对抗>Exp06 信息搜集与漏洞扫描 实践内容 各种搜索技巧的应用 DNS IP注册信息的查询 基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的查点 ...

  9. 2017-2018-2 20155315《网络对抗技术》Exp7 :网络欺诈防范

    实验目的 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 实验内容 简单应用SET工具建立冒名网站 ettercap DNS spoof 结合应用两种技术,用DNS sp ...

  10. 类调用自己的静态方法必须把该方法设置为public

    否则调用不了 ParaChecker.isOK(bindingResult); public class ParaChecker { static BaseResult paraCheck(Bindi ...