索引

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

在下面的示例中,定义了一个泛型类,并为其提供了简单的 getset 访问器方法(作为分配和检索值的方法)。 Program 类为存储字符串创建了此类的一个实例。

 

 

 1  
 2 class SampleCollection<T>
 3 {
 4     // Declare an array to store the data elements.
 5     private T[] arr = new T[100];
 6 // Define the indexer, which will allow client code
 7     // to use [] notation on the class instance itself.
 8     // (See line 2 of code in Main below.)        
 9     public T this[int i]
10     {
11         get
12         {
13             // This indexer is very simple, and just returns or sets
14             // the corresponding element from the internal array.
15             return arr[i];
16         }
17         set
18         {
19             arr[i] = value;
20         }
21     }
22 }
23 // This class shows how client code uses the indexer.
24 class Program
25 {
26     static void Main(string[] args)
27     {
28         // Declare an instance of the SampleCollection type.
29         SampleCollection<string> stringCollection = new SampleCollection<string>();
30 // Use [] notation on the type.
31         stringCollection[0] = "Hello, World";
32         System.Console.WriteLine(stringCollection[0]);
33     }
34 }
35 // Output:
36 // Hello, World.

 

 

 

 

 

 

 

 

 

 

说明

有关更多示例,请参见 相关章节

 

索引器概述

 

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

 

 1 class TempRecord
 2 {
 3     // Array of temperature values
 4     private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 
 5                                             61.3F, 65.9F, 62.1F, 59.2F, 57.5F };
 6 // To enable client code to validate input 
 7     // when accessing your indexer.
 8     public int Length
 9     {
10         get { return temps.Length; }
11     }
12     // Indexer declaration.
13     // If index is out of range, the temps array will throw the exception.
14     public float this[int index]
15     {
16         get
17         {
18             return temps[index];
19         }
20 set
21         {
22             temps[index] = value;
23         }
24     }
25 }
26 class MainClass
27 {
28     static void Main()
29     {
30         TempRecord tempRecord = new TempRecord();
31         // Use the indexer's set accessor
32         tempRecord[3] = 58.3F;
33         tempRecord[5] = 60.1F;
34 // Use the indexer's get accessor
35         for (int i = 0; i < 10; i++)
36         {
37             System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
38         }
39 // Keep the console window open in debug mode.
40         System.Console.WriteLine("Press any key to exit.");
41         System.Console.ReadKey();
42 }
43 }
44 /* Output:
45         Element #0 = 56.2
46         Element #1 = 56.7
47         Element #2 = 56.5
48         Element #3 = 58.3
49         Element #4 = 58.8
50         Element #5 = 60.1
51         Element #6 = 65.9
52         Element #7 = 62.1
53         Element #8 = 59.2
54         Element #9 = 57.5
55     */
56  
57 ******************************************************************************************
58  
59  
60 // Using a string as an indexer value
61 class DayCollection
62 {
63     string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
64 // This method finds the day or returns -1
65     private int GetDay(string testDay)
66     {
67 for (int j = 0; j < days.Length; j++)
68         {
69             if (days[j] == testDay)
70             {
71                 return j;
72             }
73         }
74 throw new System.ArgumentOutOfRangeException(testDay, "testDay must be in the form \"Sun\", \"Mon\", etc");
75     }
76 // The get accessor returns an integer for a given string
77     public int this[string day]
78     {
79         get
80         {
81             return (GetDay(day));
82         }
83     }
84 }
85 class Program
86 {
87     static void Main(string[] args)
88     {
89         DayCollection week = new DayCollection();
90         System.Console.WriteLine(week["Fri"]);
91 // Raises ArgumentOutOfRangeException
92         System.Console.WriteLine(week["Made-up Day"]);
93 // Keep the console window open in debug mode.
94         System.Console.WriteLine("Press any key to exit.");
95         System.Console.ReadKey();
96     }
97 }
98 // Output: 5

 

 

 

posted @ 2018-01-20 21:54  奔跑的蒲公英  阅读(161)  评论(0编辑  收藏  举报