实现ICollection

实现ICollection

注意: ICollection inherits IEnumerable


using System;
using System.Collections;//注意using Collections

using System.Collections.Generic;
using
 System.Text;

namespace
 CollectionTest
{
    
class
 Program
    {
        
static void Main(string
[] args)
        {
            WordCollection words 
= new
 WordCollection();
            
foreach (string value in
 words)
            {
                Console.WriteLine(value);
            }
            Console.Read();
        }
    }

    
public class
 WordCollection : ICollection
    {
        
private string
[] _list;
        
private object _root = new object
();

        
public
 WordCollection()
        {
            _list 
= new string[] { "You""and""me""!"
 };
        }

        
#region ICollection Members


        
public void CopyTo(Array array, int index)
        {
            _list.CopyTo(array, index);
        }

        
public int
 Count
        {
            
get { return
 _list.Length; }
        }

        
public bool
 IsSynchronized
        {
            
get { return true
; }
        }

        
public object
 SyncRoot
        {
            
get { return
 _root; }
        }

        
#endregion


        
#region IEnumerable Members

        
public IEnumerator GetEnumerator()
        {
            
return
 _list.GetEnumerator();
        }

        
#endregion

    }
}

 

  

posted @ 2010-09-15 16:57  英雄不问出处  阅读(472)  评论(0编辑  收藏  举报