索引器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Test2
{

    //索引器允许类或结构的实例就像数组一样进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。
    //使用索引器可以用类似于数组的方式为对象建立索引。
    //get 访问器返回值。set 访问器分配值。
    //this 关键字用于定义索引器。
    //value 关键字用于定义由 set 索引器分配的值。
    //索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
    //索引器可被重载。
    //索引器可以有多个形参,例如当访问二维数组时。




    //索引器的声明和使用索引器赋值
    //class SampleCollection<T>
    //{
    //    //声明类型为T,元素个数为100的数组。
    //    private T[] arr = new T[100];
    //    //声明一个索引器
    //    public T this[int i]
    //    {
    //        get
    //        {
    //            return arr[i];
    //        }
    //        set
    //        {
    //            arr[i] = value;
    //        }
    //    }
    //}


    //class Program
    //{
    //    static void Main(string[] args)
    //    {
    //        SampleCollection<string> stringCollection = new SampleCollection<string>();
    //        stringCollection[0] = "Hello, World";
    //        System.Console.WriteLine(stringCollection[0]);

    //        Console.ReadKey();
    //    }
    //}





    //使用数字作为索引值
    //class TempRecord
    //{

    //    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 };

    //    //访问索引时 为了使客户端代码验证输入
    //    public int Length
    //    {
    //        get { return temps.Length; }
    //    }
    //    // 声明索引器
    //    // 如果索引超出范围,temps数组将抛出异常。 
    //    public float this[int index]
    //    {
    //        get
    //        {
    //            return temps[index];
    //        }

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

    //class MainClass
    //{
    //    static void Main()
    //    {
    //        TempRecord tempRecord = new TempRecord();
    //        // 使用索引器的set访问器 
    //        tempRecord[3] = 58.3F;
    //        tempRecord[5] = 60.1F;

    //        //使用索引器的get访问器 
    //        for (int i = 0; i < 10; i++)
    //        {
    //            System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
    //        }


    //        System.Console.WriteLine("输入任意键结束");
    //        System.Console.ReadKey();

    //    }
    //}






    // 使用一个字符串作为索引值 
    //class DayCollection
    //{
    //    string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };

    //    // 这个方法找到一天或返回- 1 
    //    private int GetDay(string testDay)
    //    {

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

    //        throw new System.ArgumentOutOfRangeException(testDay, "超出范围");
    //    }

    //    // get访问器返回给定字符串的整数 
    //    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"]);

    //        // 引发范围异常的参数 
    //        System.Console.WriteLine(week["我是测试用例"]);


    //        System.Console.WriteLine("Press any key to exit.");
    //        System.Console.ReadKey();
    //    }
    //}





    //接口索引器的访问器与类索引器的访问器具有以下方面的不同:

    //接口访问器不使用修饰符。

    //接口访问器没有体。

    //因此,访问器的用途是指示索引器是读写、只读还是只写。



    // 定义一个接口
    //public interface ISomeInterface
    //{
    //    // 定义索引器
    //    int this[int index]
    //    {
    //        get;
    //        set;
    //    }
    //}

    // 继承接口
    //class IndexerClass : ISomeInterface
    //{

    //    private int[] arr = new int[100];
    //    // 定义索引器
    //    public int this[int index]
    //    {
    //        get
    //        {

    //            return arr[index];
    //        }
    //        set
    //        {
    //            arr[index] = value;
    //        }
    //    }


    //}


    //class MainClass
    //{
    //    static void Main()
    //    {
    //        IndexerClass test = new IndexerClass();
    //        // 调用索引初始化数组元素2和5  
    //        test[2] = 4;
    //        test[5] = 32;

    //        // 如果索引超出范围,则抛出异常
    //       // test[121] = 12;
    //        for (int i = 0; i <= 10; i++)
    //        {
    //            System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
    //        }


    //        System.Console.WriteLine("Press any key to exit.");
    //        System.Console.ReadKey();
    //    }
    //}




    //索引器和索引器的区别

    //索引器:
    //允许像调用公共数据成员一样调用方法。
    //允许对一个对象本身使用数组表示法来访问该对象内部集合中的元素。
    //可通过简单的名称进行访问。
    //可通过索引器进行访问。
    //可以为静态成员或实例成员。
    //必须为实例成员。

    //属性:
    //属性的 get 访问器没有参数。
    //索引器的 get 访问器具有与索引器相同的形参表。
    //属性的 set 访问器包含隐式 value 参数。
    //除了值参数外,索引器的 set 访问器还具有与索引器相同的形参表。
    //支持对自动实现的属性(C# 编程指南)使用短语法。
    //不支持短语法。


}
原文地址:https://www.cnblogs.com/hao-1234-1234/p/6141253.html