索引器
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 var myindex = new MyIndex(); 14 myindex[0] = "0"; 15 myindex[1] = "1"; 16 myindex[2] = "2"; 17 myindex[3] = "3"; 18 myindex[4] = "4"; 19 myindex[5] = "5"; 20 myindex[6] = "6"; 21 myindex[7] = "7"; 22 myindex[8] = "8"; 23 myindex[9] = "9"; 24 for (int i = 0; i < 10; i++) 25 { 26 Console.WriteLine(myindex[i]); 27 } 28 Console.ReadLine(); 29 } 30 } 31 class MyIndex 32 { 33 private string[] MyString = new string[10]; 34 public MyIndex() 35 { 36 for (int i = 0; i < MyString.Length; i++) 37 { 38 MyString[i] = "N/A"; 39 } 40 } 41 public string this[int index] 42 { 43 get 44 { 45 string tmp; 46 if (index >= 0 && index < MyString.Length) 47 tmp = MyString[index]; 48 else 49 tmp = ""; 50 return tmp; 51 } 52 set 53 { 54 if (index >= 0 && index < MyString.Length) 55 { 56 MyString[index] = value; 57 } 58 } 59 } 60 } 61 }