using System; using System.IO; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var names = new IndexedNames(); names[0] = "hello"; names[1] = "hong"; names[2] = "qian"; names[3] = "jin"; names[4] = "name"; names[5] = "hao"; names[6] = "good"; for (int i = 0; i <= 6; i++) { Console.WriteLine(names[i]); } Console.WriteLine(names["jin"]); Console.WriteLine(names["aaaa"]); Console.ReadKey(); } } class IndexedNames { private string [] nameList=new string [10]; public IndexedNames() { for (int i = 0; i < nameList.Length; i++) { nameList[i] = "N/A"; } } public string this[int index] { get { string tmp; if (index > 0 && index <= nameList.Length - 1) { tmp = nameList[index]; } else { tmp = ""; } return tmp; } set { if (index > 0 && index <= nameList.Length - 1) { nameList[index] = value; } } } public int this[string name] { get { int index = 0; while (index < nameList.Length) { if (nameList[index] == name) { return index; } index++; } return -1; } } } public interface ISomeInterface { int this[int index] { get; set; } } class IndexerClass : ISomeInterface { private int[] arr=new int [100]; public int this[int index] { get { return arr[index]; } private set { arr[index] = value; } } } }
时间: 2024-11-09 14:55:17