实现ICollection例子

it2022-05-09  25

实现ICollection的范例 自定义  1using System; 2using System.Collections; 3 4namespace Relaction.Collections 5{ 6    /**//// <summary> 7    ///  8    /// </summary> 9    public class MyCollections:ICollection10    {11        private string[] _list;12        private object _root = new object();13        public MyCollections()14        {15            _list = new string[3]{"1","2","3"};16        }17        ICollection 成员#region ICollection 成员1819        public bool IsSynchronized20        {21            get22            {23                return true;24            }25        }2627        public int Count28        {29            get30            {31                return _list.Length;32            }33        }3435        public void CopyTo(Array array, int index)36        {37            _list.CopyTo(array,index);38        }3940        public object SyncRoot41        {42            get43            {44                return _root;45            }46        }4748        #endregion4950        IEnumerable 成员#region IEnumerable 成员5152        public IEnumerator GetEnumerator()53        {54            return _list.GetEnumerator();55        }5657        #endregion58    }59}60 客户代码: 客户代码 1    private void button7_Click(object sender, System.EventArgs e)2        {3           Relaction.Collections.MyCollections c = new Relaction.Collections.MyCollections();4            foreach(string s in c)5            {6                label1.Text += s.ToString();7            }8        }

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

相关资源:数据结构—成绩单生成器

最新回复(0)