7.索引器 重载
public class Demo
{
private Hashtable name = new Hashtable();
public string this[int index]
{
get
{
return name[index].ToString();
}
set
{
name.Add(index, value);
}
}
public int this[string index]
{
set
{
name.Add(value, index);
}
get
{
foreach (DictionaryEntry dd in name)
{
if (dd.Value.ToString() == index)
{
return Convert.ToInt32(dd.Key);
}
}
return -1;
}
}
}
public class Test
{
public static void Main(string[] args)
{
Demo dd = new Demo();
dd[3] = "Lobin";
dd[4] = "Jack";
Console.WriteLine("3号 是 {0}",dd[3]);
Console.WriteLine("4号 是 {0}",dd[4]);
Console.WriteLine("Lobin的编号为: {0}", dd["Lobin"]);
Console.WriteLine("Jack的编号为: {0}",dd["Jack"]);
Console.ReadKey();
dd["Mark"] = 5;
Console.WriteLine("Mark的编号为 {0}", dd["Mark"]);
Console.ReadKey();
}
}