美文网首页
20、索引器

20、索引器

作者: silence_k | 来源:发表于2016-12-19 17:41 被阅读0次

1. 简介:

索引器允许类或结构的实例就像数组一样进行索引。 索引器类似于属性,不同之处在于它们的取值函数采用参数。 典型用法 数组中 []

2. 语法:

关键字符: this[]

    class IndexTest
    {
        private int[] arrayInts = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        
        public int this[int index]
        {
            get { return arrayInts[index]; }
            set { arrayInts[index] = value; }
        }
    }

    // 使用该类
    class Program
    {
        static void Main(string[] args)
        {
            IndexTest test = new IndexTest();
            Console.WriteLine(test[5]); // 6
            Console.ReadKey();
        }
    }

3. 索引器概述:

  • 使用索引器可以用类似于数组的方式为对象建立索引。

  • get 取值函数返回值。 set取值函数分配值。

  • this关键字用于定义索引器。

  • value 关键字用于定义由 set 索引器分配的值。

  • 索引器不必根据整数值进行索引;由你决定如何定义特定的查找机制。

  • 索引器可被重载。

  • 索引器可以有多个形参,例如当访问二维数组时。

4. 索引器其他用法:

1> 索引器可以有多个形参,例如多维数组的用法。

        public int this[int index, int index2]
        {
            get
            {
                if (index >= arrayInts.Length)
                {
                    throw new Exception("索引超出了界线");
                }
                else
                {
                    return arrayInts[index];                    
                }
                
            }
            set { arrayInts[index] = value; }
        }

        // 调用:
        static void Main(string[] args)
        {
            IndexTest test = new IndexTest();
            Console.WriteLine(test[12,6]); 
            Console.ReadKey();
        }

2> C# 并不将索引类型限制为整数。 例如,对索引器使用字符串可能是有用的。 通过搜索集合内的字符串并返回相应的值,可以实现此类索引器。

案例说明:在此例中,声明了存储星期几的类。 声明了一个 get
访问器,它接受字符串(天名称),并返回相应的整数。 例如,星期日将返回 0,星期一将返回 1,等等。

        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

相关文章

  • 20、索引器

    1. 简介: 索引器允许类或结构的实例就像数组一样进行索引。 索引器类似于属性,不同之处在于它们的取值函数采用参数...

  • 索引构建

    1 索引构建 索引构建 建立倒排索引的过程,就是索引构建 索引器 构建索引的程序或者计算机,就是索引器 索引器需要...

  • 索引器, 正则表达式(Regex)

    /* * 索引器: * 作用:让对象具有快速访问元素的能力 * 索引器和数组的区别: * 1.索引器的索引类型不限...

  • 10月23日C#学习总结

    今天学习了属性、索引器、继承 属性:C#不直接访问类的数据,通过访问器访问(get,set)。 索引器:索引器(i...

  • C#之索引器

    什么是索引器 索引器允许类或者结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于他们的访问采用...

  • sql自学笔记(十四)——MySQL8.0版本的新特性(四)

    优化器索引 隐藏索引 降序索引 函数索引 隐藏索引 MySQL8.0开始支持隐藏索引(invisible inde...

  • Elasticsearch---索引管理、基于scroll+bu

    创建索引的语法 示例: 添加索引(索引一旦建立,不能修改) 删除索引 分词器的修改与定制 修改分词器设置 默认分词...

  • 索引器

    一.索引器是类似属性的东西 1.1和属性一样,索引器不用分配内存来储存。 1.2索引器和属性都主要被用来访问其他数...

  • 索引器

    索引器是一组get和set访问器,与属性类似。如下展示了一个类的索引器的表现形式,该类可以回去和设置string型...

  • 索引器

    索引器 没有名字 ,索引器的内部本质(ILSpy的IL模式下看)类型this[参数]{get;set;} 可以是只...

网友评论

      本文标题:20、索引器

      本文链接:https://www.haomeiwen.com/subject/zwutvttx.html