1
using
System;
2 using System.Collections.Generic;
3 using System.Collections;
4 using System.Text;
5
6 namespace indexer
7 {
8 class Program
9 {
10 static void Main( string [] args)
11 {
12 // class chongzai-------------------
13 chongzai newindexer = new chongzai(); // 和类数组相比,索引器只须声明一个实例
14 newindexer[ 0 ] = " li " ; // set索引器
15 newindexer[ 1 ] = " lai " ;
16 newindexer[ 2 ] = " chen " ;
17 Console.WriteLine( " newindexer[0]= " + newindexer[ 0 ]); // get索引器
18 Console.WriteLine( " newindexer[1]= " + newindexer[ 1 ]);
19 Console.WriteLine( " newindexer[2]= " + newindexer[ 2 ]);
20 newindexer[ " li " ] = 0 ;
21 newindexer[ " lai " ] = 1 ;
22 newindexer[ " chen " ] = 2 ;
23 Console.WriteLine( " newindexer[li]= " + newindexer[ " li " ]);
24 Console.WriteLine( " newindexer[lai]= " + newindexer[ " lai " ]);
25 Console.WriteLine( " newindexer[chen]= " + newindexer[ " chen " ]);
26
27 // class hashtableA------------------------
28 // hashtableA newhashA = new hashtableA();
29 // newhashA["a001"] = "A1";
30 // newhashA["a002"] = "A2";
31 // newhashA["a003"] = "A3";
32 // Console.WriteLine("newhashA[a001]=" + newhashA["a001"]);
33 // Console.WriteLine("newhashA[a002]=" + newhashA["a002"]);
34 // Console.WriteLine("newhashA[a003]=" + newhashA["a003"]);
35
36 // class indexeR-------------------------
37 // indexeR newindexer = new indexeR(); // 和类数组相比,索引器只须声明一个实例
38 // newindexer[0] = "liao";
39 // newindexer[1] = "lai";
40 // newindexer[2] = "chen";
41 // Console.WriteLine("newindexer[0]=" + newindexer[0]);
42 // Console.WriteLine("newindexer[1]=" + newindexer[1]);
43 // Console.WriteLine("newindexer[2]=" + newindexer[2]);
44
45 // class arrclasS---------------------
46 // arrclasS[] newarR = new arrclasS[3]; // 声明数组类
47 // newarR[0] = new arrclasS("zhang"); // 数组类可以带参数
48 // newarR[1] = new arrclasS("liao");
49 // newarR[2] = new arrclasS("tang");
50 // Console.WriteLine("newarR[0]=" + newarR[0].yournamE);
51 // Console.WriteLine("newarR[1]=" + newarR[1].yournamE);
52 // Console.WriteLine("newarR[2]=" + newarR[2].yournamE);
53
54 // class a-------------------
55 // a[] newa = new a[3];
56 // newa[0] = new a("fd");
57 // newa[1] = new a("nn");
58 }
59 }
60 class chongzai
61 {
62 private Hashtable nameA = new Hashtable(); // Hashtable声明的成员用Add方法为它赋值
63 private string [] strB = new string [ 3 ];
64 internal string this [ int index] // 索引器不可以声明为static
65 {
66 set
67 {
68 strB[index] = value;
69 }
70 get
71 {
72 return strB[index];
73 }
74 }
75 internal int this [ string yournamE] // 同一个类的同名索引器(带this)用不同的参数调用,就是重载
76 {
77 set
78 {
79 nameA.Add(value, yournamE); // 括号里的value,key的位置对应索引器的this和后面带的参数[string yournamE],对应的数据类型也要一样
80 }
81 get
82 {
83 foreach (DictionaryEntry b in nameA) // 用b从nameA字典中遍历全部值 // DictionaryEntry是Hashtable的一个属性
84 {
85 if (b.Value.ToString() == yournamE) // 匹配等于调用值的字典关键字
86 {
87 return Convert.ToInt32(b.Key); // 返回字典关键字
88 }
89 }
90 return - 1 ;
91 }
92 }
93
94 }
95
96 // class hashtableA // 非整数数组成员的索引器
97 // {
98 // private Hashtable nameA = new Hashtable(); // Hashtable是系统参数,它的值为key,value
99 // internal String this[string index]
100 // {
101 // set
102 // {
103 // nameA.Add(index, value); // 括号里的值对应key,value // 等同于nameA[index]=value
104 // }
105 // get
106 // {
107 // return nameA[index].ToString();
108 // }
109 // }
110 // }
111
112 // class indexeR // 索引器R
113 // {
114 // // private string name;
115 // private String[] namE = new String[3];
116 // internal string this[int index] // 用this关键字声明数组类构造器
117 // {
118 // get
119 // {
120 // return namE[index];
121 // }
122 // set
123 // {
124 // namE[index] = value;
125 // }
126 // }
127
128 // }
129
130 // class arrclasS // 属性
131 // {
132 // private readonly String namE;
133 // internal arrclasS(String nameA)
134 // {
135 // this.namE = nameA;
136 // }
137 // internal String yournamE
138 // {
139 // get
140 // {
141 // return namE;
142 // }
143 // }
144 // }
145
146 // class a
147 // {
148 // internal a(String str)
149 // {
150 // Console.WriteLine(str);
151 // }
152 // }
153 }
154
155
156
2 using System.Collections.Generic;
3 using System.Collections;
4 using System.Text;
5
6 namespace indexer
7 {
8 class Program
9 {
10 static void Main( string [] args)
11 {
12 // class chongzai-------------------
13 chongzai newindexer = new chongzai(); // 和类数组相比,索引器只须声明一个实例
14 newindexer[ 0 ] = " li " ; // set索引器
15 newindexer[ 1 ] = " lai " ;
16 newindexer[ 2 ] = " chen " ;
17 Console.WriteLine( " newindexer[0]= " + newindexer[ 0 ]); // get索引器
18 Console.WriteLine( " newindexer[1]= " + newindexer[ 1 ]);
19 Console.WriteLine( " newindexer[2]= " + newindexer[ 2 ]);
20 newindexer[ " li " ] = 0 ;
21 newindexer[ " lai " ] = 1 ;
22 newindexer[ " chen " ] = 2 ;
23 Console.WriteLine( " newindexer[li]= " + newindexer[ " li " ]);
24 Console.WriteLine( " newindexer[lai]= " + newindexer[ " lai " ]);
25 Console.WriteLine( " newindexer[chen]= " + newindexer[ " chen " ]);
26
27 // class hashtableA------------------------
28 // hashtableA newhashA = new hashtableA();
29 // newhashA["a001"] = "A1";
30 // newhashA["a002"] = "A2";
31 // newhashA["a003"] = "A3";
32 // Console.WriteLine("newhashA[a001]=" + newhashA["a001"]);
33 // Console.WriteLine("newhashA[a002]=" + newhashA["a002"]);
34 // Console.WriteLine("newhashA[a003]=" + newhashA["a003"]);
35
36 // class indexeR-------------------------
37 // indexeR newindexer = new indexeR(); // 和类数组相比,索引器只须声明一个实例
38 // newindexer[0] = "liao";
39 // newindexer[1] = "lai";
40 // newindexer[2] = "chen";
41 // Console.WriteLine("newindexer[0]=" + newindexer[0]);
42 // Console.WriteLine("newindexer[1]=" + newindexer[1]);
43 // Console.WriteLine("newindexer[2]=" + newindexer[2]);
44
45 // class arrclasS---------------------
46 // arrclasS[] newarR = new arrclasS[3]; // 声明数组类
47 // newarR[0] = new arrclasS("zhang"); // 数组类可以带参数
48 // newarR[1] = new arrclasS("liao");
49 // newarR[2] = new arrclasS("tang");
50 // Console.WriteLine("newarR[0]=" + newarR[0].yournamE);
51 // Console.WriteLine("newarR[1]=" + newarR[1].yournamE);
52 // Console.WriteLine("newarR[2]=" + newarR[2].yournamE);
53
54 // class a-------------------
55 // a[] newa = new a[3];
56 // newa[0] = new a("fd");
57 // newa[1] = new a("nn");
58 }
59 }
60 class chongzai
61 {
62 private Hashtable nameA = new Hashtable(); // Hashtable声明的成员用Add方法为它赋值
63 private string [] strB = new string [ 3 ];
64 internal string this [ int index] // 索引器不可以声明为static
65 {
66 set
67 {
68 strB[index] = value;
69 }
70 get
71 {
72 return strB[index];
73 }
74 }
75 internal int this [ string yournamE] // 同一个类的同名索引器(带this)用不同的参数调用,就是重载
76 {
77 set
78 {
79 nameA.Add(value, yournamE); // 括号里的value,key的位置对应索引器的this和后面带的参数[string yournamE],对应的数据类型也要一样
80 }
81 get
82 {
83 foreach (DictionaryEntry b in nameA) // 用b从nameA字典中遍历全部值 // DictionaryEntry是Hashtable的一个属性
84 {
85 if (b.Value.ToString() == yournamE) // 匹配等于调用值的字典关键字
86 {
87 return Convert.ToInt32(b.Key); // 返回字典关键字
88 }
89 }
90 return - 1 ;
91 }
92 }
93
94 }
95
96 // class hashtableA // 非整数数组成员的索引器
97 // {
98 // private Hashtable nameA = new Hashtable(); // Hashtable是系统参数,它的值为key,value
99 // internal String this[string index]
100 // {
101 // set
102 // {
103 // nameA.Add(index, value); // 括号里的值对应key,value // 等同于nameA[index]=value
104 // }
105 // get
106 // {
107 // return nameA[index].ToString();
108 // }
109 // }
110 // }
111
112 // class indexeR // 索引器R
113 // {
114 // // private string name;
115 // private String[] namE = new String[3];
116 // internal string this[int index] // 用this关键字声明数组类构造器
117 // {
118 // get
119 // {
120 // return namE[index];
121 // }
122 // set
123 // {
124 // namE[index] = value;
125 // }
126 // }
127
128 // }
129
130 // class arrclasS // 属性
131 // {
132 // private readonly String namE;
133 // internal arrclasS(String nameA)
134 // {
135 // this.namE = nameA;
136 // }
137 // internal String yournamE
138 // {
139 // get
140 // {
141 // return namE;
142 // }
143 // }
144 // }
145
146 // class a
147 // {
148 // internal a(String str)
149 // {
150 // Console.WriteLine(str);
151 // }
152 // }
153 }
154
155
156