List<T>::Insert Method (Int32, T)
Inserts an element into the List<T> at the specified index.
Assembly: mscorlib (in mscorlib.dll)
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 null for reference types.
Implements
IList<T>::Insert(Int32, T)| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException |
List<T> accepts null 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.
The following example demonstrates how to add, remove, and insert a simple business object in a List<T>.
The following 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.
using namespace System; using namespace System::Collections::Generic; void main() { List<String^>^ dinosaurs = gcnew List<String^>(); Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity); dinosaurs->Add("Tyrannosaurus"); dinosaurs->Add("Amargasaurus"); dinosaurs->Add("Mamenchisaurus"); dinosaurs->Add("Deinonychus"); dinosaurs->Add("Compsognathus"); Console::WriteLine(); for each(String^ dinosaur in dinosaurs ) { Console::WriteLine(dinosaur); } Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity); Console::WriteLine("Count: {0}", dinosaurs->Count); Console::WriteLine("\nContains(\"Deinonychus\"): {0}", dinosaurs->Contains("Deinonychus")); Console::WriteLine("\nInsert(2, \"Compsognathus\")"); dinosaurs->Insert(2, "Compsognathus"); Console::WriteLine(); for each(String^ dinosaur in dinosaurs ) { Console::WriteLine(dinosaur); } Console::WriteLine("\ndinosaurs[3]: {0}", dinosaurs[3]); Console::WriteLine("\nRemove(\"Compsognathus\")"); dinosaurs->Remove("Compsognathus"); Console::WriteLine(); for each(String^ dinosaur in dinosaurs ) { Console::WriteLine(dinosaur); } dinosaurs->TrimExcess(); Console::WriteLine("\nTrimExcess()"); Console::WriteLine("Capacity: {0}", dinosaurs->Capacity); Console::WriteLine("Count: {0}", dinosaurs->Count); dinosaurs->Clear(); Console::WriteLine("\nClear()"); Console::WriteLine("Capacity: {0}", dinosaurs->Capacity); Console::WriteLine("Count: {0}", dinosaurs->Count); } /* 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 */
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1