上面那个用了泛型,这个看起来要简单点哈:
using System;
namespace CsharpStepByStep
{
/**//// <summary>
/// Program 的摘要说明。
/// </summary>
public class Person
{
private string name;
public Person(string Name)
{
name = Name;
}
public string Name
{
get {return name;}
}
}
public class Authors
{
private Person[] persons = new Person[5];
public Person this[int index]
{
get {return persons[index];}
set {persons[index]=value;}
}
}
public class SingleIndexer
{
public static void Main()
{
Authors authors = new Authors();
authors[0] = new Person("aa");
authors[1] = new Person("bb");
authors[2] = new Person("cc");
authors[3] = new Person("dd");
authors[4] = new Person("ee");
for(int i=0;i<5;i++)
{
Console.WriteLine(authors[i].Name);
}
Console.ReadLine();
}
}
}