class Indexes { public List<string> strList { get; set; } public Indexes(List<string> list) { strList = list; } public string this[int i] { get { return strList[i]; } } public string this[string str] { get { return strList.FirstOrDefault(x => x == str); } } }
应用:
string s = ""; List<string> list = new List<string>(); for (int i = 0; i < 10; i++) { list.Add("这是"+i); } Indexes indexes = new Indexes(list); s += indexes[4]+"\t"; s += indexes["这是5"]; Console.WriteLine(s); Console.ReadKey();
完!