索引器的应用
来源:互联网 发布:12306抢票软件 编辑:程序博客网 时间:2024/05/16 16:14
利用索引器,我们可以象使用数组一样对类,结构,和接口编制索引。在类和结构上定义索引器,需要使用this关键字。
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ConsoleTest
- {
- class mainClass
- {
- static void Main()
- {
- IndexerDemo demo = new IndexerDemo();
- int result=demo[3];
- Console.WriteLine(result); //Output 67
- }
- }
- class IndexerDemo
- {
- private int[] arrs = new int[] { 5, 8, 54, 67, 25, 1 };
- public int this[int index]
- {
- get
- {
- return arrs[index];
- }
- }
- }
- }
除了使用索引器时,需要使用参数,其余特性和属性相似。
注意:
1.使用索引器,可以象使用数组一样操作类,结构和接口
2.不一定要用整数索引
3.可以多载
4.可以多参
参考msdn.
利用索引器实现一个简单的集合类
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace Demo
- {
- class Demo3
- {
- static void Main()
- {
- MyArray arr = new MyArray();
- arr.Add("Hello");
- arr.Add("world");
- arr.Add("net");
- for (int i = 0; i < arr.Length; i++)
- {
- Console.WriteLine(arr[i]);
- }
- }
- }
- class MyArray
- {
- private string[] _items=new string[5];
- private int _size = 0;
- public void Add(string item)
- {
- _items[_size] = item;
- _size++;
- }
- public string this[int index]
- {
- get
- {
- return _items[index];
- }
- }
- public int Length
- {
- get
- {
- return _size;
- }
- }
- }
- }
- 索引器的应用
- 2010.4.7 索引器简单的应用
- mysql 索引的应用
- 索引指示器的应用
- 索引指示器的应用
- 强制索引的应用
- 索引序列的应用
- 索引的应用
- 索引的应用介绍
- MySQL索引的应用
- 位图索引的应用
- 索引的应用
- 索引的应用
- 索引的应用
- 主键,外键,索引,唯一索引的应用
- C#索引指示器的应用
- 索引的应用规律总结
- GridView行索引的应用
- SQL语句的并集UNION,交集JOIN(内连接,外连接),交叉连接(CROSS JOIN笛卡尔积),差集(NOT IN)
- 掌握 Facebook 应用程序开发,第 1 部分
- 建模揭秘,第 2 部分: 构建用户模型
- 删除默认共享
- 健壮的 Java 基准测试,第 1 部分:问题
- 索引器的应用
- SOA 治理框架和解决方案架构
- 窗口最小化,任务栏不显示图标的解决办法
- NET 学习方法论
- 闭包的概念、形式与应用
- 146条经典民间偏方曝光
- 使用失真门户可视化时变数据
- oracle数据文件为什么存在 Rfile# and file#
- 一个招聘要求!汗。。