zoukankan      html  css  js  c++  java
  • C# 索引器基础

    索引器在语法上方便您创建客户端应用程序可将其作为数组访问的结构接口

    要声明类或结构上的索引器,请使用 this 关键字,如下例所示:

    public int this[int index]    // Indexer declaration
    {
        // get and set accessors
    }
    

    实例1

    class TempRecord
    {
        // Array of temperature values
        private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F,
                                                61.3F, 65.9F, 62.1F, 59.2F, 57.5F };

        // To enable client code to validate input
        // when accessing your indexer.
        public int Length
        {
            get { return temps.Length; }
        }
        // Indexer declaration.
        // If index is out of range, the temps array will throw the exception.
        public float this[int index]
        {
            get
            {
                return temps[index];
            }

            set
            {
                temps[index] = value;
            }
        }
    }

    class MainClass
    {
        static void Main()
        {
            TempRecord tempRecord = new TempRecord();
            // Use the indexer's set accessor
            tempRecord[3] = 58.3F;
            tempRecord[5] = 60.1F;

            // Use the indexer's get accessor
            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
            }

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();

        }
    }
    /* Output:
            Element #0 = 56.2
            Element #1 = 56.7
            Element #2 = 56.5
            Element #3 = 58.3
            Element #4 = 58.8
            Element #5 = 60.1
            Element #6 = 65.9
            Element #7 = 62.1
            Element #8 = 59.2
            Element #9 = 57.5
        */


    实例2

    // Using a string as an indexer value
    class DayCollection
    {
        string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };

        // This method finds the day or returns -1
        private int GetDay(string testDay)
        {

            for (int j = 0; j < days.Length; j++)
            {
                if (days[j] == testDay)
                {
                    return j;
                }
            }

            throw new System.ArgumentOutOfRangeException(testDay, "testDay must be in the form \"Sun\", \"Mon\", etc");
        }

        // The get accessor returns an integer for a given string
        public int this[string day]
        {
            get
            {
                return (GetDay(day));
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DayCollection week = new DayCollection();
            System.Console.WriteLine(week["Fri"]);

            // Raises ArgumentOutOfRangeException
            System.Console.WriteLine(week["Made-up Day"]);

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
    // Output: 5


  • 相关阅读:
    C#网页数据采集(三)HttpWebRequest
    C#获取局域网ip
    C#调用Mail发送QQ邮件
    C#操作Excel(NPOI)
    html文字两行后,就用省略号代替剩下的
    js的dom测试及实例代码
    js循环数组(总结)
    黑马vue---61、为什么vue组件的data要是一个函数
    黑马vue---59-60、组件中的data和methods
    黑马vue---31-32、vue过滤器实例
  • 原文地址:https://www.cnblogs.com/yuloe2012/p/2705278.html
Copyright © 2011-2022 走看看