|
class SampleCollection<T> { private T[] arr = new T[100]; private T[,] arr2 = new T[10, 10]; public T this[int i] { get { return arr[i]; } set { arr[i] = value; } } public T this[int i, int j] { get { return arr2[i, j]; } set { arr2[i, j] = value; } } } // This class shows how client code uses the indexer class Program { static void Main(string[] args) { SampleCollection<string> stringCollection = new SampleCollection<string>(); stringCollection[0] = "Hello, World"; stringCollection[0, 0] = "i,j"; System.Console.WriteLine(stringCollection[0]); System.Console.WriteLine(stringCollection[0,0]); Console.ReadLine(); } }
//泛型的例子 //class Program //{ // static void Main(string[] args) // { // //产生一个customer的强类型集合 // List1<Customer> customers = new List1<Customer>(); // customers.Add(new Customer("Motown-Jobs")); // customers.Add(new Customer("Fatman's")); // foreach (Customer c in customers) // Console.WriteLine(c.CustomerName); // Console.ReadLine(); // } //} //public class Customer //{ // private string customerName = ""; // public string CustomerName // { // get { return customerName; } // set { customerName = value; } // } // public Customer(string customerName) // { // this.customerName = customerName; // } //} // 自定的泛型类: //public class List1<T> : CollectionBase //{ // public List() { } // public T this[int index] // { // get { return (T)List[index]; } // set { List[index] = value; } // } // public int Add(T value) // { // return List.Add(value); // } //}
|
一共有 0 条评论