- class Test : System.Collections.ICollection
- {
- string[] _list;
- object _root = new object();
- public Test()
- {
- _list = new string[]{"abc","def","ghi","jkl","mno","pqr","stu","vwx","yz"};
- }
- #region ICollection 成员
- public bool IsSynchronized
- {
- get
- {
- // TODO: 添加 Test.IsSynchronized getter 实现
- return false;
- }
- }
- public int Count
- {
- get
- {
- return this._list.Length;
- }
- }
- public void CopyTo(Array array, int index)
- {
- foreach(string s in this._list)
- {
- array.SetValue(s,index);
- index ++;
- }
- }
- public object SyncRoot
- {
- get
- {
- return this;
- }
- }
- #endregion
- #region IEnumerable 成员
- public System.Collections.IEnumerator GetEnumerator()
- {
- // TODO: 添加 Test.GetEnumerator 实现
- return new MyEnum(this._list);
- }
- #endregion
- }
- class MyEnum : System.Collections.IEnumerator
- {
- string[] str;
- private int cursor;
- public MyEnum(string[] list)
- {
- this.str = list;
- this.cursor = -1;
- }
- #region IEnumerator 成员
- public void Reset()
- {
- this.cursor = -1;
- }
- public object Current
- {
- get
- {
- if((this.cursor < 0) || this.cursor == str.Length)
- {
- throw new InvalidOperationException();
- }
- return this.str[this.cursor];
- }
- }
- public bool MoveNext()
- {
- if(this.cursor < str.Length)
- this.cursor ++;
- return (!(this.cursor == str.Length));
- }
- #endregion
- }
实现Icollection接口的对象
最新推荐文章于 2023-07-11 18:22:43 发布