游戏人生
首页
(current)
GameDevTools
登陆
|
注册
个人中心
注销
C# 基础教程
C# 教程
C# 简介
C# 与.net
C# 开发工具
C# 程序结构
C# 基本语法
C# 数据类型
C# 类型转换
C# 变量
C# 常量
C# 运算符
C# 判断
C# 循环
C# 封装
C# 方法
C# 可空类型(Nullable)
C# 数组(Array)
C# 字符串(string)
C# 结构体(Struct)
C# 枚举(Enum)
C# 类(Class)
C# 继承
C# 多态
C# 运算符重载
C# 接口(Interface)
C# 命名空间(Namespace)
C# 预处理器指令
C# 正则表达式
C# 异常处理
C# 文件的输入与输出
C# 高级教程
C# 特性(Attribute)
C# 反射(Reflection)
C# 属性(Property)
C# 索引器(Indexer)
C# 委托(Delegate)
C# 事件(Event)
C# 集合(Collection)
C# 泛型(Generic)
C# 匿名方法
C# 不安全代码
C# 多线程
C# 实例
C# 正则表达式实例(1)-fprintf立即刷新
<< C# 属性(Property)
C# 委托(Delegate) >>
C# 索引器(Indexer)
索引器(Indexer) 允许一个对象可以像数组一样被索引。当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符([ ])来访问该类的实例。 ------------ #### 语法 一维索引器的语法如下: ```csharp element-type this[int index] { // get 访问器 get { // 返回 index 指定的值 } // set 访问器 set { // 设置 index 指定的值 } } ``` ------------ #### 索引器(Indexer)的用途 索引器的行为的声明在某种程度上类似于属性(property)。就像属性(property),您可使用 get 和 set 访问器来定义索引器。但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。换句话说,它把实例数据分为更小的部分,并索引每个部分,获取或设置每个部分。 定义一个属性(property)包括提供属性名称。索引器定义的时候不带有名称,但带有 this 关键字,它指向对象实例。下面的实例演示了这个概念: ```csharp 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. ------------ #### 重载索引器(Indexer) 索引器(Indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。没有必要让索引器必须是整型的。C# 允许索引器可以是其他类型,例如,字符串类型。 下面的实例演示了重载索引器: ```csharp 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
<< C# 属性(Property)
C# 委托(Delegate) >>
提交
5e32b22d498b3f00945f89f6