indexerclass.cs

来自「这是.net2005学习不可缺少的教程」· CS 代码 · 共 51 行

CS
51
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace indexDemo
{
    class IndexerClass
    {
        private int[] arr = new int[100];
        public int this[int index]   // Indexer declaration
        {
            get
            {
                // Check the index limits.
                if (index < 0 || index >= 100)
                {
                    return 0;
                }
                else
                {
                    return arr[index];
                }
            }
            set
            {
                if (!(index < 0 || index >= 100))
                {
                    arr[index] = value;
                }
            }
        }
    }

    class MainClass
    {
        static void Main()
        {
            IndexerClass test = new IndexerClass();
            // Call the indexer to initialize the elements #3 and #5.
            test[3] = 256;
            test[5] = 1024;
            for (int i = 0; i <= 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
            }
        }
    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?