Sub List - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

Sub List

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;//from  ww  w  . j a va  2  s.com

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