索引器许可类或组织的实例就像数组一样举行索引。索引器相似于属性,差别之处在于它们的取值函数采纳参数。索引器许可类或许组织的实例依据与数组雷同的体式格局举行索引取值,索引器与属性相似,差别的是索引器的接见是带参的。
索引器概述
运用索引器能够用相似于数组的体式格局为对象竖立索引。
get 取值函数返回值。 set 取值函数分派值。
this 症结字用于定义索引器。
value 症结字用于定义由 set 索引器分派的值。
索引器没必要依据整数值举行索引;由你决议怎样定义特定的查找机制。
索引器可被重载。
索引器能够有多个形参,比方当接见二维数组时。
运用索引器能够用相似于数组的体式格局为对象竖立索引。
get 取值函数返回值。 set 取值函数分派值。
this 症结字用于定义索引器。
value 症结字用于定义由 set 索引器分派的值。
索引器没必要依据整数值举行索引;由你决议怎样定义特定的查找机制。
索引器可被重载。
索引器能够有多个形参,比方当接见二维数组时。
索引器和数组比较:
(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作为索引器下标的时刻,要用Hashtable private 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存取Values public string this[int index] { get { return name[index].ToString(); } set { name.Add(index, value); } } //2:经由过程Values存取key public int this[string aName] { get { //Hashtable中现实寄存的是DictionaryEntry(字典)范例,假如要遍历一个Hashtable,就需要运用到DictionaryEntry foreach(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); } } }
以上就是关于C#中索引器的细致引见的细致内容,更多请关注ki4网别的相干文章!