c#编程指南(六) 类索引器(Class Indexer)
c#编程指南(六) 类索引器(Class Indexer)
------------------------------------------------------------------------------------------
看起来很像C++的重载“[ ]”操作符
------------------------------------------------------------------------------------------
类索引器,可以使得你使用数组一样的方式来访问类的数据。
这种访问多见于数组,列表,词典,哈希表的快捷访问。
实际上写法很简单,写成:public T1 this[T2 i]
代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Drawing;
6
7 namespace Indexer
8 {
9 public class Test
10 {
11 private List<string> _lstTest = new List<string>();
12
13 public List<string> Items
14 {
15 get { return _lstTest; }
16 set { _lstTest = value; }
17 }
18
19 public string this[int i]
20 {
21 get {
22 if ((i >= 0) && (i < _lstTest.Count)) return _lstTest[i];
23 else throw new IndexOutOfRangeException("the error index is " + i);
24 }
25
26 set {
27 if ((i >= 0) && (i < _lstTest.Count)) _lstTest[i] = value;
28 else throw new IndexOutOfRangeException("the error index is " + i);
29 }
30 }
31
32 public string this[string s] { get { return "Test Return " + s; } }
33
34
35 public string this[Color c] { get { return c.ToString(); } }36 }37 38 39 class Program40 {41 static void Main(string[] args)42 {43 Test test = new Test();44 45 test.Items.Add("test1");46 test.Items.Add("test2");47 test.Items.Add("test3");48 for (int i = 0; i < test.Items.Count; i++)49 {50 Console.WriteLine(test[i]);51 }52 53 Console.WriteLine("----------------------------------------------------------");54 test[0] = "test4";55 for (int i = 0; i < test.Items.Count; i++)56 {57 Console.WriteLine(test[i]);58 }59 60 Console.WriteLine("----------------------------------------------------------");61 Console.WriteLine(test["香山飘雪"]);62 63 64 Console.WriteLine("----------------------------------------------------------");65 Console.WriteLine(test[Color.BlueViolet]);66 }67 }68 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Drawing;
6
7 namespace Indexer
8 {
9 public class Test
10 {
11 private List<string> _lstTest = new List<string>();
12
13 public List<string> Items
14 {
15 get { return _lstTest; }
16 set { _lstTest = value; }
17 }
18
19 public string this[int i]
20 {
21 get {
22 if ((i >= 0) && (i < _lstTest.Count)) return _lstTest[i];
23 else throw new IndexOutOfRangeException("the error index is " + i);
24 }
25
26 set {
27 if ((i >= 0) && (i < _lstTest.Count)) _lstTest[i] = value;
28 else throw new IndexOutOfRangeException("the error index is " + i);
29 }
30 }
31
32 public string this[string s] { get { return "Test Return " + s; } }
33
34
35 public string this[Color c] { get { return c.ToString(); } }36 }37 38 39 class Program40 {41 static void Main(string[] args)42 {43 Test test = new Test();44 45 test.Items.Add("test1");46 test.Items.Add("test2");47 test.Items.Add("test3");48 for (int i = 0; i < test.Items.Count; i++)49 {50 Console.WriteLine(test[i]);51 }52 53 Console.WriteLine("----------------------------------------------------------");54 test[0] = "test4";55 for (int i = 0; i < test.Items.Count; i++)56 {57 Console.WriteLine(test[i]);58 }59 60 Console.WriteLine("----------------------------------------------------------");61 Console.WriteLine(test["香山飘雪"]);62 63 64 Console.WriteLine("----------------------------------------------------------");65 Console.WriteLine(test[Color.BlueViolet]);66 }67 }68 }
很简单吧,
第一个,我定义了一个可读可写的以int为参数的索引器。
第二个,我定义了一个可读的以string为参数的索引器。
第三个,比较搞怪了,我定义了一个color参数的索引器。
呵呵,是很简单吧!!
SkyDriver代码下载:下载