C# List GetRange

Description

List GetRange creates a shallow copy of a range of elements in the source List .

Syntax

List.GetRange has the following syntax.


public List<T> GetRange(
  int index,
  int count
)

Parameters

List.GetRange has the following parameters.

  • index - The zero-based List index at which the range starts.
  • count - The number of elements in the range.

Example


using System;/*ww  w.ja v a 2s.  co m*/
using System.Collections;
using System.Collections.Generic;

class Sample
{
    public static void Main()
    {
        //Create a List of Strings
        //String-typed list
        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("First:"+words[0]);  // first word 
        Console.WriteLine ("Last:"+words[words.Count - 1]);  // last word 

        foreach (string s in words) 
            Console.WriteLine ("all:"+s);   // all words 

        IList<string> subset = words.GetRange (1, 2); // 2nd->3rd words
   }
}

The output:





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack