本代码创建了一个类的索引器用来访问类,就像访问数组一样
using System.Collections.Generic;
...
class Client
{
private Dictionary<int, string> invoices
= new Dictionary<int, string>();
public void Add (int id, string value)
{ invoices.Add (id, value); }
public string this [int id] // indexer
{
get { return invoices[id]; }
set { invoices[id] = value; }
}
}
public class Test
{
public static void Main( )
{
Client client = new Client();
client.Add (1, "I005238A");
Console.WriteLine ("Invoice: " + client[1]); // indexer access
}
}
