大纲:
1.语法
2.Indexer的重载
3.基于接口的Indexer和代码强壮性
1.语法部分 ( Syntax )
索引器(Indexer)
允许一个对象可以像数组一样被索引。当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符([ ])来访问该类的实例。索引器(Indexer)的用途
索引器的行为的声明在某种程度上类似于属性(property)。就像属性(property),您可使用 get 和 set 访问器来定义索引器。但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。换句话说,它把实例数据分为更小的部分,并索引每个部分,获取或设置每个部分。
定义一个属性(property)包括提供属性名称。索引器定义的时候不带有名称,但带有 this 关键字,它指向对象实例。
案例:
创建一个 ConsoleApplication
class Program
{
//
//Indexer
//
static void Main(string[] args)
{
var indexer = new IndexedNames();
indexer[0] = "C#";
indexer[1] = "Java";
indexer[2] = "C";
indexer[3] = "Javascript";
indexer[4] = "Bootstrap";
indexer[5] = "AngularJS";
indexer[6] = "ASP.NET";
indexer[7] = "Lambda Expression";
indexer[8] = "Indexer";
indexer[9] = "Linq";
for (int i = 0; i <= 9; i++)
{
Console.WriteLine(indexer[i]);
}
Console.ReadKey();
}
}
class IndexedNames
{
private string[] nameList = new string[10];
/// <summary>
/// Constructor
/// </summary>
public IndexedNames()
{
for (int i = 0; i < nameList.Length; i++)
{
nameList[i] = "N/A";
}
}
/// <summary>
/// Indexer
/// </summary>
/// <param name="index">The value of incoming</param>
/// <returns></returns>
public string this[int index]
{
get
{
string temp = String.Empty;
//limit the range of the array "nameList" .
if (index >= 0 && index <= nameList.Length - 1)
{
temp = nameList[index];
}
return temp;
}
set
{
//limit the range of the array "nameList" .
if (index >= 0 && index <= nameList.Length - 1)
{
nameList[index] = value;
}
}
}
}
2.Indexer的重载
重载索引器(Indexer)
索引器(Indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。没有必要让索引器必须是整型的。C# 允许索引器可以是其他类型,例如,字符串类型。
案例:
创建一个ConsoleApplication,下面贴出Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
//
//Indexer
//
static void Main(string[] args)
{
var indexer = new IndexedNames();
indexer[0] = "C#";
indexer[1] = "Java";
indexer[2] = "C";
indexer[3] = "Javascript";
indexer[4] = "Bootstrap";
indexer[5] = "AngularJS";
indexer[6] = "ASP.NET";
indexer[7] = "Lambda Expression";
indexer[8] = "Indexer";
indexer[9] = "Linq";
for (int i = 0; i <= 9; i++)
{
Console.WriteLine(indexer[i]);
}
Console.WriteLine(indexer["Linq"]);//9
Console.WriteLine(indexer["Indexer"]);//8
Console.WriteLine(indexer["Unique"]);//-1
Console.ReadKey();
}
}
class IndexedNames
{
private string[] nameList = new string[10];
/// <summary>
/// Constructor
/// </summary>
public IndexedNames()
{
for (int i = 0; i < nameList.Length; i++)
{
nameList[i] = "N/A";
}
}
/// <summary>
/// Indexer
/// </summary>
/// <param name="index">The value of incoming</param>
/// <returns></returns>
public string this[int index]
{
get
{
string temp = String.Empty;
//limit the range of the array "nameList" .
if (index >= 0 && index <= nameList.Length - 1)
{
temp = nameList[index];
}
return temp;
}
set
{
//limit the range of the array "nameList" .
if (index >= 0 && index <= nameList.Length - 1)
{
nameList[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = 0;
while (index <nameList.Length)
{
if (nameList[index]==name)
{
return index;
}
index++;
}
return -1;
}
}
}
}
按下F5运行后效果如图:
3.基于接口的Indexer和代码强壮性
public interface ISomeInterface
{
int this[int index]
{
get;
set;
}
}
class IndexerClass : ISomeInterface
{
private int[] count = new int[100];
public int this[int index]
{
get
{
if (index>=0 &&index<=count.Length-1)
{
return count[index];
}
return -1;
}
set
{
count[index] = value;
}
}
}