C# 索引器 Indexer
c# 索引器 indexer
索引器(indexer) 允许一个对象可以像数组一样使用下标的方式来访问。
当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符 [ ] 来访问该类的的成员。
1. 语法
一维索引器的语法如下:
element-type this[int index]
{
// get 访问器
get
{
// 返回 index 指定的值
}
// set 访问器
set
{
// 设置 index 指定的值
}
}
2. 索引器(indexer)的用途
索引器的行为的声明在某种程度上类似于属性(property)。就像属性(property),您可使用 get 和 set 访问器来定义索引器。但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象范例的一个特定值。换句话说,它把范例数据分为更小的部分,并索引每个部分,获取或设置每个部分。
定义一个属性(property)包括提供属性名称。索引器定义的时候不带有名称,但带有 this 关键字,它指向对象范例。下面的范例演示了这个概念:
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] = "zara";
names[1] = "riz";
names[2] = "nuha";
names[3] = "asif";
names[4] = "davinder";
names[5] = "sunil";
names[6] = "rubic";
for ( int i = 0; i < indexednames.size; i++ )
{
console.writeline(names[i]);
}
console.readkey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
zara riz nuha asif davinder sunil rubic n. a. n. a. n. a.
3. 重载索引器(indexer)
索引器(indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。没有必要让索引器必须是整型的。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] = "zara";
names[1] = "riz";
names[2] = "nuha";
names[3] = "asif";
names[4] = "davinder";
names[5] = "sunil";
names[6] = "rubic";
// 使用带有 int 参数的第一个索引器
for (int i = 0; i < indexednames.size; i++)
{
console.writeline(names[i]);
}
// 使用带有 string 参数的第二个索引器
console.writeline(names["nuha"]);
console.readkey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
zara riz nuha asif davinder sunil rubic n. a. n. a. n. a. 2


