
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Insert at the specified index in StringCollection in C#
To insert at the specified index in StringCollection, the code is as follows −
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); } } }
Output
This will produce the following output −
StringCollection elements... Accessories Books Electronics StringCollection elements...UPDATED Accessories Books Headphone Electronics
Example
Let us now see another 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...UPDATED"); foreach (string res in strCol){ Console.WriteLine(res); } } }
Output
This will produce the following output −
StringCollection elements... Laptop Desktop Tablet Speakers StringCollection elements...UPDATED Laptop Desktop Headphone Alienware Tablet Speakers StringCollection elements...UPDATED Laptop Desktop Headphone Alienware E-Book Reader Monitor HDD SSD Tablet Speakers
- Related Articles
- Copy StringCollection at the specified index of array in C#
- Get or Set at specified index in StringCollection in C#
- Gets or sets the element at the specified index in StringCollection in C#
- Remove from the specified index of the StringCollection in C#
- Insert an element into Collection at specified index in C#
- Insert an element into the ArrayList at the specified index in C#
- Insert into OrderedDictionary with key and value at specified index in C#
- Golang Program to insert an element into the array at the specified index
- Insert the specified element at the specified position in Java CopyOnWriteArrayList
- How to insert the elements of a collection into the List at the specified index in C#?
- Check if the specified string is in the StringCollection in C#
- Copy StringDictionary to Array at the specified index in C#
- Remove the entry at specified index from OrderedDictionary in C#
- Index of first occurrence in StringCollection in C#?
- Remove element at specified index of Collection in C#

Advertisements