List<T>.Insert Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Inserts an element into the List<T> at the specified index.

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub Insert ( _
    index As Integer, _
    item As T _
)
public void Insert(
    int index,
    T item
)

Parameters

  • index
    Type: System.Int32
    The zero-based index at which item should be inserted.
  • item
    Type: T
    The object to insert. The value can be nulla null reference (Nothing in Visual Basic) for reference types.

Implements

IList<T>.Insert(Int32, T)

Exceptions

Exception Condition
ArgumentOutOfRangeException

index is less than 0.

-or-

index is greater than Count.

Remarks

List<T> accepts nulla null reference (Nothing in Visual Basic) as a valid value for reference types and allows duplicate elements.

If Count already equals Capacity, the capacity of the List<T> is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

If index is equal to Count, item is added to the end of List<T>.

This method is an O(n) operation, where n is Count.

Examples

The following code example demonstrates the Insert method, along with various other properties and methods of the List<T> generic class. After the list is created, elements are added. The Insert method is used to insert an item into the middle of the list. The item inserted is a duplicate, which is later removed using the Remove method.

Imports System.Collections.Generic

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim dinosaurs As New List(Of String)

      outputBlock.Text &= String.Format(vbLf & "Capacity: {0}", dinosaurs.Capacity) & vbCrLf

      dinosaurs.Add("Tyrannosaurus")
      dinosaurs.Add("Amargasaurus")
      dinosaurs.Add("Mamenchisaurus")
      dinosaurs.Add("Deinonychus")
      dinosaurs.Add("Compsognathus")

      outputBlock.Text &= vbCrLf
      For Each dinosaur As String In dinosaurs
         outputBlock.Text &= dinosaur & vbCrLf
      Next

      outputBlock.Text &= String.Format(vbLf & "Capacity: {0}", dinosaurs.Capacity) & vbCrLf
      outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf

      outputBlock.Text &= vbLf & String.Format("Contains(""Deinonychus"": {0}", _
          dinosaurs.Contains("Deinonychus")) & vbCrLf

      outputBlock.Text &= String.Format(vbLf & "Insert(2, ""Compsognathus"")") & vbCrLf
      dinosaurs.Insert(2, "Compsognathus")

      outputBlock.Text &= vbCrLf
      For Each dinosaur As String In dinosaurs
         outputBlock.Text &= dinosaur & vbCrLf
      Next

      outputBlock.Text &= String.Format(vbLf & "dinosaurs(3): {0}", dinosaurs(3)) & vbCrLf

      outputBlock.Text &= vbLf & "Remove(""Compsognathus"")" & vbCrLf
      dinosaurs.Remove("Compsognathus")

      outputBlock.Text &= vbCrLf
      For Each dinosaur As String In dinosaurs
         outputBlock.Text &= dinosaur & vbCrLf
      Next

      dinosaurs.TrimExcess()
      outputBlock.Text &= vbLf & "TrimExcess()" & vbCrLf
      outputBlock.Text &= String.Format("Capacity: {0}", dinosaurs.Capacity) & vbCrLf
      outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf

      dinosaurs.Clear()
      outputBlock.Text &= vbLf & "Clear()" & vbCrLf
      outputBlock.Text &= String.Format("Capacity: {0}", dinosaurs.Capacity) & vbCrLf
      outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf
   End Sub
End Class

' This code example produces the following output:
'
'Capacity: 0
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'Compsognathus
'
'Capacity: 8
'Count: 5
'
'Contains("Deinonychus"): True
'
'Insert(2, "Compsognathus")
'
'Tyrannosaurus
'Amargasaurus
'Compsognathus
'Mamenchisaurus
'Deinonychus
'Compsognathus
'
'dinosaurs(3): Mamenchisaurus
'
'Remove("Compsognathus")
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'Compsognathus
'
'TrimExcess()
'Capacity: 5
'Count: 5
'
'Clear()
'Capacity: 5
'Count: 0
using System;
using System.Collections.Generic;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      List<string> dinosaurs = new List<string>();

      outputBlock.Text += String.Format("\nCapacity: {0}", dinosaurs.Capacity) + "\n";

      dinosaurs.Add("Tyrannosaurus");
      dinosaurs.Add("Amargasaurus");
      dinosaurs.Add("Mamenchisaurus");
      dinosaurs.Add("Deinonychus");
      dinosaurs.Add("Compsognathus");

      outputBlock.Text += "\n";
      foreach (string dinosaur in dinosaurs)
      {
         outputBlock.Text += dinosaur + "\n";
      }

      outputBlock.Text += String.Format("\nCapacity: {0}", dinosaurs.Capacity) + "\n";
      outputBlock.Text += String.Format("Count: {0}", dinosaurs.Count) + "\n";

      outputBlock.Text += String.Format("\nContains(\"Deinonychus\"): {0}",
          dinosaurs.Contains("Deinonychus")) + "\n";

      outputBlock.Text += String.Format("\nInsert(2, \"Compsognathus\")") + "\n";
      dinosaurs.Insert(2, "Compsognathus");

      outputBlock.Text += "\n";
      foreach (string dinosaur in dinosaurs)
      {
         outputBlock.Text += dinosaur + "\n";
      }

      outputBlock.Text += String.Format("\ndinosaurs[3]: {0}", dinosaurs[3]) + "\n";

      outputBlock.Text += "\nRemove(\"Compsognathus\")" + "\n";
      dinosaurs.Remove("Compsognathus");

      outputBlock.Text += "\n";
      foreach (string dinosaur in dinosaurs)
      {
         outputBlock.Text += dinosaur + "\n";
      }

      dinosaurs.TrimExcess();
      outputBlock.Text += "\nTrimExcess()" + "\n";
      outputBlock.Text += String.Format("Capacity: {0}", dinosaurs.Capacity) + "\n";
      outputBlock.Text += String.Format("Count: {0}", dinosaurs.Count) + "\n";

      dinosaurs.Clear();
      outputBlock.Text += "\nClear()" + "\n";
      outputBlock.Text += String.Format("Capacity: {0}", dinosaurs.Capacity) + "\n";
      outputBlock.Text += String.Format("Count: {0}", dinosaurs.Count) + "\n";
   }
}

/* This code example produces the following output:

Capacity: 0

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
Compsognathus

Capacity: 8
Count: 5

Contains("Deinonychus"): True

Insert(2, "Compsognathus")

Tyrannosaurus
Amargasaurus
Compsognathus
Mamenchisaurus
Deinonychus
Compsognathus

dinosaurs[3]: Mamenchisaurus

Remove("Compsognathus")

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
Compsognathus

TrimExcess()
Capacity: 5
Count: 5

Clear()
Capacity: 5
Count: 0
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.