天天看點

索引器

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# 程式設計指南)使用短文法。
    //不支援短文法。


}
      

樹立目标,保持活力,gogogo!