C#索引器


索引器允許類或者結構的實例按照與數組相同的方式進行索引取值,索引器與屬性類似,不同的是索引器的訪問是帶參的。

索引器和數組比較:

(1)索引器的索引值(Index)類型不受限制

(2)索引器允許重載

(3)索引器不是一個變量

索引器和屬性的不同點

(1)屬性以名稱來標識,索引器以函數形式標識

(2)索引器可以被重載,屬性不可以

(3)索引器不能聲明為static,屬性可以

 

一個簡單的索引器例子

using System;
using System.Collections;

public
class IndexerClass { private string[] name = new string[2]; //索引器必須以this關鍵字定義,其實這個this就是類實例化之后的對象 public string this[int index] { //實現索引器的get方法 get { if (index < 2) { return name[index]; } return null; } //實現索引器的set方法 set { if (index < 2) { name[index] = value; } } } } public class Test { static void Main() { //索引器的使用 IndexerClass Indexer = new IndexerClass(); //“=”號右邊對索引器賦值,其實就是調用其set方法 Indexer[0] = "張三"; Indexer[1] = "李四"; //輸出索引器的值,其實就是調用其get方法 Console.WriteLine(Indexer[0]); Console.WriteLine(Indexer[1]); } }

 以字符串作為下標,對索引器進行存取

public class IndexerClass
{
    //用string作為索引器下標的時候,要用Hashtable
    private Hashtable name = new Hashtable();

    //索引器必須以this關鍵字定義,其實這個this就是類實例化之后的對象
    public string this[string index]
    {
        get { return name[index].ToString(); 
        set { name.Add(index, value); }
    }
}
public class Test
{
    static void Main()
    {
        IndexerClass Indexer = new IndexerClass();
        Indexer["A0001"] = "張三";
        Indexer["A0002"] = "李四";
        Console.WriteLine(Indexer["A0001"]);
        Console.WriteLine(Indexer["A0002"]);
    }
}

 索引器的重載

public class IndexerClass
{
    private Hashtable name = new Hashtable();

    //1:通過key存取Values
    public string this[int index]
    {
        get { return name[index].ToString(); }
        set { name.Add(index, value); }
    }

    //2:通過Values存取key
    public int this[string aName]
    {
        get
        {
            //Hashtable中實際存放的是DictionaryEntry(字典)類型,如果要遍歷一個Hashtable,就需要使用到DictionaryEntry
            foreach(DictionaryEntry d in name)
            {
                if (d.Value.ToString() == aName)
                {
                    return Convert.ToInt32(d.Key);
                }
            }
            return -1;
        }
        set
        {
            name.Add(value, aName);
        }
    }
}
public class Test
{
    static void Main()
    {
        IndexerClass Indexer = new IndexerClass();

        //第一種索引器的使用
        Indexer[1] = "張三";//set訪問器的使用
        Indexer[2] = "李四";
       Console.WriteLine("編號為1的名字:" + Indexer[1]);//get訪問器的使用
        Console.WriteLine("編號為2的名字:" + Indexer[2]);

        Console.WriteLine();
        //第二種索引器的使用
        Console.WriteLine("張三的編號是:" + Indexer["張三"]);//get訪問器的使用
        Console.WriteLine("李四的編號是:" + Indexer["李四"]);
       Indexer["王五"] = 3;//set訪問器的使用
        Console.WriteLine("王五的編號是:" + Indexer["王五"]);
    }
}

 多參索引器

using System;
using System.Collections;

//入職信息類
public class EntrantInfo
{
    //姓名、編號、部門
    private string name;
    private int number;
    private string department;
    public EntrantInfo()
    {

    }
    public EntrantInfo(string name, int num, string department)
    {
        this.name = name;
        this.number = num;
        this.department = department;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Num
    {
        get { return number; }
        set { number = value; }
    }

    public string Department
    {
        get { return department; }
        set { department = value; }
    }
}

//聲明一個類EntrantInfo的索引器
public class IndexerForEntrantInfo
{
    private ArrayList ArrLst;//用於存放EntrantInfo類
    public IndexerForEntrantInfo()
    {
        ArrLst = new ArrayList();
    }

    //聲明一個索引器:以名字和編號查找存取部門信息
    public string this[string name, int num]
    {
        get
        {
            foreach (EntrantInfo en in ArrLst)
            {
                if (en.Name == name && en.Num == num)
                {
                    return en.Department;
                }
            }
            return null;
        }
        set
        {
            //new關鍵字:C#規定,實例化一個類或者調用類的構造函數時,必須使用new關鍵
            ArrLst.Add(new EntrantInfo(name, num, value));
        }
    }

    //聲明一個索引器:以編號查找名字和部門
    public ArrayList this[int num]
    {
        get
        {
            ArrayList temp = new ArrayList();
            foreach (EntrantInfo en in ArrLst)
            {
                if (en.Num == num)
                {
                    temp.Add(en);
                }
            }
            return temp;
        }
    }

    //還可以聲明多個版本的索引器...
}

public class Test
{
    static void Main()
    {
        IndexerForEntrantInfo Info = new IndexerForEntrantInfo();
        //this[string name, int num]的使用
        Info["張三", 101] = "人事部";
        Info["李四", 102] = "行政部";
        Console.WriteLine(Info["張三", 101]);
        Console.WriteLine(Info["李四", 102]);

        Console.WriteLine();

        //this[int num]的使用
        foreach (EntrantInfo en in Info[102])
        {
            Console.WriteLine(en.Name);
            Console.WriteLine(en.Department);
        }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM