- C# 基础教程
- C# - 主页
- C# - 概述
- C# - 环境
- C# - 程序结构
- C# - 基本语法
- C# - 数据类型
- C# - 类型转换
- C# - 变量
- C# - 常量
- C# - 运算符
- C# - 决策
- C# - 循环
- C# - 封装
- C# - 方法
- C# - 可空值
- C# - 数组
- C# - 字符串
- C# - 结构
- C# - 枚举
- C# - 类
- C# - 继承
- C# - 多态性
- C# - 运算符重载
- C# - 接口
- C# - 命名空间
- C# - 预处理器指令
- C# - 正则表达式
- C# - 异常处理
- C# - 文件 I/O
- C# 高级教程
- C# - 属性
- C# - 反射
- C# - 属性
- C# - 索引器
- C# - 委托
- C# - 事件
- C# - 集合
- C# - 泛型
- C# - 匿名方法
- C# - 不安全代码
- C# - 多线程
- C# 有用资源
- C# - 问题与解答
- C# - 快速指南
- C# - 有用的资源
- C# - 讨论
C# - 索引器
索引器允许对对象(例如数组)进行索引。当您为类定义索引器时,此类的Behave类似于虚拟数组。然后,您可以使用数组访问运算符 ([ ]) 访问此类的实例。
句法
一维索引器具有以下语法 -
element-type this[int index] {
// The get accessor.
get {
// return the value specified by index
}
// The set accessor.
set {
// set the value specified by index
}
}
索引器的使用
索引器的Behave声明在某种程度上类似于属性。与属性类似,您可以使用get和set访问器来定义索引器。但是,属性返回或设置特定的数据成员,而索引器则返回或设置对象实例的特定值。换句话说,它将实例数据分成更小的部分,并对每个部分进行索引,获取或设置每个部分。
定义属性涉及提供属性名称。索引器不是用名称定义的,而是用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.
超载索引器
索引器可能会过载。索引器还可以使用多个参数进行声明,并且每个参数可以是不同的类型。索引不必是整数。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";
//using the first indexer with int parameter
for (int i = 0; i < IndexedNames.size; i++) {
Console.WriteLine(names[i]);
}
//using the second indexer with the string parameter
Console.WriteLine(names["Nuha"]);
Console.ReadKey();
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A. 2
