Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Insert at the specified index in StringCollection in C#
The StringCollection class in C# provides the Insert() method to add elements at specific positions within the collection. This method shifts existing elements to the right to make room for the new element at the specified index.
Syntax
Following is the syntax for the Insert() method −
stringCollection.Insert(index, value);
Parameters
index − The zero-based index at which to insert the element.
value − The string value to insert into the collection.
Using Insert() Method
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Accessories");
strCol.Add("Books");
strCol.Add("Electronics");
Console.WriteLine("StringCollection elements...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
Console.WriteLine("StringCollection elements...UPDATED");
foreach (string res in strCol) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
StringCollection elements... Accessories Books Electronics StringCollection elements...UPDATED Accessories Books Headphone Electronics
Multiple Insertions at Different Positions
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Laptop");
strCol.Add("Desktop");
strCol.Add("Tablet");
strCol.Add("Speakers");
Console.WriteLine("StringCollection elements...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
strCol.Insert(3, "Alienware");
Console.WriteLine("StringCollection elements...UPDATED");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(4, "E-Book Reader");
strCol.Insert(5, "Monitor");
strCol.Insert(6, "HDD");
strCol.Insert(7, "SSD");
Console.WriteLine("StringCollection elements...FINAL UPDATE");
foreach (string res in strCol) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
StringCollection elements... Laptop Desktop Tablet Speakers StringCollection elements...UPDATED Laptop Desktop Headphone Alienware Tablet Speakers StringCollection elements...FINAL UPDATE Laptop Desktop Headphone Alienware E-Book Reader Monitor HDD SSD Tablet Speakers
Insert at Beginning and End
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Middle Item 1");
strCol.Add("Middle Item 2");
Console.WriteLine("Original collection:");
foreach (string res in strCol) {
Console.WriteLine(res);
}
// Insert at beginning (index 0)
strCol.Insert(0, "First Item");
// Insert at end (current count)
strCol.Insert(strCol.Count, "Last Item");
Console.WriteLine("After inserting at beginning and end:");
foreach (string res in strCol) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Original collection: Middle Item 1 Middle Item 2 After inserting at beginning and end: First Item Middle Item 1 Middle Item 2 Last Item
How It Works
When you call Insert() on a StringCollection, the method shifts all elements at and after the specified index one position to the right. The new element is then placed at the specified index position. This operation maintains the order of existing elements while accommodating the new insertion.
Conclusion
The Insert() method in StringCollection allows precise control over element placement by inserting strings at specific index positions. It automatically shifts existing elements to accommodate new insertions, making it useful for maintaining ordered collections where position matters.
