
- 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 an element into Collection at specified index in C#
To insert an element into Collection at the specified index, the code is as follows −
Example
using System; using System.Collections.ObjectModel; public class Demo { public static void Main(){ Collection<string> col = new Collection<string>(); col.Add("Laptop"); col.Add("Desktop"); col.Add("Notebook"); col.Add("Ultrabook"); col.Add("Tablet"); col.Add("Headphone"); col.Add("Speaker"); Console.WriteLine("Elements in Collection..."); foreach(string str in col){ Console.WriteLine(str); } Console.WriteLine("Element at index 3 = " + col[3]); Console.WriteLine("Element at index 4 = " + col[4]); col.Insert(5, "Alienware"); Console.WriteLine("Elements in Collection...UPDATED"); foreach(string str in col){ Console.WriteLine(str); } } }
Output
This will produce the following output −
Elements in Collection... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 3 = Ultrabook Element at index 4 = Tablet Elements in Collection...UPDATED Laptop Desktop Notebook Ultrabook Tablet Alienware Headphone Speaker
Example
Let us now see another example −
using System; using System.Collections.ObjectModel; public class Demo { public static void Main(){ Collection<string> col = new Collection<string>(); col.Add("Andy"); col.Add("Kevin"); col.Add("John"); col.Add("Kevin"); col.Add("Mary"); col.Add("Katie"); col.Add("Barry"); col.Add("Nathan"); Console.WriteLine("Elements in Collection..."); foreach(string str in col){ Console.WriteLine(str); } col.Insert(3, "Jacob"); Console.WriteLine("Elements in Collection...UPDATED"); foreach(string str in col){ Console.WriteLine(str); } } }
Output
This will produce the following output −
Elements in Collection... Andy Kevin John Kevin Mary Katie Barry Nathan Elements in Collection...UPDATED Andy Kevin John Jacob Kevin Mary Katie Barry Nathan
- Related Articles
- Insert an element into the ArrayList at the specified index in C#
- Golang Program to insert an element into the array at the specified index
- Remove element at specified index of Collection in C#
- How to insert the elements of a collection into the List at the specified index in C#?
- Get or set the element at specified index in Collection in C#
- Insert into OrderedDictionary with key and value at specified index in C#
- Insert at the specified index in StringCollection in C#
- Insert the specified element at the specified position in Java CopyOnWriteArrayList
- Insert all elements of other Collection to Specified Index of Java ArrayList
- Replace an element at a specified index of the Vector in Java
- Java Program to insert all elements of other Collection to specified Index of ArrayList
- Swift Program to insert multiple elements into the array from the specified index
- Golang Program to insert multiple elements into the array from the specified index
- Insert a specified element in a specified position in JavaScript?
- Add an element to specified index of ArrayList in Java

Advertisements