索引器

using System;

public class IndexTest
{

    int[] arr = new int[100];
    public int this[int index]
    {
        get
        {
            if (arr.Length == null||arr.Length==0)
                return 0;
            if (index < 0 || index > arr.Length)
                return 0;
            else
                return arr[index];
        }
        set
        {
            if (index > -1 && index < arr.length)
                arr[index] = value;
        }

    }

    public static void Main()
    {
        IndexTest it = new IndexTest();
        it[0] = 1;
        it[5] = 5;
        it[6] = 6;
        it[7] = 7;
        it[8] = 8;
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("#{0} is {1}", i, it[i]);
        }

    }

}

posted on 2010-08-07 23:22  椅望  阅读(124)  评论(1编辑  收藏  举报

导航