Share via


BaseCollection.Insert Method

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Inserts an element at a specific position in the collection.

Namespace: Microsoft.SpeechServer.Dialog
Assembly: Microsoft.SpeechServer (in microsoft.speechserver.dll)

Syntax

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

Parameters

  • index
    The position at which the element is added.
  • item
    The object to add to the collection.

Exceptions

Exception type Condition
ArgumentNullException

item is null.

InvalidOperationException

item is already contained in the collection.

ArgumentOutOfRangeException

index is not a valid index in the collection.

NotSupportedException

The collection is read-only. The collection is a fixed size.

Example

The following example is presented on the reference pages for many members of this class. The example instantiates a simple class object that inherits from BaseCollection and exercises many of the BaseCollection methods and properties. In this example, a telephone number is inserted into the collection at index 2. No items are overwritten in this operation. Any items that were formerly at index 2 and higher have their indexes incremented by one.

// A simple class that inherits from the abstract class, BaseCollection<T>
public class bc : BaseCollection<string>
{
  public bc() : base() { }
}


private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
  bc coll_1 = new bc();
  // Add four phone numbers to the collection
  coll_1.Add("555-0000");
  coll_1.Add("555-0001");
  coll_1.Add("555-0010");
  coll_1.Add("555-0011");

  // Copy the numbers in coll_1 to another collection, coll_2
  bc coll_2 = new bc();
  coll_2.AddRange(coll_1);

  // Clear the coll_2 collection
  coll_2.Clear();

  // Check to see if coll_1 contains two phone numbers
  bool isIn = coll_1.Contains("555-0100"); //false
  isIn = coll_1.Contains("555-0000"); //true

  // Determine the index of a specific phone number
  int idx = coll_1.IndexOf("555-0010");

  // Insert a new phone number into the collection
  coll_1.Insert(2, "555-0100");

  // Copy the elements in the collection to an array
  string[] list = new string[10];
  coll_1.CopyTo(list, 0);

  // Remove the number that we just added
  // If result gets set to true, the removal succeeded
  bool result;
  result = coll_1.Remove("555-0100");

  // Try to remove a number that isn't in the collection
  // If result gets set to false, the removal did not succeed
  result = coll_1.Remove("555-1111");

  // Insert the number into the collection at index 2
  coll_1.Insert(2, "555-0100");

  // Remove the number again
  coll_1.RemoveAt(2);

  // Determine the number of elements in the collection
  int count = coll_1.Count;

  // Use the collection's Item property to write a number to the collection 
  // This will overwrite whatever item was at that position in the collection
  coll_1[2] = "555-0100";

  // Use the collection's Item property to read a number in the collection
  string ph_number = coll_1[2];
}

Thread Safety

All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

Windows Server 2003

See Also

Reference

BaseCollection Generic Class
BaseCollection Members
Microsoft.SpeechServer.Dialog Namespace