扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
//假设你有一个人员集合 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); } void IList.Remove(object item) { Remove((Person)item); } ... } |
public class DogCollection : IList { ... private ArrayList _Dogs = new ArrayList(); public Dog this[int index] { get { return (Dog)_Dogs[index]; } } public int Add(Dog item) { _Dogs.Add(item); return _Dogs.Count - 1;} public void Remove(Dog item) { _Dogs.Remove(item); } object IList.this[int index] { get { return _Dogs[index]; } set { _Dogs[index] = (Dog)value; }} int IList.Add(object item) { return Add((Dog)item); } void IList.Remove(object item) { Remove((Dog)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]; |
public class IntCollection : IList { ... private ArrayList _Ints = new ArrayList(); public int this[int index] { get { return (int)_Ints[index]; } } public int Add(int item) { _Ints.Add(item); return _Ints.Count - 1;} public void Remove(int item) { _Ints.Remove(item); } object IList.this[int index] { get { return _Ints[index]; } set { _Ints[index] = (int)value; }} int IList.Add(object item) { return Add((int)item); } void IList.Remove(object item) { Remove((int)item); } ... } static void Main(string[] args) { IntCollection ints = new IntCollection(); ints.Add(5); //装箱 int i = ints[0]; //拆箱 } |
static void Main() { List<int> ints = new List<int>(); ints.Add(5); //不用装箱 int i = ints[0]; //不用拆箱 } |
static void Swap<T>(ref T a, ref T b) { Console.WriteLine("You sent the Swap() method a {0}", typeof(T)); T temp; temp = a; a = b; b = temp; } |
public class Point<T> { private T _x; private T _y; public T X { get { return _x; } set { _x = value; }} public T Y { get { return _y; } set { _y = value; }} public override string ToString() { return string.Format("[{0}, {1}]", _x, _y); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者