Wednesday, 19 February 2014

how to implement icollection interface in c#

ICollection Interface use for creating the collection of objects.
In below code sample implementing the ICollection imterface to the base class.

public class BasePage : ICollection
    {
         List _arraList;
        static int _currPos = 0;
        public BasePage()
        {
            _arraList = new List();
            _currPos = 0;
        }
        public void Add(T Item)
        { _arraList.Add(Item); }
        #region ICollection Members
        public void CopyTo(Array array, int index)
        {
            if (object.ReferenceEquals(array, null))
            {
                throw new ArgumentNullException(
                    "Null array reference",
                    "array"
                    );
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "Index is out of range",
                    "index"
                    );
            }
            if (array.Rank > 1)
            {
                throw new ArgumentException(
                    "Array is multi-dimensional",
                    "array"
                    );
            }
            foreach (object o in this)
            {
                array.SetValue(o, index);
                index++;
            }
        }
        public bool IsSynchronized
        {
            get { return false; }
        }
        public object SyncRoot
        {
            get { return this; }
        }
        #endregion ICollection Members
        public int Count
        {
            get { return _arraList.Count; }
        }
        public IEnumerator GetEnumerator()
        {
            foreach (T item in _arraList)
            {
                yield return item;
            }
        }



    }

No comments:

Post a Comment

Others

You will like to read it