实现ICollection例子

实现ICollection的范例
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 MyCollections:ICollection
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        private string[] _list;
12InBlock.gif        private object _root = new object();
13InBlock.gif        public MyCollections()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15ExpandedSubBlockStart.gifContractedSubBlock.gif            _list = new string[3]dot.gif{"1","2","3"};
16ExpandedSubBlockEnd.gif        }

17ContractedSubBlock.gifExpandedSubBlockStart.gif        ICollection 成员#region ICollection 成员
18InBlock.gif
19InBlock.gif        public bool IsSynchronized
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            get
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                return true;
24ExpandedSubBlockEnd.gif            }

25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27InBlock.gif        public int Count
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            get
30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
31InBlock.gif                return _list.Length;
32ExpandedSubBlockEnd.gif            }

33ExpandedSubBlockEnd.gif        }

34InBlock.gif
35InBlock.gif        public void CopyTo(Array array, int index)
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37InBlock.gif            _list.CopyTo(array,index);
38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40InBlock.gif        public object SyncRoot
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            get
43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
44InBlock.gif                return _root;
45ExpandedSubBlockEnd.gif            }

46ExpandedSubBlockEnd.gif        }

47InBlock.gif
48ExpandedSubBlockEnd.gif        #endregion

49InBlock.gif
50ContractedSubBlock.gifExpandedSubBlockStart.gif        IEnumerable 成员#region IEnumerable 成员
51InBlock.gif
52InBlock.gif        public IEnumerator GetEnumerator()
53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
54InBlock.gif            return _list.GetEnumerator();
55ExpandedSubBlockEnd.gif        }

56InBlock.gif
57ExpandedSubBlockEnd.gif        #endregion

58ExpandedSubBlockEnd.gif    }

59ExpandedBlockEnd.gif}

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

8ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值