C#中这样的索引器是什么特性

xsff1024 2008-09-01 04:19:14
我们都知道C#中的索引器,它为我们访问类中的集合或数组类型的成员提供了方便。假如有一个类CS1,该类有一个集合成员AList。我们通过CS1的索引器(假如它有的话)就可以访问AList集合中的元素。即CS1obj[index](CS1obj是类CS1的一个实例)对应的就是AList[index]元素。

现在我们变通一下:假如AList集合中的各元素是另一个嵌套定义在CS1中的类CS2的实例。而CS2也有一个数组成员Array,并且CS2还有一个索引器用来访问该数组的元素。那么我们知道通过CS2obj[index](CS2obj是类CS2的实例)就可以访问Array[index]元素。

我们知道CS2obj是集合AList中的元素,而集合AList又是类CS1的成员。那么我通过CS1obj[index]可以访问AList[index]元素,也即访问到了CS2的一个实例,那么通过该实例的索引器是不是就可以访问到数组Array[]中的元素呢。即

CS1obj[index][index2]是不是就是Array[index2]的元素呢?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace 索引指示器示例
{
class CS1
{
ArrayList CS2List = new ArrayList(); ///定义一个集合
public CS1() ///CS1的构造函数,用来初始化集合CS2List
{ ///使CS2List中的成员都是类CS2的实例
for (int i = 0; i < 3; i++)
CS2List.Add(new CS2());
}
public class CS2 ///类CS2,嵌套定义在CS1中,注意该类必须是public的,否则会显示错误
{ ///信息:返回类型与索引器不一致。
int [] my=new int[5];
public CS2() ///类CS2的构造函数,用来初始化my[]数组。
{
for (int i = 0; i < 5; i++)
my[i] = i;
}
public int this[int index] ///类CS2的索引器,返回my[]数组中的元素
{
get
{
return my[index];
}
}
}
public CS2 this[int index] ///CS1的索引器,返回集合CS2List中的元素,也就是说返回CS2类型的一个引用
{
get ///所以你如果把CS2定义为private类型,在这里就会给出一个编译错误:
{ ///返回类型比索引器可访问级别地
return (CS2)CS2List[index];
}
}
}
class Program
{
static void Main(string[] args)
{
CS1 how = new CS1();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
Console.WriteLine(how[i][j]); ///访问CS1.CS2List[i].my[j]的值(当然在程序中不能这么写,这里是为了说明)
Console.ReadKey();
}
}
}

...全文
360 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xsff1024 2008-09-01
  • 打赏
  • 举报
回复
我想问的是这是索引器的什么特性,知道可以怎么用,但不知道它的深层次的内涵。
GhostAdai 2008-09-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xsff1024 的回复:]
不懂别乱说,///也是注释用的。
[/Quote]

我昏~~楼主弄明白“///”的注释用途是什么了没?“///”的注释是在编译时生成说明文档用的,把书好好看一遍,没哪个代码的注释用“///”的,标准注释只有两种:“//”、“/**/”。
另外,不知道楼主要问的是什么,如果“CS1obj[index][index2]是不是就是Array[index2]的元素呢?”就是提问的话,我要说了——代码都有了,试试就知道,用得着这里的发问吗?
xsff1024 2008-09-01
  • 打赏
  • 举报
回复
不懂别乱说,///也是注释用的。
hackztx 2008-09-01
  • 打赏
  • 举报
回复
我靠 你抄书呢。。。。

CS1obj[index][index2]是不是就是Array[index2]的元素呢?
只要CS1obj[index]=Array就成立以上条件

注释也不注释重点。。。。。


using System;
using System.Collections;


class CS1
{
//注释代码是两个/
// 3个/是对对象或者方法或者其他的一种类似于页面注释的说明
//代码乱其八糟
ArrayList objList;
public CS1()
{
objList = new ArrayList();
for (int i = 0; i < 3; i++)
{
objList.Add(new CS2());
}
}

public class CS2
{
int[] intList ;
public CS2()
{
intList = new int[5];
for (int i = 0; i < 5; i++)
{
intList[i] = i;
}
}
public int this[int index]//返回int List 的对应索引的值
{
get { return intList[index]; }
}
}
public CS2 this[int index]// 返回内部类的只读索引对象
{
get { return (CS2)objList[index]; }
}
}

class Program
{
static void Main(string[] args)
{
CS1 cs1List = new CS1();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
Console.WriteLine(cs1List[i][j]);
}
Console.ReadKey();
}
}
}

//一般默认的索引器为Get Set 也就是可读可写 现在你重写了就是Get了而没有了Set
//tm B的再也不看别人乱其八糟的代码了。。。。。。。。。。。。。。。。。。。。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧