Get or set the element at the specified index in a List in CSharp

Description

The following code shows how to get or set the element at the specified index in a List.

Example


//from w w w.  j ava  2s .  co m
using System;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    List<string> myData = new List<string>();
    
    Console.WriteLine(myData.Capacity);
    
    myData.Add("A");
    myData.Add("B");
    myData.Add("C");
    myData.Add("D");
    myData.Add("E");
    
    Console.WriteLine(myData[3]);
    
    myData[3] = "java2s.com";
    
    foreach(string s in myData){
       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