标签:
using System; using System.Collections; using System.Collections.Generic; using System.IO; namespace codeTest { class Program { static void Main(string[] args) { IndexTest indexname = new IndexTest(); indexname[0] = "0"; indexname[1] = "1"; indexname[2] = "2"; indexname[3] = "3"; indexname[4] = "4"; indexname[5] = "5"; indexname[6] = "6"; indexname[7] = "7"; indexname[8] = "8"; indexname[9] = "9"; for (int i = 0; i < 10; i++) { Console.WriteLine(indexname[i]); } Console.WriteLine(indexname["1"]); Console.WriteLine(indexname["2"]); Console.WriteLine(indexname["6"]); IndexTest1 indextest1 = new IndexTest1(); Console.WriteLine(indextest1[1]); Console.WriteLine(indextest1[2]); Console.WriteLine(indextest1[3]); Console.ReadLine(); } public class IndexTest { string[] indexName; public IndexTest() { indexName = new string[10] { "", "", "", "", "", "", "", "", "", "" }; } public string this[int index] { get { string tmp; if (index >= 0 && index <= 10) { tmp = indexName[index]; } else { tmp = ""; } return tmp; } set { if (index >= 0 && index <= 10) { indexName[index] = value; } } } public int this[string name] { get { int index = 0; while (index < indexName.Length) { if (indexName[index] == name) { return index; } index++; } return -1; } } } public interface IIndexTest { int this[int index] { get; set; } } public class IndexTest1 : IIndexTest { int[] array = new int[5] { 1, 2, 3, 4, 5 }; public int this[int index] { get { return array[index]; } set { array[index] = value; } } } } }
标签:
原文地址:http://www.cnblogs.com/lgxlsm/p/4765229.html