18._6索引器在接口中的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _18._6索引器在接口中的使用 { public interface ItextIndex { int this[int index] { get; set; } } class itextIndex : ItextIndex { private int[] arr = new int[10]; public int this[int index] { get { if (index < 0 || index >= 10) return 0; else return arr[index]; } set { if (index >= 0 && index < 10) arr[index] = value; } } } class Program { static void Main(string[] args) { itextIndex arr = new itextIndex(); arr[-1] = 2; arr[4] = 30; arr[9] = 34; arr[14] = 23; for(int i = -1; i < 15; i = i + 5) { Console.WriteLine("arr[{0}]:{1}", i,arr[i]); } Console.Read(); } } }