自定义Collections

it2022-05-09  25

定义: 定义  1using System;   2using System.Collections; 3 4namespace Relaction.Collections 5{ 6    /**//// <summary> 7    ///  8    /// </summary> 9    public class MyEnumerator:IEnumerator10    {11        private int _index;12        private string[] _list;13        public MyEnumerator(string[] list)14        {15            _list = list;16        }17        IEnumerator 成员#region IEnumerator 成员1819        public void Reset()20        {21            _index = 0;22        }2324        public object Current25        {26            get27            {28                return _list[_index];29            }30        }3132        public bool MoveNext()33        {34            _index++;35            if(_index >= _list.Length)36            {37                _index = _list.Length-1;38                return false;39            }40            return true;41        }4243        #endregion4445    }46    public class MyEnumerable:IEnumerable47    {48        private string[] _list;49        public MyEnumerable()50        {51            _list = new string[3];52            _list[0= "1";53            _list[1= "2";54            _list[2= "3";55        }56        IEnumerable 成员#region IEnumerable 成员5758        public IEnumerator GetEnumerator()59        {60            return new MyEnumerator(_list);61        }6263        #endregion64    }65}66 客户: 客户 1    private void button6_Click(object sender, System.EventArgs e)2        {3            Relaction.Collections.MyEnumerable m = new Relaction.Collections.MyEnumerable();4            foreach(string s in m)5            {6                label1.Text += s.ToString();7            }89        }

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


最新回复(0)