Get a sub range of elements from List in CSharp

Description

The following code shows how to get a sub range of elements from List.

Example


using System;/*from  w  ww.j  av a  2  s  .  co  m*/
using System.Collections;
using System.Collections.Generic;

class Sample
{
    public static void Main()
    {
        List<string> words = new List<string>();  

        words.Add("A");
        words.Add("B");
        words.Add("C");
        words.Add("D");
        words.Add("E");
        words.Add("F");


        Console.WriteLine(words[0]);
        Console.WriteLine(words[words.Count - 1]);

        foreach (string s in words) {
            Console.WriteLine (s);
        }
        IList<string> subset = words.GetRange (1, 2); // 2nd->3rd words

        foreach (string s in subset) {
            Console.WriteLine (s);
        }

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary