8.C#编程指南-索引器

索引器允许类或结构的实例像数组一样进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。

View Code
class SampleCollection<T>
{
    // Declare an array to store the data elements.
    private T[] arr = new T[100];

    // Define the indexer, which will allow client code
    // to use [] notation on the class instance itself.
    // (See line 2 of code in Main below.)        
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

// This class shows how client code uses the indexer.
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();

        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}

索引器概述:

  • 使用索引器可以用类似于数组的方式为对象建立索引。
  • get访问器返回值。set访问器分配值。
  • this关键字用于定义索引器。
  • value关键字用于定义由set索引器分配的值。
  • 索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
  • 索引器可被重载。
  • 索引器可以有多个形参,例如当访问二维数组时。

使用索引器

索引器在语法上方便您创建客户端应用程序可将其作为数组访问的结构接口 索引器经常是在主要用于封装内部集合或数组的类型中实现的 例如,假定具有一个名为 TempRecord 的类,此类表示在 24 小时内的 10 个不同时间记录的华氏度。 此类包含一个表示温度的 float 类型的名为“temps”的数组和表示记录温度的日期的 DateTime 通过在此类中实现一个索引器,客户端可以通过 float temp = tr[4] 而不是 float temp = tr.temps[4] 语法访问 TempRecord 实例中的温度。 索引器表示法不仅简化了客户端应用程序的语法,还使其他开发人员能够更加直观地理解类及其用途。

要声明类或结构上的索引器,请使用 this 关键字,如下例所示:

public int this[int index]    // Indexer declaration
{
    // get and set accessors
}

索引器的签名由其形参的数量和类型组成。它不包括索引器类型或形参名。如果在同一类中声明一个以上的索引器,则它们必须具有不同的签名。
索引器值不属于变量;因此,不能将索引器值作为ref或out参数进行传递。

要为索引器提供一个其他语言可以使用的名字,请使用声明中的name特性。例如:

//此索引器将具有名称 TheItem。 不提供名称特性将生成 Item 默认名称。 
[System.Runtime.CompilerServices.IndexerName("TheItem")]
public int this [int index]   // Indexer declaration
{
}

下面的示例说明如何声明私有数组字段、temps 和索引器。 使用索引器可直接访问实例 tempRecord[i] 另一种使用索引器的方法是将数组声明为 public 成员并直接访问它的成员 tempRecord.temps[i]

请注意,当计算索引器的访问时(例如,在 Console.Write 语句中),将调用 get 访问器。 因此,如果 get 访问器不存在,将发生编译时错误。

View Code
class TempRecord
{
    // Array of temperature values
    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 };

    // To enable client code to validate input 
    // when accessing your indexer.
    public int Length
    {
        get { return temps.Length; }
    }
    // Indexer declaration.
    // If index is out of range, the temps array will throw the exception.
    public float this[int index]
    {
        get
        {
            return temps[index];
        }

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

class MainClass
{
    static void Main()
    {
        TempRecord tempRecord = new TempRecord();
        // Use the indexer's set accessor
        tempRecord[3] = 58.3F;
        tempRecord[5] = 60.1F;

        // Use the indexer's get accessor
        for (int i = 0; i < 10; i++)
        {
            System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

    }
}
/* Output:
        Element #0 = 56.2
        Element #1 = 56.7
        Element #2 = 56.5
        Element #3 = 58.3
        Element #4 = 58.8
        Element #5 = 60.1
        Element #6 = 65.9
        Element #7 = 62.1
        Element #8 = 59.2
        Element #9 = 57.5
    */

C#并不将索引类型限制为整数。例如,对索引器使用字符串可能是有用的。通过搜索集合内的字符串并返回相应的值,可以实现此类索引器。由于访问器可被重载,字符串和整数版本可以共存。

View Code
//在此例中,声明了存储星期几的类。 声明了一个 get 访问器,它接受字符串(天名称),并返回相应的整数。 例如,星期日将返回 0,星期一将返回 1,等等。
// Using a string as an indexer value
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

提高索引器的安全性和可靠性有两种主要的方法:

  • 确保结合某一类型的错误处理策略,以处理万一客户端代码传入无效索引值的情况。 在本主题前面的第一个示例中,TempRecord 类提供了 Length 属性,使客户端代码能够在将输入传递给索引器之前对其进行验证。 也可以将错误处理代码放入索引器自身内部。 确保为用户记录在索引器的访问器中引发的任何异常。 有关更多信息,请参见 异常设计准则
  • 应当get 和 set 访问器的可访问性设置尽可能多的限制。 这一点对 set 访问器尤为重要。 有关更多信息,请参见 限制访问器可访问性(C# 编程指南)

接口索引器

索引器可在接口上声明。接口索引器的访问器与类索引器的访问器具有以下方面的不同:

  • 接口访问器不使用修饰符。
  • 接口访问器没有体。
View Code
// Indexer on an interface:
    public interface ISomeInterface
    {
        // Indexer declaration:
        int this[int index]
        {
            get;
            set;
        }
    }

    // Implementing the interface.
    class IndexerClass : ISomeInterface
    {
        private int[] arr = new int[100];
        public int this[int index]   // indexer declaration
        {
            get
            {
                // The arr object will throw IndexOutOfRange exception.
                return arr[index];
            }
            set
            {
                arr[index] = value;
            }
        }
    }

    class MainClass
    {
        static void Main()
        {
            IndexerClass test = new IndexerClass();
            System.Random rand = new System.Random();
            // Call the indexer to initialize its elements.
            for (int i = 0; i < 10; i++)
            {
                test[i] = rand.Next();
            }
            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
            }

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
    /* Sample output:
        Element #0 = 360877544
        Element #1 = 327058047
        Element #2 = 1913480832
        Element #3 = 1519039937
        Element #4 = 601472233
        Element #5 = 323352310
        Element #6 = 1422639981
        Element #7 = 1797892494
        Element #8 = 875761049
        Element #9 = 393083859
     */

只有当类例用同一索引器签名实现一个以上的接口时,为避免多义性需要使用完全限定名。例如,如果Employee类实现的是两个接口ICitizen和IEmployee,并且这两个接口具有相同的索引器签名,则必须使用显式接口成员实现。即,以下索引器声明:

public string IEmployee.this
{
}
public string ICitizen.this
{
}

属性和索引器之间的区别

Property

索引器

允许像调用公共数据成员一样调用方法。

允许对一个对象本身使用数组表示法来访问该对象内部集合中的元素。

可通过简单的名称进行访问。

可通过索引器进行访问。

可以为静态成员或实例成员。

必须为实例成员。

属性的 get 访问器没有参数。

索引器的 get 访问器具有与索引器相同的形参表。

属性的 set 访问器包含隐式 value 参数。

除了参数外,索引器的 set 访问器还具有与索引器相同的形参表。

支持对自动实现的属性(C# 编程指南)使用短语法。

不支持短语法。

posted on 2012-05-03 23:26  YeChun  阅读(1098)  评论(0编辑  收藏  举报

导航