Strong-Type Collection

it2022-05-09  25

自定义集合类: 自定义强类型集合类   1using System;  2using System.Collections;  3  4namespace Relaction.Collections  5{  6    /**//// <summary>  7    ///   8    /// </summary>  9    public class MyStrongTypeList:IList 10    { 11        private ArrayList _list; 12        public MyStrongTypeList() 13        { 14            _list = new ArrayList(); 15        } 16        IList 成员#region IList 成员 17 18        public bool IsReadOnly 19        { 20            get 21            { 22                return _list.IsReadOnly; 23            } 24        } 25 26        public object this[int index] 27        { 28            get 29            { 30                return _list[index]; 31            } 32            set 33            { 34                _list[index] = value; 35            } 36        } 37 38        public void RemoveAt(int index) 39        { 40            _list.RemoveAt(index); 41        } 42 43        void IList.Insert(int index, object value) 44        { 45            _list.Insert(index,value); 46        } 47 48        public void Insert(int index,string value) 49        { 50            ((IList)this).Insert(index,value); 51        } 52 53        void IList.Remove(object value) 54        { 55            _list.Remove(value); 56        } 57        public void Remove(string value) 58        { 59            ((IList)this).Remove(value); 60        } 61 62        bool IList.Contains(object value) 63        { 64            return _list.Contains(value); 65        } 66 67        public bool Contains(string value) 68        { 69            return ((IList)this).Contains(value); 70        } 71 72        public void Clear() 73        { 74            _list.Clear(); 75        } 76 77        int IList.IndexOf(object value) 78        { 79            return _list.IndexOf(value); 80        } 81 82        public int IndexOf(string value) 83        { 84            return ((IList)this).IndexOf(value); 85        } 86 87        int IList.Add(object value) 88        { 89            return _list.Add(value); 90        } 91 92        public int Add(string value) 93        { 94            return ((IList)this).Add(value); 95        } 96 97        public bool IsFixedSize 98        { 99            get100            {101                return _list.IsFixedSize;102            }103        }104105        #endregion106107        ICollection 成员#region ICollection 成员108109        public bool IsSynchronized110        {111            get112            {113                return _list.IsSynchronized;114            }115        }116117        public int Count118        {119            get120            {121                return _list.Count;122            }123        }124125        public void CopyTo(Array array, int index)126        {127            _list.CopyTo(array,index);128        }129130        public object SyncRoot131        {132            get133            {134                return _list.SyncRoot;135            }136        }137138        #endregion139140        IEnumerable 成员#region IEnumerable 成员141142        public IEnumerator GetEnumerator()143        {144            return _list.GetEnumerator();145        }146147        #endregion148    }149}150 客户代码: 客户代码  1    private void button9_Click(object sender, System.EventArgs e) 2        { 3            Relaction.Collections.MyStrongTypeList list = new Relaction.Collections.MyStrongTypeList(); 4            list.Add("1"); 5            list.Add("2"); 6            list.Add("nantest"); 7            for(int i =0;i<list.Count;i++) 8            { 9                label1.Text += list[i].ToString();10            }11        }

转载于:https://www.cnblogs.com/nanshouyong326/archive/2006/11/28/575054.html


最新回复(0)