//当给类添加了索引器后,使用索引器的形式类似于数组,请看
using System;public class SpellingList
{
protected string[] words=new string[size];
static public int size=10; public SpellingList()
{
for(int x=0;x<size;x++)
words[x]=string.Format("Word{0}",x);
} public string this[int index]
{
get
{
string tmp;
if(index>=0&&index<=size-1)
tmp=words[index];
else
tmp=""; return(tmp);
}
set
{
if(index>=0&&index<=size-1)
words[index]=value;
}
}
}public class TestApp
{
public static void Main()
{
SpellingList myList=new SpellingList();
myList[3]="=========";
myList[4]="Brad";
myList[7]="==========";
for(int x=0;x<SpellingList.size;x++)
Console.WriteLine(myList[x]); Console.Read();
}
}//请问此时该如何使用这个类定义数组,又该如何使用这个类数组?无法按下面的方式使用了
public static void Main()
{
SpellingList[] myList=new SpellingList[2];

Console.WriteLine(myList[0].GetType()); Console.Read();
}

解决方案 »

  1.   

    首先你在
    SpellingList[] myList=new SpellingList[2];
    之后要对每个对象进行初始化然后要访问SpellingList中的索引东西,可以如下:
    (myList[0])[3] = "=========";
      

  2.   

    //这样?
    public static void Main()
    {
    SpellingList[] myList=new SpellingList[2];
    myList[0]=new SpellingList();
    myList[1]=new SpellingList();
    myList[0][3]="alskdjfsdf";
    Console.WriteLine(myList[0][3]); Console.Read();
    }
      

  3.   

    //完整一点的用法:
    public static void Main()
    {
    SpellingList[] myList=new SpellingList[2];
    myList[0]=new SpellingList();
    myList[1]=new SpellingList();
    myList[0][3]="alskdjfsdf";
    for(int x=0;x<SpellingList.size;x++)
                Console.WriteLine(myList[0][x]); Console.WriteLine("===============");
    for(int x=0;x<SpellingList.size;x++)
    Console.WriteLine(myList[1][x]);
    Console.Read();
    }