C#类中索引器的使用
索引器允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数。被称为有参属性。
简单的索引器实例:
class Program
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 IndexClass a = new IndexClass(); 6 a[0] = "张三"; 7 a[1] = "李四"; 8 a[2] = "王五"; 9 Console.WriteLine("a[0]=" + a[0]); 10 Console.WriteLine("a[1]=" + a[1]); 11 Console.WriteLine("a[2]=" + a[2]); 12 Console.ReadKey(); 13 } 14 }
class IndexClass
1 class IndexClass 2 { 3 private string[] name = new string[10]; 4 public string this[int index] 5 { 6 get { return name[index]; } 7 set { this.name[index] = value; } 8 } 9 }
索引器与数组的比较:
索引器的索引值不受类型限制。用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值。
索引器允许重载,一个类可以有多个索引器。
索引器不是一个变量没有直接对应的数据存储地方。索引器有get和set访问器。
———————————————————————————————————————————————————————
索引器允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数。被称为有参属性。
简单的索引器实例:
索引器与属性的比较:
标示方式:属性以名称来标识,索引器以函数签名来标识。
索引器可以被重载。属性则不可以被重载。
属性可以为静态的,索引器属于实例成员,不能被声明为static
多参数索引器实例:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Collections; 5 namespace Study 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 ScoreIndex s = new ScoreIndex(); 12 s["张三", 1] = 90; 13 s["张三", 2] = 100; 14 s["张三", 3] = 80; 15 s["李四", 1] = 60; 16 s["李四", 2] = 70; 17 s["李四", 3] = 50; 18 Console.WriteLine("张三课程编号为1的成绩为:" + s["张三",1]); 19 Console.WriteLine("张三的所有成绩为:"); 20 ArrayList temp; 21 temp = s["张三"]; 22 foreach (IndexClass b in temp) 23 { 24 Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score); 25 } 26 Console.ReadKey(); 27 } 28 } 29 class IndexClass 30 { 31 private string _name; 32 private int _courseid; 33 private int _score; 34 public IndexClass(string _name, int _courseid, int _score) 35 { 36 this._name = _name; 37 this._courseid = _courseid; 38 this._score = _score; 39 } 40 public string Name 41 { 42 get { return _name; } 43 set { this._name = value; } 44 } 45 public int CourseID 46 { 47 get { return _courseid; } 48 set { this._courseid = value; } 49 } 50 public int Score 51 { 52 get { return _score; } 53 set { this._score = value; } 54 } 55 } 56 class ScoreIndex 57 { 58 private ArrayList arr; 59 public ScoreIndex() 60 { 61 arr = new ArrayList(); 62 } 63 public int this[string _name, int _courseid] 64 { 65 get 66 { 67 foreach (IndexClass a in arr) 68 { 69 if (a.Name == _name && a.CourseID == _courseid) 70 { 71 return a.Score; 72 } 73 } 74 return -1; 75 } 76 set 77 { 78 arr.Add(new IndexClass(_name, _courseid, value)); //arr["张三",1]=90 79 } 80 } 81 //重载索引器 82 public ArrayList this[string _name] 83 { 84 get 85 { 86 ArrayList temp = new ArrayList(); 87 foreach (IndexClass b in arr) 88 { 89 if (b.Name == _name) 90 { 91 temp.Add(b); 92 } 93 } 94 return temp; 95 } 96 } 97 } 98 }
备注:
所有索引器都使用this关键词来取代方法名。Class或Struct只允许定义一个索引器,而且总是命名为this。
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。
get 访问器返回值。set 访问器分配值。
this 关键字用于定义索引器。
value 关键字用于定义由 set 索引器分配的值。
索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
索引器可被重载。
索引器可以有多个形参,例如当访问二维数组时。
索引器可以使用百数值下标,而数组只能使用整数下标:如下列定义一个String下标的索引器
public int this [string name] {...}
属性和索引器
属性和索引器之间有好些差别:
类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。
属性可以是static(静态的)而索引器则必须是实例成员。


浙公网安备 33010602011771号