Insert an element into the Collection at the specified index in CSharp

Description

The following code shows how to insert an element into the Collection at the specified index.

Example


using System;/*w w  w. ja v a2 s  .com*/
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<string> myData = new Collection<string>();

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");
 
        myData.Insert(0,"D");

        foreach( string item in myData)
        {
            Console.WriteLine(item);
        } 
    }
}

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