我们都知道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();
}
}
}
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();
}
}
}
只要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的再也不看别人乱其八糟的代码了。。
我昏~~楼主弄明白“///”的注释用途是什么了没?“///”的注释是在编译时生成说明文档用的,把书好好看一遍,没哪个代码的注释用“///”的,标准注释只有两种:“//”、“/**/”。
另外,不知道楼主要问的是什么,如果“CS1obj[index][index2]是不是就是Array[index2]的元素呢?”就是提问的话,我要说了——代码都有了,试试就知道,用得着这里的发问吗?