要声明类或结构上的索引器,请使用this关键字,例如:
public int this[int index] //声明索引器
{
// get and set 访问
}
foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用。
1
using
System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 /*
8 如何才能让这个类支持foreach呢?其实很简单,只要让我们的类继承自IEnumerable接口,并对该接口中的GetEnumerator()方法加以实现就可以了.
9 */
10
11 namespace IedexSample
12 {
13 class Program
14 {
15 static void Main( string [] args)
16 {
17 /* int NUM = 50;
18 // IndexSample<string> indexStrs = new IndexSample<string>();
19 IndexSample<string> indexStrs = new IndexSample<string>(NUM);
20 try
21 {
22 for (int i = 0; i < 10; i++)
23 {
24 indexStrs[i] = "Sample" + i.ToString();
25 }
26 foreach (string str in indexStrs)
27 {
28 Console.WriteLine(str);
29 }
30 }
31 catch (IndexOutOfRangeException ex)
32 {
33 Console.WriteLine(ex.ToString());
34 } */ //上面的为IndexSample的索引用INT
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 /*
8 如何才能让这个类支持foreach呢?其实很简单,只要让我们的类继承自IEnumerable接口,并对该接口中的GetEnumerator()方法加以实现就可以了.
9 */
10
11 namespace IedexSample
12 {
13 class Program
14 {
15 static void Main( string [] args)
16 {
17 /* int NUM = 50;
18 // IndexSample<string> indexStrs = new IndexSample<string>();
19 IndexSample<string> indexStrs = new IndexSample<string>(NUM);
20 try
21 {
22 for (int i = 0; i < 10; i++)
23 {
24 indexStrs[i] = "Sample" + i.ToString();
25 }
26 foreach (string str in indexStrs)
27 {
28 Console.WriteLine(str);
29 }
30 }
31 catch (IndexOutOfRangeException ex)
32 {
33 Console.WriteLine(ex.ToString());
34 } */ //上面的为IndexSample的索引用INT
//下面的用string来索引
35
36 IndexerSample < string > indexStrs = new IndexerSample < string > (); // T string
37 for ( int i = 0 ;i < 10 ;i ++ )
38 {
39 indexStrs[i.ToString()] = " sample " + i.ToString();
40 }
41 foreach ( string str in indexStrs)
42 { // 遍历所有value;
43 Console.WriteLine(str);
44 }
45 Console.ReadLine();
46 }
47 }
48 public class IndexSample < T > : IEnumerable
49 {
50 T[] array = null ;
51 public IndexSample( int number)
52 {
53 array = new T[number];
54 }
55 public IndexSample( )
56 {
57 array = new T[ 100 ];
58 }
59 public T this [ int i]
60 {
61 get
62 {
63 return array[i];
64 }
65 set
66 {
67 array[i] = value;
68 }
69 }
70 public IEnumerator GetEnumerator()
71 {
72 for ( int i = 0 ; i < array.Length; i ++ )
73 {
74 yield return this [i];
75 }
76 }
77 }
78
79 public class IndexerSample < T > : IEnumerable < T >
80 {
81 Dictionary < string ,T > dic = null ;
82 public IndexerSample()
83 {
84 this .dic = new Dictionary < string , T > ();
85 }
86 public T this [ string indexStr]
87 {
88 get
89 {
90 if (dic.ContainsKey(indexStr))
91 {
92 return dic[indexStr];
93 }
94 else
95 {
96 return default (T);
97 }
98 }
99 set
100 {
101 dic[indexStr] = value;
102 }
103 }
104 public int Count
105 {
106 get
107 {
108 return dic.Count;
109 }
110 }
111 IEnumerator IEnumerable.GetEnumerator()
112 {
113 return dic.Values.GetEnumerator();
114 }
115 IEnumerator < T > System.Collections.Generic.IEnumerable < T > .GetEnumerator()
116 {
117 return dic.Values.GetEnumerator();
118 }
119 }
120 }
121
35
36 IndexerSample < string > indexStrs = new IndexerSample < string > (); // T string
37 for ( int i = 0 ;i < 10 ;i ++ )
38 {
39 indexStrs[i.ToString()] = " sample " + i.ToString();
40 }
41 foreach ( string str in indexStrs)
42 { // 遍历所有value;
43 Console.WriteLine(str);
44 }
45 Console.ReadLine();
46 }
47 }
48 public class IndexSample < T > : IEnumerable
49 {
50 T[] array = null ;
51 public IndexSample( int number)
52 {
53 array = new T[number];
54 }
55 public IndexSample( )
56 {
57 array = new T[ 100 ];
58 }
59 public T this [ int i]
60 {
61 get
62 {
63 return array[i];
64 }
65 set
66 {
67 array[i] = value;
68 }
69 }
70 public IEnumerator GetEnumerator()
71 {
72 for ( int i = 0 ; i < array.Length; i ++ )
73 {
74 yield return this [i];
75 }
76 }
77 }
78
79 public class IndexerSample < T > : IEnumerable < T >
80 {
81 Dictionary < string ,T > dic = null ;
82 public IndexerSample()
83 {
84 this .dic = new Dictionary < string , T > ();
85 }
86 public T this [ string indexStr]
87 {
88 get
89 {
90 if (dic.ContainsKey(indexStr))
91 {
92 return dic[indexStr];
93 }
94 else
95 {
96 return default (T);
97 }
98 }
99 set
100 {
101 dic[indexStr] = value;
102 }
103 }
104 public int Count
105 {
106 get
107 {
108 return dic.Count;
109 }
110 }
111 IEnumerator IEnumerable.GetEnumerator()
112 {
113 return dic.Values.GetEnumerator();
114 }
115 IEnumerator < T > System.Collections.Generic.IEnumerable < T > .GetEnumerator()
116 {
117 return dic.Values.GetEnumerator();
118 }
119 }
120 }
121