现在的位置: 首页 > 综合 > 正文

索引指示器

2012年09月30日 ⁄ 综合 ⁄ 共 3366字 ⁄ 字号 评论关闭

本节课将介绍C#的索引指示器,其目的包括:
1.了解什么是索引指示器

2.如何实现索引指示器

3.重载索引指示器

4.了解如何实现多参数的索引指示器

索引指示器并不难使用。它们的用法跟数组相同。在一个类内部,你可以按照你的意愿来管理一组数据的集合。这些对象可以是类成员的有限集合,也可以是另外一个数组,或者是一些复杂的数据结构。不考虑类的内部实现,其数据可以通过使用索引指示器来获得。如下是一个例子:

 

1.清单 11-1. 索引指示器的例子:IntIndexer.cs

 1using System;
 2/// 
 3/// A simple indexer example.
 4/// 

 5class IntIndexer
 6{
 7    private string[] myData;
 8
 9    public IntIndexer(int size)
10    {
11        myData = new string[size];
12        for (int i = 0; i < size; i++)
13        {
14            myData[i] = "empty";
15        }

16    }

17    public string this[int pos]
18    {
19        get
20        {
21            return myData[pos];
22        }

23        set
24        {
25            myData[pos] = value;
26        }

27    }

28
29    static void Main(string[] args)
30    {
31        int size = 10;
32        IntIndexer myInd = new IntIndexer(size);
33        myInd[9] = "Some Value";
34        myInd[3] = "Another Value";
35        myInd[5] = "Any Value";
36        Console.WriteLine(" Indexer Output ");
37        for (int i = 0; i < size; i++)
38        {
39            Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
40        }

41    }

42}

说明

1.清单 11-1演示了如何实现一个索引指示器, IntIndexer类有个名为myData的字符串数组,该数组是私有成员,因而其外部成员是看不见的。该数组是在构造函数中进行初始化的,该构造函数带有一个整型size参数,用来初始化myData数组,初始化时 把单词"empty"作为每个数组元素的值。

2.IntIndexer类的下一成员是索引指示器(Indexer),由关键字this和方括号[int pos]标识出来。该成员带有一个位置参数pos。正如你已经猜测到,Indexer的实现同属性一样。Indexer有get 和set访问操作,就同属性中的用法一样。索引指示器(indexer)返回一个字符串,在定义索引指示器时,string这个类型名标志着其返回类型为字符串类型。

3.Main()方法完成如下事情:初始化一个新的IntIndexer对象,添加一些值,并且打印出结果。其输出结果如下:

 

Indexer Output

myInd[0]: empty
myInd[1]: empty
myInd[2]: empty
myInd[3]: Another Value
myInd[4]: empty
myInd[5]: Any Value
myInd[6]: empty
myInd[7]: empty
myInd[8]: empty
myInd[9]: Some Value

4.在不少程序语言中,通常都是使用整数作为下标来访问作为数组元素的,但C#的索引指示器不仅能够做到这一点,而且还能够更进一步。 定义索引指示器时,可以带有多个参数,每个参数的类型可以不同。添加的参数由逗号隔开,同方法中的的参数表一样。索引指示器的合法的参数类型包括:整型,枚举类型和字符串。另外,索引指示器也可以被重载。在清单 11-2中,我们修改了前面的程序,以便用来重载索引指示器 ,从而可以接受不同类型的参数。

 

2.清单 11-2. 重载的索引指示器: OvrIndexer.cs

1using System;
 2/// 
 3/// Implements overloaded indexers.
 4/// 

 5class OvrIndexer
 6{
 7    private string[] myData;
 8    private int arrSize;
 9    public OvrIndexer(int size)
10    {
11        arrSize = size;
12        myData = new string[size];
13        for (int i = 0; i < size; i++)
14        {
15            myData[i] = "empty";
16        }

17    }

18
19    public string this[int pos]
20    {
21        get
22        {
23            return myData[pos];
24        }

25        set
26        {
27            myData[pos] = value;
28        }

29    }

30
31    public string this[string data]
32    {
33        get
34        {
35            int count = 0;
36            for (int i = 0; i < arrSize; i++)
37            {
38                if (myData[i] == data)
39                {
40                    count++;
41                }

42            }

43            return count.ToString();
44        }

45        set
46        {
47            for (int i = 0; i < arrSize; i++)
48            {
49                if (myData[i] == data)
50                {
51                    myData[i] = value;
52                }

53            }

54        }

55    }

56
57    static void Main(string[] args)
58    {
59        int size = 10;
60        OvrIndexer myInd = new OvrIndexer(size);
61        myInd[9] = "Some Value";
62        myInd[3] = "Another Value";
63        myInd[5] = "Any Value";
64        myInd["empty"] = "no value";
65        Console.WriteLine(" Indexer Output ");
66        for (int i = 0; i < size; i++)
67        {
68            Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);<

抱歉!评论已关闭.