Results 1 to 13 of 13

Thread: Custom Generic List

Threaded View

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Custom Generic List

    Hi all!

    I tried to create my first custom list which implement the IList and IEnumerable
    interfaces the code is kinda long but if someone will be kind enough to quickly view it and give me some pointers if i took the wrong path along the way, and i guess if someone will want to learn it itself it could be good starting point so no hard to post the code anyway, well here it is:
    Code:
    class XList<T> : IList<T>
        {
            private List<T> internalList = new List<T>();
            public XList() {}
            private int _Index =0;
            public int Index
            {
                get { return _Index; }
            }
    
            #region Filtering
            public void Remove(Predicate<T> filter)
            {
                for (int i = internalList.Count-1;i>0; i--)
                {
                    if (filter(internalList[i]))
                    {
                        internalList.Remove(internalList[i]);
                    }
                }
            }
            #endregion
    
            #region Sorting methods
            public void Sort()
            {
                internalList.Sort();
            }
    
            public void Sort(IComparer<T> compartion)
            {
                internalList.Sort(compartion);
            } 
            #endregion
    
            #region Rotators
    
            /// <summary>
            /// noraml foreach loop but with the ability to get index
            /// use XList[T].Index to get the current index
            /// </summary>
            /// <returns>IEnumerable[T]</returns>
            public IEnumerable<T> WithIndex()
            {
                for(_Index = 0;_Index < internalList.Count;_Index++)
                {
                    yield return internalList[_Index];
                }
            }
            #endregion
    
            #region IList<T> methods implements
    
            public int IndexOf(T item)
            {
                return internalList.IndexOf(item);
            }
    
            public void Insert(int index, T item)
            {
                internalList.Insert(index, item);
            }
    
            public void RemoveAt(int index)
            {
                internalList.RemoveAt(index);
            }
    
            public T this[int index]
            {
                get
                {
                    return internalList[index];
                }
                set
                {
                    internalList[index] = value;
                }
            }
    
            public void Add(T item)
            {
                internalList.Add(item);
            }
    
            public void Clear()
            {
                internalList.Clear();
            }
    
            public bool Contains(T item)
            {
               return  internalList.Contains(item);
            }
    
            #region CopyTo overloads
    
            /// <summary>
            /// Copy XList into Array[T]
            /// </summary>
            /// <param name="array">Array[T]</param>
            /// <param name="arrayIndex">Start Index</param>
            public void CopyTo(T[] array, int arrayIndex)
            {
                internalList.CopyTo(array, arrayIndex);
            }
    
            /// <summary>
            /// Copy XList[T] into List[T]
            /// </summary>
            /// <param name="array">List[T]</param>
            /// <param name="arrayIndex">Start index</param>
            public void CopyTo(List<T> array, int arrayIndex)
            {
                foreach (var item in internalList)
                {
                    array.Add(item);
                }
            }
    
    
    
    
            /// <summary>
            /// Copy XList[T] into XList[T]
            /// </summary>
            /// <param name="array">XList[T]</param>
            /// <param name="arrayIndex">Start index</param>
            public void CopyTo(XList<T> array, int arrayIndex)
            {
                for(int i=0;i< array.Count;i++)
                {
                    array.Add(array[i]);
                }
            } 
    
            #endregion
    
            public int Count
            {
                get { return internalList.Count; }
            }
    
            public bool IsReadOnly
            {
                get { throw new NotImplementedException(); }
            }
    
            public bool Remove(T item)
            {
               return internalList.Remove(item);
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                return new XListEnumerator<T>(internalList);
            }
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return internalList.GetEnumerator();
    
            } 
            #endregion
    
        }
    
    
        #region XList Enumerator class
        public class XListEnumerator<T> :IEnumerator<T>
        {
            private List<T> internalList = new List<T>();
            private int _index = -1;
           
    
            #region Constructors
            public XListEnumerator(List<T> l)
            {
                internalList = l;
            }
            #endregion
    
    
            #region Methods
            
            #endregion
    
            #region IEnumerator Implements methods
            public object Current
            {
                get { return internalList[_index]; }
            }
    
            public bool MoveNext()
            {
                return ++_index < internalList.Count;
            }
    
            public void Reset()
            {
                _index = -1;
            } 
            #endregion
    
            T IEnumerator<T>.Current
            {
                get { return internalList[_index]; }
            }
    
            public void Dispose()
            {
                internalList = null;
            }
    
           
        }
    Last edited by motil; Oct 30th, 2010 at 09:06 AM.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width