索引器

  所谓索引器就是一类特殊的属性,通过它们就可以像引用数组一样引用自己的类。显然,这一功能在创建集合类的场合特别有用,而在其它某些情况下,比如处理大型文件或者抽象某些有限资源等,能让类具有类似数组的行为当然也是非常有用的。

首先来看类数组:

View Code
 1     public class ArrClass
2 {
3 private readonly string UserName;
4 private readonly int UserNum;
5 public ArrClass(string UserName, int UserNum)
6 {
7 this.UserName = UserName;
8 this.UserNum = UserNum;
9 }
10 public string _UserName
11 {
12 get { return UserName; }
13 }
14 public int _UserNum
15 {
16 get { return UserNum; }
17 }
18 }
19
20 class Program
21 {
22 static void Main(string[] args)
23 {
24 ArrClass[] User = new ArrClass[3];
25 User[0] = new ArrClass("张三",001);
26 User[1] = new ArrClass("李四",002);
27 User[2] = new ArrClass("王五",003);
28 Console.Write("User[0] =" + User[0]._UserName);
29 Console.Write("User[1] =" + User[1]._UserName);
30 Console.Write("User[2] =" + User[2]._UserName);
31 Console.ReadLine();
32
33 }
34 }

索引器与数组比较:
1.索引器的索引值不受限为整数。
   用来访问数组的索引其类型一定为整型,然后索引器可以定义其他类型的索引值。
2.索引器允许重载
  一个类并不限制只能定义一个索引器,只要索引器的函数签名不同,一个类就可以拥有多个索引器,你可以重载他的功能。
3. 索引器不是一个变量
  索引器和数组不同在于索引器并不直接对应应用数据存储的地方,而数组则有。
  索引器有Get和Set访问器,用来指明要读取和写入索引器元素时需要执行的代码。

索引器 的定义:
访问修饰符 返回类型 this[参数类型 参数...]

       get{语句集合}
       set{语句集合}

索引器的使用

View Code
 1     public class Indexer
2 {
3 string[] User = new string[5];
4 public string this[int num]
5 {
6 get { return User[num]; }
7 set
8 {
9 if (num <= User.Length)
10 {
11 User[num] = value;
12 }
13 }
14 }
15 }
16
17 class Program
18 {
19 static void Main(string[] args)
20 {
21 Indexer User = new Indexer();
22 User[0] = "张三";
23 User[1] = "李四";
24 User[2] = "王五";
25 Console.Write("User[0] =" + User[0]);
26 Console.Write("User[1] =" + User[1]);
27 Console.Write("User[2] =" + User[2]);
28 Console.ReadLine();
29 }
30 }

多参数索引的使用(利用姓名、课程ID查找分数):

View Code
 1     public class CourseScore
2 {
3 private string userName;
4 private int courseID;
5 private int score;
6 public CourseScore(string userName,int courseID,int score)
7 {
8 this.userName = userName;
9 this.courseID = courseID;
10 this.score = score;
11 }
12 public string UserName
13 {
14 get { return userName; }
15 set { userName = value; }
16 }
17 public int CourseID
18 {
19 get { return courseID; }
20 set { courseID = value; }
21 }
22 public int Score
23 {
24 get { return score; }
25 set { score = value; }
26 }
27 }
28 //给CourseScore类申明一个索引器
29 public class CourseScoreIndex
30 {
31 ArrayList arr;
32 public CourseScoreIndex()
33 {
34 arr = new ArrayList();
35 }
36 public int this[string userName, int courseID]
37 {
38 get
39 {
40 foreach (CourseScore c in arr)
41 {
42 if (c.UserName == userName && c.CourseID == courseID)
43 {
44 return c.Score;
45 }
46 }
47 return -1;
48 }
49 set
50 {
51 arr.Add(new CourseScore(userName,courseID,value));
52 }
53 }
54 }
55 class Program
56 {
57 static void Main(string[] args)
58 {
59 CourseScoreIndex cs = new CourseScoreIndex();
60 cs["张三", 001] = 100;
61 cs["张三", 002] = 50;
62 cs["李四", 001] = 90;
63 Console.WriteLine(cs["张三", 002]);
64 Console.ReadLine();
65 }
66 }

索引器与属性比较:
1.标识方式:属性用名称来标识,而索引器用函数签名来标识;

2.索引器可以被重载:因为属性你名称标识,所以不能被重载,索引器以函数签名标识,故可以被重载;
3.索引器不可以被申明为static:属性可以为static,而索引器永远属于实例成员,不能为static。

原文地址:https://www.cnblogs.com/psforever/p/2120890.html