鍍金池/ 教程/ C#/ C#索引器
C#屬性(Properties)
C#與Java比較
C#方法
C#枚舉
C#關(guān)鍵字
C# StreamReader類
C#不安全代碼
C#文件(I/O)
C#匿名方法
C#線程同步
C# Thread類
C#主線程
C#數(shù)據(jù)類型
C# FileStream類
C#預(yù)處理指令
C#繼承
C#循環(huán)
C#決策結(jié)構(gòu)
C#集合
C#反射
C#類型轉(zhuǎn)換
C#泛型
C# StringReader類
C#歷史
C#運算符重載
C#屬性
C#線程實例:Sleep()方法
C#線程示例:優(yōu)先級
C#線程實例:Join()方法
C# BinaryReader類
C#類
C#索引器
C# BinaryWriter類
C#序列化
C#常量和文字
C#程序結(jié)構(gòu)
C#封裝
C#事件
C#可空類型(nullable)
C#基本語法
C#異常處理
C#教程
C#接口
C# System.IO命名空間
C#線程命名實例
C# StringWriter類
C#線程實例
C#數(shù)組
C#正則表達式
C#命名空間
C#反序列化
C#與C++比較
C# TextWriter類
C#多線程
C#字符串
C#是什么?
C#變量
C# FileInfo類
C#線程實例:Abort()方法
C#結(jié)構(gòu)體
C#運算符
C#入門程序
C#多線程生命周期
C# TextReader類
C# DirectoryInfo類
C#委托

C#索引器

索引器允許對象被索引,例如:數(shù)組。 當(dāng)為類定義索引器時,此類與虛擬數(shù)組類似??梢允褂脭?shù)組訪問運算符([])訪問此類的實例。

語法

一維索引器的語法如下:

element-type this[int index]
{
   // The get accessor.
   get
   {
      // return the value specified by index
   }

   // The set accessor.
   set
   {
      // set the value specified by index
   }
}

索引器的使用

索引器的行為聲明在某種程度上與屬性相似。與屬性類似,可以使用getset訪問器定義索引器。但是,屬性返回或設(shè)置特定的數(shù)據(jù)成員,而索引器從對象實例返回或設(shè)置特定值。 換句話說,它將實例數(shù)據(jù)分解成較小的部分,并對每個部分進行索引,獲取或設(shè)置每個部分。

定義屬性涉及提供屬性名稱。索引器不是用名稱定義的,而是使用這個引用對象實例的關(guān)鍵字。以下示例演示了以下概念:

using System;
namespace IndexerApplication
{
    class IndexedNames
    {
        private string[] namelist = new string[size];
        static public int size = 10;
        public IndexedNames()
        {
            for (int i = 0; i < size; i++)
                namelist[i] = "N. A.";
        }

        public string this[int index]
        {
            get
            {
                string tmp;

                if (index >= 0 && index <= size - 1)
                {
                    tmp = namelist[index];
                }
                else
                {
                    tmp = "";
                }

                return (tmp);
            }
            set
            {
                if (index >= 0 && index <= size - 1)
                {
                    namelist[index] = value;
                }
            }
        }

        static void Main(string[] args)
        {
            IndexedNames names = new IndexedNames();
            names[0] = "Maxsu";
            names[1] = "Sukida";
            names[2] = "Mark";
            names[3] = "Jame";
            names[4] = "Davinder";
            names[5] = "Lucy";
            names[6] = "Lily";
            for (int i = 0; i < IndexedNames.size; i++)
            {
                Console.WriteLine(names[i]);
            }

            Console.ReadKey();
        }
    }
}

當(dāng)上述代碼被編譯并執(zhí)行時,它產(chǎn)生以下結(jié)果:

Maxsu
Sukida
Mark
Jame
Davinder
Lucy
Lily
N. A.
N. A.
N. A.

重載索引器

索引器可以重載。索引器也可以聲明為多個參數(shù),每個參數(shù)可能是不同的類型。 索引不一定是整數(shù)。 C# 允許索引為其他類型,例如:字符串。

以下示例演示了重載的索引器:

using System;
namespace IndexerApplication
{
    class IndexedNames
    {
        private string[] namelist = new string[size];
        static public int size = 10;
        public IndexedNames()
        {
            for (int i = 0; i < size; i++)
            {
                namelist[i] = "N. A.";
            }
        }

        public string this[int index]
        {
            get
            {
                string tmp;

                if (index >= 0 && index <= size - 1)
                {
                    tmp = namelist[index];
                }
                else
                {
                    tmp = "";
                }

                return (tmp);
            }
            set
            {
                if (index >= 0 && index <= size - 1)
                {
                    namelist[index] = value;
                }
            }
        }
        public int this[string name]
        {
            get
            {
                int index = 0;
                while (index < size)
                {
                    if (namelist[index] == name)
                    {
                        return index;
                    }
                    index++;
                }
                return index;
            }

        }

        static void Main(string[] args)
        {
            IndexedNames names = new IndexedNames();
            names[0] = "Maxsu";
            names[1] = "Richer";
            names[2] = "Nuber";
            names[3] = "DockJ";
            names[4] = "Vadder";
            names[5] = "Sukida";
            names[6] = "Ruby";

            //using the first indexer with int parameter
            for (int i = 0; i < IndexedNames.size; i++)
            {
                Console.WriteLine(names[i]);
            }

            //using the second indexer with the string parameter
            Console.WriteLine(names["Nuha"]);
            Console.ReadKey();
        }
    }
}

當(dāng)上述代碼被編譯并執(zhí)行時,它產(chǎn)生以下結(jié)果:

Maxsu
Richer
Nuber
DockJ
Vadder
Sukida
Ruby
N. A.
N. A.
N. A.
10