List Indexer

In this chapter you will learn:

  1. How to use Indexer to get elements from a List

Get element with Indexer

using System;/*j a va2s.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:

Next chapter...

What you will learn in the next chapter:

  1. How to add and remove a range of values in a List