什么是索引器
索引器是一组 get 和 set 访问器,与属性类似:
注意:
索引器的特点:
(1)代码例子:采用字典方式
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu["Math"] = 90;
var mathScore = stu["Math"];
Console.WriteLine(mathScore);
}
}
class Student
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private Dictionary scoreDictionary = new Dictionary();
public int? this[string subject]
{
get
{
if(this.scoreDictionary.ContainsKey(subject))
{
return this.scoreDictionary[subject];
}
else
{
return null;
}
}
set
{
if (value.HasValue == false)
{
throw new Exception("Score cannot be null.");
}
if(this.scoreDictionary.ContainsKey(subject))
{
this.scoreDictionary[subject] = value.Value;//value 为可空类型
}
else
{
this.scoreDictionary.Add(subject, value.Value);
}
}
}
}
(2)代码例子:采用索引值方式
以下所有例子都来源于:# 索引器的多个例子及重载
class Program
{
static void Main(string[] args)
{
//索引器的使用
IndexerClass Indexer = new IndexerClass();
//“=”号右边对索引器赋值,其实就是调用其set方法
Indexer[0] = "张三";
Indexer[1] = "李四";
//输出索引器的值,其实就是调用其get方法
Console.WriteLine(Indexer[0]);
Console.WriteLine(Indexer[1]);
Console.ReadKey();
}
}
public class IndexerClass
{
private string[] name = new string[2];
public string this[int index]
{
get
{
if (index < 2)
{
return name[index];
}
return null;
}
set
{
if (index < 2)
{
name[index] = value;
}
}
}
}
(3)代码例子:采用字符串为下标方式
class Program
{
static void Main(string[] args)
{
IndexerClass Indexer = new IndexerClass();
Indexer["A0001"] = "张三";
Indexer["A0002"] = "李四";
Console.WriteLine(Indexer["A0001"]);
Console.WriteLine(Indexer["A0002"]);
Console.ReadKey();
}
}
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); }
}
}
class Program
{
static void Main(string[] args)
{
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["王五"]);
Console.ReadKey();
}
}
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 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;
}
}
//还可以声明多个版本的索引器...
}
class Program
{
static void Main(string[] args)
{
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);
}
Console.ReadKey();
}
}