public class Person{ private string _name; public string Name { get { return _name; } set { _name = value;}} }
//假設你有一個人員集合
public class PersonCollection : IList { ... private ArrayList _Persons = new ArrayList(); public Person this[int index] { get { return (Person)_Persons[index]; } }
public int Add(Person item) { _Persons.Add(item); return _Persons.Count - 1;}
public void Remove(Person item) { _Persons.Remove(item); }
object IList.this[int index] { get { return _Persons[index]; } set { _Persons[index] = (Person)value; }}
int IList.Add(object item) { return Add((Person)item); }
List<Person> persons = new List<Person>(); persons.Add(new Person()); Person person = persons[0]; List<Dog> dogs = new List<Dog>(); dogs.Add(new Dog()); Dog dog = dogs[0];