摘自:http://www.cnitblog.com/yide/archive/2012/04/11/78764.html
C# 索引器四个例子:普通索引器,字符串索引器,接口定义索引器,泛型索引器。
//普通索引器
using System; namespace Index1 { 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 }; System.DateTime date { get; set; } public int Length { get { return temps.Length; } } public float this[int index] { get { return temps[index]; } set { temps[index] = value; } } } class MainClass { static void Main() { TempRecord tempRecord = new TempRecord(); tempRecord[3] = 58.3F; tempRecord[5] = 60.1F; for (int i = 0; i < 10; i++) { if (i < tempRecord.Length) { System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]); } else { System.Console.WriteLine("Index value of {0} is out of range", i); } } } } }
//字符串,自定义索引器
using System; namespace Index2 { class DayCollection { string[] days = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" }; private int GetDay(string testDay) { int i = 0; foreach (string day in days) { if (day == testDay) { return i; } i++; } return -1; } public int this[string day] { get { return (GetDay(day)); } } } class Program { static void Main(string[] args) { DayCollection week = new DayCollection(); System.Console.WriteLine(week["星期五"]); System.Console.WriteLine(week["星期八"]); } } }
//泛型索引器
using System; namespace Index3 { class SampleCollection<T> { private T[] arr = new T[100]; //this 关键字用于定义索引器。 //索引器不必根据整数值进行索引,自定义决定如何定义特定的查找机制。 //索引器可以有多个形参,例如当访问二维数组时。 //索引器可被重载。 public T this[int i] { get { return arr[i]; } set { arr[i] = value; } } } // This class shows how client code uses the indexer class Program { static void Main(string[] args) { SampleCollection<string> stringCollection = new SampleCollection<string>(); stringCollection[0] = "Hello, World"; System.Console.WriteLine(stringCollection[0]); } } }
//接口实现索引器
using System; namespace Index4 { public interface ISomeInterface { int this[int index] { get; set; } } class IndexerClass : ISomeInterface { private int[] arr = new int[100]; public int this[int index] { get { if (index < 0 || index >= 100) { return 0; } else { return arr[index]; } } set { if (!(index < 0 || index >= 100)) { arr[index] = value; } } } } class MainClass { static void Main() { IndexerClass test = new IndexerClass(); test[2] = 4; test[5] = 32; for (int i = 0; i <= 10; i++) { System.Console.WriteLine("Element #{0} = {1}", i, test[i]); } } } }
摘自:http://www.cnblogs.com/LilianChen/archive/2013/10/09/3358970.html
1. 什么是索引
索引是一组get和set访问器,类似于属性的访问器。
2. 索引和属性
- 和属性一样,索引不用分配内存来存储
- 索引和属性都主要被用来访问其他数据成员,这些成员和它们关联,它们为这些成员提供设置和获取访问:(1)属性通常是访问单独的数据成员;(2)缩影通常是访问多个数据成员
- 索引可以只有一个访问器,也可以两个都有
- 索引总是实例成员,因此,索引不能被声明为static
- 和属性一样,实现get和set访问器的代码不必一定关联到某个字段或属性。这段代码可以做任何事情或者什么也不做,只要get访问器返回某个指定类型的值即可
3. 声明索引
- 索引没有名称,在名称的位置是关键字this
- 参数列表在方括号中间
- 参数列表中至少必须声明一个参数
4. 两个索引的示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication11 { class Employee { public string LastName; public string FirstName; public string CityOfBirth; public string this[int index] { set { switch (index) { case 0: LastName = value; break; case 1: FirstName = value; break; case 2: CityOfBirth = value; break; default: throw new ArgumentOutOfRangeException("Index"); } } get { switch (index) { case 0: return LastName; case 1: return FirstName; case 2: return CityOfBirth; default: throw new ArgumentOutOfRangeException("Index"); } } } } class Example { int Temp0; int Temp1; public int this[int index] { get { return (0 == index) ? Temp0 : Temp1; } set { if (0 == index) { Temp0 = value; } else { Temp1 = value; } } } } class Program { static void Main(string[] args) { Employee emp1 = new Employee(); emp1[0] = "Doe"; emp1[1] = "Jane"; emp1[2] = "Dallas"; Console.WriteLine("{0}", emp1[0]); Console.WriteLine("{0}", emp1[1]); Console.WriteLine("{0}", emp1[2]); Example a = new Example(); a[0] = 15; a[1] = 20; Console.WriteLine("Values--T0:{0}, T1:{1}", a[0], a[1]); Console.ReadLine(); } } }
5. 索引重载
只要索引的参数列表不同,类可以有不止一个索引。
class MyClass { public string this[int index] { get { return "Testing"; } set { } } public string this[int index1, int index2] { get { return "Testing"; } set { } } public int this[float index] { get { return 3; } set { } } }
6. 访问器的访问修饰符
默认情况下,成员的两个访问器有和成员自身相同的访问级别。在特定情况下,成员的访问器可以有不同的访问级别。
- 仅当成员既有get访问器也有set访问器时,其访问器才能有访问修饰符
- 虽然两个访问器都必须出现,但它们中只能有一个访问修饰符
- 访问器的访问修饰符必须比成员的访问级别有更严格的限制性