自定义Collections

定义:
ContractedBlock.gif ExpandedBlockStart.gif 定义
 1None.gifusing System;  
 2None.gifusing System.Collections;
 3None.gif
 4None.gifnamespace Relaction.Collections
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// 
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    public class MyEnumerator:IEnumerator
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        private int _index;
12InBlock.gif        private string[] _list;
13InBlock.gif        public MyEnumerator(string[] list)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            _list = list;
16ExpandedSubBlockEnd.gif        }

17ContractedSubBlock.gifExpandedSubBlockStart.gif        IEnumerator 成员#region IEnumerator 成员
18InBlock.gif
19InBlock.gif        public void Reset()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            _index = 0;
22ExpandedSubBlockEnd.gif        }

23InBlock.gif
24InBlock.gif        public object Current
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            get
27ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
28InBlock.gif                return _list[_index];
29ExpandedSubBlockEnd.gif            }

30ExpandedSubBlockEnd.gif        }

31InBlock.gif
32InBlock.gif        public bool MoveNext()
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            _index++;
35InBlock.gif            if(_index >= _list.Length)
36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
37InBlock.gif                _index = _list.Length-1;
38InBlock.gif                return false;
39ExpandedSubBlockEnd.gif            }

40InBlock.gif            return true;
41ExpandedSubBlockEnd.gif        }

42InBlock.gif
43ExpandedSubBlockEnd.gif        #endregion

44InBlock.gif
45ExpandedSubBlockEnd.gif    }

46InBlock.gif    public class MyEnumerable:IEnumerable
47ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
48InBlock.gif        private string[] _list;
49InBlock.gif        public MyEnumerable()
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            _list = new string[3];
52InBlock.gif            _list[0= "1";
53InBlock.gif            _list[1= "2";
54InBlock.gif            _list[2= "3";
55ExpandedSubBlockEnd.gif        }

56ContractedSubBlock.gifExpandedSubBlockStart.gif        IEnumerable 成员#region IEnumerable 成员
57InBlock.gif
58InBlock.gif        public IEnumerator GetEnumerator()
59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
60InBlock.gif            return new MyEnumerator(_list);
61ExpandedSubBlockEnd.gif        }

62InBlock.gif
63ExpandedSubBlockEnd.gif        #endregion

64ExpandedSubBlockEnd.gif    }

65ExpandedBlockEnd.gif}

66None.gif

客户:
ContractedBlock.gif ExpandedBlockStart.gif 客户
1None.gif    private void button6_Click(object sender, System.EventArgs e)
2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
3InBlock.gif            Relaction.Collections.MyEnumerable m = new Relaction.Collections.MyEnumerable();
4InBlock.gif            foreach(string s in m)
5ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
6InBlock.gif                label1.Text += s.ToString();
7ExpandedSubBlockEnd.gif            }

8InBlock.gif
9ExpandedBlockEnd.gif        }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值