Free mag vol1 | Page 397

CHAPTER 9  COLLECTIONS AND GENERICS } public T this[int index] { get; set; } } When you create a List specifying Person objects, it is as if the List type were defined as follows: namespace System.Collections.Generic { public class List : IList, ICollection, IEnumerable, IReadOnlyList IList, ICollection, IEnumerable { ... public void Add(Person item); public ReadOnlyCollection AsReadOnly(); public int BinarySearch(Person item); public bool Contains(Person item); public void CopyTo(Person[] array); public int FindIndex(System.Predicate match); public Person FindLast(System.Predicate match); public bool Remove(Person item); public int RemoveAll(System.Predicate match); public Person[] ToArray(); public bool TrueForAll(System.Predicate match); public Person this[int index] { get; set; } } } Of course, when you create a generic List variable, the compiler does not literally create a brand new implementation of the List class. Rather, it will address only the members of the generic type you actually invoke. Specifying Type Parameters for Generic Members It is fine for a nongeneric class or structure to support a handful of generic members (e.g., methods and properties). In these cases, you would also need to specify the placeholder value at the time you invoke the method. For example, System.Array supports a several generic methods. Specifically, the nongeneric static Sort() method now has a generic counterpart named Sort(). Consider the following code snippet, where T is of type int: int[] myInts = { 10, 4, 2, 33, 93 }; // Specify the placeholder to the generic // Sort<>() method. Array.Sort(myInts); foreach (int i in myInts) { Console.WriteLine(i); } 336