Sub List ICollection - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Sub List ICollection

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;//from  w  w  w  .ja va2s  . c o  m

public class Main{
        public static IList<T> SubList<T>(ICollection<T> list, int start, int n)
        {
            List<T> ret = new List<T>(n);
            for (int i = start; i < start + n && i < list.Count; i++)
            {
                ret.Add(GetElement(list, i));
            }
            return ret;
        }
        public static ArrayList SubList(IEnumerable list, int start, int n)
        {
            ArrayList ret = new ArrayList(n);
            int i = 0;
            foreach (object o in list)
            {
                if (i >= start + n)
                    break;

                if (i >= start)
                {
                    ret.Add(o);
                }

                i++;
            }

            return ret;
        }
        public static IList SubList(IList list, int n)
        {
            ArrayList ret = new ArrayList(n);
            for (int i = 0; i < n && i < list.Count; i++)
            {
                ret.Add(list[i]);
            }
            return ret;
        }
}

Related Tutorials