Export (0) Print
Expand All

StringCollection.Insert Method

Inserts a string into the StringCollection at the specified index.

Namespace: System.Collections.Specialized
Assembly: System (in system.dll)

public void Insert (
	int index,
	string value
)
public void Insert (
	int index, 
	String value
)
public function Insert (
	index : int, 
	value : String
)
Not applicable.

Parameters

index

The zero-based index at which value is inserted.

value

The string to insert. The value can be a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentOutOfRangeException

index is less than zero.

-or-

index greater than Count.

Duplicate strings are allowed in StringCollection.

If index is equal to Count, value is added to the end of StringCollection.

In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accommodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hash table.

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

The following code example adds new elements to the StringCollection.

using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Adds a range of elements from an array to the end of the StringCollection.
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "After adding a range of elements:" );
      PrintValues( myCol );

      // Adds one element to the end of the StringCollection and inserts another at index 3.
      myCol.Add( "* white" );
      myCol.Insert( 3, "* gray" );

      Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
      PrintValues( myCol );

   }

   public static void PrintValues( IEnumerable myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

*/

import System.* ;
import System.Collections.* ;
import System.Collections.Specialized.* ;

public class SamplesStringCollection
{      
    public static void main(String[] args)
    {        
        // Creates and initializes a new StringCollection.
        StringCollection myCol =  new StringCollection();
        
        Console.WriteLine("Initial contents of the StringCollection:");
        PrintValues(myCol);
        
        // Adds a range of elements from an array to the end of the 
        // StringCollection.
        String myArr[] = new String[]{"RED", "orange", "yellow", "RED", 
            "green", "blue", "RED", "indigo", "violet", "RED"};
        myCol.AddRange(myArr);
        
        Console.WriteLine("After adding a range of elements:");
        PrintValues(myCol);
        
        // Adds one element to the end of the StringCollection and inserts
        // another at index 3.
        myCol.Add("* white");
        myCol.Insert(3, "* gray");
        
        Console.WriteLine("After adding \"* white\" to the end and inserting"
            + " \"* gray\" at index 3:");
        PrintValues(myCol);
    } //main

    public static void PrintValues(IEnumerable myCol) 
    {
        Object obj = null;
        IEnumerator objEnum = myCol.GetEnumerator();

        while (objEnum.MoveNext()) { 
            obj = objEnum.get_Current();
            Console.WriteLine("   {0}", obj);
        }

        Console.WriteLine();
    } //PrintValues
} //SamplesStringCollection 

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft