索引器相当于带参数的属性,索引器没有名字,而是使用this关键字。索引器的参数不一定是整型,可以是任何类型,并且索引器可以重载。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Collections;
 7 
 8 namespace ConsoleApplication1
 9 {
10 
11     class IndexClass
12     {
13         private Hashtable myHT = new Hashtable();
14 
15         public string this[string index]
16         {
17             get
18             {
19                 string value = null;
20                 if (myHT.Contains(index))
21                 {
22                     value = myHT[index].ToString();
23                 }
24 
25                 return value;
26             }
27 
28             set
29             {
30                 if (myHT.Contains(index))
31                 {
32                     myHT[index] = value;
33                 }
34                 else
35                 {
36                     myHT.Add(index, value);
37                 }
38             }
39         }
40         
41     }
42 
43     class Program
44     {
45         static void Main(string[] args)
46         {
47             IndexClass ic = new IndexClass();
48 
49             ic["A000"] = "00";
50             ic["A001"] = "01";
51             ic["A002"] = "02";
52             ic["A003"] = "03";
53 
54             ic["A002"] = "22";
55 
56             Console.WriteLine("ic['A000']={0}", ic["A000"]);
57             Console.WriteLine("ic['A001']={0}", ic["A001"]);
58             Console.WriteLine("ic['A002']={0}", ic["A002"]);
59             Console.WriteLine("ic['A003']={0}", ic["A003"]);
60 
61             Console.ReadKey();
62         }
63     }
64 }
View Code

重载(overload)就是允许多个同名但是形参个数或者类型不同的函数方法存在于同一个类里。当类统一调用方式时由形参来决定调用具体的方法。

 

Note: 因为声明的是整型数组,每个元素都是值类型,所以在托管堆中为每个元素开辟的内存空间直接存储值类型的值。

Note: 因为声明的是Class数组,每个元素都是一个Class,所以在托管堆中为每个元素开辟的内存空间存储该Class的地址(引用),这个地址再指向为每个Class分配的内存空间。

 

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Collections;
  7 
  8 namespace ConsoleApplication1
  9 {
 10 
 11     class CourseScore
 12     {
 13         private string name;
 14         private string course;
 15         private int score;
 16 
 17         public CourseScore(string name, string course, int score)
 18         {
 19             this.name = name;
 20             this.course = course;
 21             this.score = score;
 22         }
 23 
 24         public string Name
 25         {
 26             get
 27             {
 28                 return name;
 29             }
 30 
 31             set
 32             {
 33                 name = value;
 34             }
 35         }
 36 
 37         public string Course
 38         {
 39             get
 40             {
 41                 return course;
 42             }
 43 
 44             set
 45             {
 46                 course = value;
 47             }
 48         }
 49 
 50         public int Score
 51         {
 52             get
 53             {
 54                 return score;
 55             }
 56 
 57             set
 58             {
 59                 score = value;
 60             }
 61         }
 62         
 63     }
 64 
 65     class CourseScoreIndex
 66     {
 67         private ArrayList courseScore;
 68 
 69         public CourseScoreIndex()
 70         {
 71             this.courseScore = new ArrayList();
 72         }
 73 
 74         public int this[string name, string course]
 75         {
 76             get
 77             {
 78                 foreach(CourseScore c in this.courseScore)
 79                 {
 80                     if(c.Name == name && c.Course == course)
 81                     {
 82                         return c.Score;
 83                     }
 84                 }
 85 
 86                 return -1;
 87             }
 88 
 89             set
 90             {
 91                 this.courseScore.Add(new CourseScore(name, course, value));
 92             }
 93         }
 94 
 95         public ArrayList this[string name]
 96         {
 97             get
 98             {
 99                 ArrayList al = new ArrayList();
100 
101                 foreach (CourseScore c in this.courseScore)
102                 {
103                     if (c.Name == name)
104                     {
105                         al.Add(c);
106                     }
107                 }
108 
109                 return al;
110             }
111         }
112     }
113 
114     class Program
115     {
116         static void Main(string[] args)
117         {
118             CourseScoreIndex cs = new CourseScoreIndex();
119 
120             cs["Jim", "C#"] = 85;
121             cs["Jim", "C++"] = 95;
122             cs["Tom", "An"] = 90;
123 
124             Console.WriteLine(cs["Jim", "C++"]);
125 
126             ArrayList al = cs["Jim"];
127             foreach (CourseScore c in al)
128             {
129                 Console.WriteLine("{0} {1}: {2}", c.Name, c.Course, c.Score);
130             }
131 
132             Console.ReadKey();
133         }
134     }
135 }
View Code

Note:所以可以看做是带参数的属性,可以重载。

 

posted on 2018-05-29 10:51  zzunstu  阅读(170)  评论(0编辑  收藏  举报