C# List Insert

Description

List Insert inserts an element into the List at the specified index.

Syntax

List.Insert has the following syntax.


public void Insert(
  int index,
  T item
)

Parameters

List.Insert has the following parameters.

  • index - The zero-based index at which item should be inserted.
  • item - The object to insert. The value can be null for reference types.

Example

The following code shows how to add elements a List.


using System;//from  w w w .java  2s . c o m
using System.Collections;
using System.Collections.Generic;

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

        words.Insert(0, "A"); // Insert at start 

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

}

The code above generates the following result.





















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




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack