举例说明我对索引器的认识
2010-07-25 11:47
211 查看
索引器允许类或结构的实例就想数组一样进行索引。
this关键字定义索引。不必根据整数值进行索引,由开发者自己决定特定的查找机制。
索引器可被重载,可以有多个参数。
索引器用法举例1:
class MyArrayList
{
object[] objarr = new object[0];
public MyArrayList(int Cap) //构造函数
{
objarr = new object[Cap];
}
public object this[int i] //定义索引
{
get
{
return objarr[i];
}
set
{
objarr[i] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
MyArrayList mal = new MyArrayList(1);//定义长度为1的数组
mal[0]="aaa"; //赋值给索引为0的实例
Console.WriteLine(mal[0]); //读取索引为0的实例的值
}
输出结果为: aaa
本例中,可以看出索引器类似于属性,可以直接通过索引存取实例的值。
举例2:
class Myclass
{
string[] Days =new string[7]
{"Mon","Tue","Wen","Thurs","Fri","Sat","Sun"};
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in Days) //定义查找机制,遍历数组,返回查
{ //找对象的序列号
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}
public int this[string day] //定义只读索引器,get方法调用GetDay
{ //方法,查找对象
get
{
return (GetDay(day));
}
}
}
class Program
{
static void Main(string[] args)
{
Myclass week = new Myclass();
System.Console.WriteLine(week["Fri"]);
System.Console.WriteLine(week["Made-up Day"]);
}
}
结果为: 4 -1
本例中,由开发者自己定义查找机制。通过索引器,返回字符串在实例中的索引序号。
有更多的用法,欢迎留言交流。本文出自 “马田野的个人博客” 博客,请务必保留此出处http://supermaty.blog.51cto.com/1802607/359081
this关键字定义索引。不必根据整数值进行索引,由开发者自己决定特定的查找机制。
索引器可被重载,可以有多个参数。
索引器用法举例1:
class MyArrayList
{
object[] objarr = new object[0];
public MyArrayList(int Cap) //构造函数
{
objarr = new object[Cap];
}
public object this[int i] //定义索引
{
get
{
return objarr[i];
}
set
{
objarr[i] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
MyArrayList mal = new MyArrayList(1);//定义长度为1的数组
mal[0]="aaa"; //赋值给索引为0的实例
Console.WriteLine(mal[0]); //读取索引为0的实例的值
}
输出结果为: aaa
本例中,可以看出索引器类似于属性,可以直接通过索引存取实例的值。
举例2:
class Myclass
{
string[] Days =new string[7]
{"Mon","Tue","Wen","Thurs","Fri","Sat","Sun"};
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in Days) //定义查找机制,遍历数组,返回查
{ //找对象的序列号
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}
public int this[string day] //定义只读索引器,get方法调用GetDay
{ //方法,查找对象
get
{
return (GetDay(day));
}
}
}
class Program
{
static void Main(string[] args)
{
Myclass week = new Myclass();
System.Console.WriteLine(week["Fri"]);
System.Console.WriteLine(week["Made-up Day"]);
}
}
结果为: 4 -1
本例中,由开发者自己定义查找机制。通过索引器,返回字符串在实例中的索引序号。
有更多的用法,欢迎留言交流。本文出自 “马田野的个人博客” 博客,请务必保留此出处http://supermaty.blog.51cto.com/1802607/359081
相关文章推荐
- 聚簇索引(Clustered Index)和非聚簇索引 (Non- Clustered Index)举例说明
- 【安卓面试题】Activity和Task的启动模式有哪些?每种含义是什么?举例说明各自的应用场景
- 关于static方法和变量,举例说明
- 大地坐标的概念 大地坐标系的举例和说明分类
- java中Collections有什么用,举例说明?
- 详解Qt,并举例说明动态编译(shared)和静态编译(static)以及debug and release 编译版本区别(可产生静态版的Debug版本,需要把-release 改为 –debug-and-release)
- 举例说明如何在C#中使用C/C++写的DLL
- css中padding、margin两个重要属性的详细介绍及举例说明
- 简单举例说明android自定义view的方法
- 举例说明android中实现发送邮件的方法
- Python中list的详细操作描述(举例说明)
- 举例说明利用《知网》计算词语相似度
- 举例说明BroadcastReceiver的使用方法----实现android手机开机之后显示画面的功能
- 举例说明如何在C#中使用C/C++写的DLL
- UML类图符号 各种关系说明以及举例
- Android.mk和Application.mk文件语法规范说明及举例
- 举例说明Spark RDD的分区、依赖
- 举例说明source执行脚本和sh执行脚本的区别
- UML类图符号 各种关系说明以及举例
- AngularJS工作原理--举例说明