c# 重写索引

时间:2023-08-26 10:55:14
//using System;
//using System.Collections.Generic;
//using System.Text;
//namespace 索引
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//        }
//    }
//}
//using System;
//class MyIndexer
//{ private string [ ]  myArray=new string[4];
//  public string this[int index]
//   { get
//        {  if(index<0||index>=4)
//            return null;
//           else 
//           return myArray[index];
//        }
//    set
//      {   if(!(index<0||index>=4))
//           myArray[index]=value;
//      }    
//    }
//}
//class MainClass
//{    
//        static void Main()
//        {  MyIndexer idx=new MyIndexer();
//            idx[0]="vivid";
//            idx[1]="Miles";
//           for(int i=0;i<=3;i++)
//            Console.WriteLine("Element #{0}={1}",i,idx[i]);
//        }
//}
//////////////////////////////////////////////
//class SampleCollection<T>
//{
//    private T[] arr = new T[100];
//    public T this[int i]
//    {
//        get
//        {
//            return arr[i];
//        }
//        set
//        {
//            arr[i] = value;
//        }
//    }
//}
//// This class shows how client code uses the indexer
//class Program
//{
//    static void Main(string[] args)
//    {
//        SampleCollection<string> stringCollection = new SampleCollection<string>();
//        stringCollection[0] = "Hello, World";
//        System.Console.WriteLine(stringCollection[0]);
//    }
//}
///////////////////////
namespace A
{
    class test
    {
        private int[] arry = new int[5];
        //protected int Arry
        //{
        //    get
        //    {
        //        for (int i = 0; i < 5; i++)
        //        {
        //            return arry[i];
        //        }
        //    }
        //    set
        //    {
        //        for (int i = 0; i < 5; i++)
        //        {
        //            arry[i] = value;
        //        }
        //    }
        //}
        public int this[int index]
        {
            get
            {
                return arry[index];
            }
            set
            {
                arry[index] = value;
            }
        }
    }
    class print
    {
        static void Main()
        {
            test arr = new test();
            for (int i = 0; i < 5; i++)
            {
                arr[i] = i * i;
            }
            for (int i = 0; i < 5; i++)
            {
                System.Console.WriteLine("arr[{0}]={1}", i + 1, arr[i]);
            }
        }
    }
}