C#编程-98:索引器在接口中的使用

69 阅读1分钟
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5.  

  6. namespace InterfaceTest

  7. {

  8.     public interface IIndexTest

  9.     {

  10.         int this[int index]

  11.         {

  12.             set;

  13.             get;

  14.         }

  15.     }

  16.     class Indextest : IIndexTest

  17.     { 

  18.     private int[] myint=new int[10];

  19.     public int this[int index]

  20.     {

  21.         set

  22.         {

  23.             if (index >= 0 && index < 10) 

  24.                 myint[index] = value;

  25.         }

  26.         get

  27.         {

  28.             if (index < 0 || index >= 10)

  29.                 return 0;

  30.             else

  31.                 return myint[index];

  32.         }

  33.     }

  34.          

  35.     }

  36.     class Program

  37.     {

  38.         static void Main(string[] args)

  39.         {

  40.             Indextest arr = new Indextest();

  41.             arr[-1] = 5;

  42.             arr[4] = 10;

  43.             arr[9] = 15;

  44.             arr[14] = 20;

  45.  

  46.             for (int i = -1; i < 15; i = i + 5)

  47.             {

  48.                 Console.WriteLine("arr[{0}]={1}",i,arr[i]);

  49.             }

  50.             Console.ReadKey();

  51.         }

  52.     }

  53. }

C#编程-98:索引器在接口中的使用

C#编程-98:索引器在接口中的使用

\