?
索引器允許類或者結構的實例按照與數組相同的方式進行索引取值,索引器與屬性類似,不同的是索引器的訪問是帶參的。
索引器和數組比較:
(1)索引器的索引值(Index)類型不受限制
(2)索引器允許重載
(3)索引器不是一個變量
索引器和屬性的不同點
(1)屬性以名稱來標識,索引器以函數形式標識
(2)索引器可以被重載,屬性不可以
(3)索引器不能聲明為static,屬性可以
?
一個簡單的索引器例子
using System; using System.Collections;
public class IndexerClass {private string[] name = new string[2];//索引器必須以this關鍵字定義,其實這個this就是類實例化之后的對象public string this[int index]{//實現索引器的get方法get{if (index < 2){return name[index];}return null;}//實現索引器的set方法set{if (index < 2){name[index] = value;}}} } public class Test {static void Main(){//索引器的使用IndexerClass Indexer = new IndexerClass();//“=”號右邊對索引器賦值,其實就是調用其set方法Indexer[0] = "張三";Indexer[1] = "李四";//輸出索引器的值,其實就是調用其get方法Console.WriteLine(Indexer[0]);Console.WriteLine(Indexer[1]);} }
?以字符串作為下標,對索引器進行存取
public class IndexerClass {//用string作為索引器下標的時候,要用Hashtableprivate Hashtable name = new Hashtable();//索引器必須以this關鍵字定義,其實這個this就是類實例化之后的對象public string this[string index]{get { return name[index].ToString(); set { name.Add(index, value); }} } public class Test {static void Main(){IndexerClass Indexer = new IndexerClass();Indexer["A0001"] = "張三";Indexer["A0002"] = "李四";Console.WriteLine(Indexer["A0001"]);Console.WriteLine(Indexer["A0002"]);} }
?索引器的重載
public class IndexerClass {private Hashtable name = new Hashtable();//1:通過key存取Valuespublic string this[int index]{get { return name[index].ToString(); }set { name.Add(index, value); }}//2:通過Values存取keypublic int this[string aName]{get{//Hashtable中實際存放的是DictionaryEntry(字典)類型,如果要遍歷一個Hashtable,就需要使用到DictionaryEntryforeach(DictionaryEntry d in name){if (d.Value.ToString() == aName){return Convert.ToInt32(d.Key);}}return -1;}set{name.Add(value, aName);}} } public class Test {static void Main(){IndexerClass Indexer = new IndexerClass();//第一種索引器的使用Indexer[1] = "張三";//set訪問器的使用Indexer[2] = "李四";Console.WriteLine("編號為1的名字:" + Indexer[1]);//get訪問器的使用Console.WriteLine("編號為2的名字:" + Indexer[2]);Console.WriteLine();//第二種索引器的使用Console.WriteLine("張三的編號是:" + Indexer["張三"]);//get訪問器的使用Console.WriteLine("李四的編號是:" + Indexer["李四"]);Indexer["王五"] = 3;//set訪問器的使用Console.WriteLine("王五的編號是:" + Indexer["王五"]);} }
?多參索引器
using System; using System.Collections;//入職信息類 public class EntrantInfo {//姓名、編號、部門private string name;private int number;private string department;public EntrantInfo(){}public EntrantInfo(string name, int num, string department){this.name = name;this.number = num;this.department = department;}public string Name{get { return name; }set { name = value; }}public int Num{get { return number; }set { number = value; }}public string Department{get { return department; }set { department = value; }} }//聲明一個類EntrantInfo的索引器 public class IndexerForEntrantInfo {private ArrayList ArrLst;//用于存放EntrantInfo類public IndexerForEntrantInfo(){ArrLst = new ArrayList();}//聲明一個索引器:以名字和編號查找存取部門信息public string this[string name, int num]{get{foreach (EntrantInfo en in ArrLst){if (en.Name == name && en.Num == num){return en.Department;}}return null;}set{//new關鍵字:C#規定,實例化一個類或者調用類的構造函數時,必須使用new關鍵ArrLst.Add(new EntrantInfo(name, num, value));}}//聲明一個索引器:以編號查找名字和部門public ArrayList this[int num]{get{ArrayList temp = new ArrayList();foreach (EntrantInfo en in ArrLst){if (en.Num == num){temp.Add(en);}}return temp;}}//還可以聲明多個版本的索引器... }public class Test {static void Main(){IndexerForEntrantInfo Info = new IndexerForEntrantInfo();//this[string name, int num]的使用Info["張三", 101] = "人事部";Info["李四", 102] = "行政部";Console.WriteLine(Info["張三", 101]);Console.WriteLine(Info["李四", 102]);Console.WriteLine();//this[int num]的使用foreach (EntrantInfo en in Info[102]){Console.WriteLine(en.Name);Console.WriteLine(en.Department);}} }
?
?
原文鏈接:http://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html