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
How to insert an item in a list at a given position in C#?
To insert an item in an already created List, use the Insert() method. This method allows you to insert an element at a specific index position within the list, shifting all subsequent elements to the right.
Syntax
Following is the syntax for the Insert() method −
list.Insert(index, item);
Parameters
-
index − The zero-based index at which the item should be inserted.
-
item − The object to insert into the list.
Using Insert() Method
Firstly, create a list and add elements −
List<int> list = new List<int>(); list.Add(989); list.Add(345); list.Add(654); list.Add(876); list.Add(234); list.Add(909);
Now, to insert an item at the 4th position (index 3), use the Insert() method −
// inserting element at index 3 (4th position) list.Insert(3, 567);
Example
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
List<int> list = new List<int>();
list.Add(989);
list.Add(345);
list.Add(654);
list.Add(876);
list.Add(234);
list.Add(909);
Console.WriteLine("Count: {0}", list.Count);
Console.Write("List: ");
foreach(int i in list) {
Console.Write(i + " ");
}
// inserting element at 4th position (index 3)
list.Insert(3, 567);
Console.Write("\nList after inserting a new element: ");
foreach(int i in list) {
Console.Write(i + " ");
}
Console.WriteLine("\nCount: {0}", list.Count);
}
}
}
The output of the above code is −
Count: 6 List: 989 345 654 876 234 909 List after inserting a new element: 989 345 654 567 876 234 909 Count: 7
Insert Multiple Items at Different Positions
Example
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<string> fruits = new List<string> {"Apple", "Banana", "Orange"};
Console.WriteLine("Original list:");
foreach(string fruit in fruits) {
Console.Write(fruit + " ");
}
// Insert at the beginning (index 0)
fruits.Insert(0, "Mango");
// Insert at the end (current count)
fruits.Insert(fruits.Count, "Grapes");
// Insert in the middle (index 2)
fruits.Insert(2, "Cherry");
Console.WriteLine("\nAfter multiple insertions:");
foreach(string fruit in fruits) {
Console.Write(fruit + " ");
}
}
}
The output of the above code is −
Original list: Apple Banana Orange After multiple insertions: Mango Apple Cherry Banana Orange Grapes
Key Points
-
The
Insert()method uses zero-based indexing, so index 0 is the first position. -
Elements at and after the insertion index are shifted to the right.
-
The list's Count property increases by 1 after insertion.
-
You can insert at index 0 (beginning) or at the current count (end).
Conclusion
The Insert() method provides a convenient way to add elements at specific positions in a C# List. It automatically handles shifting existing elements and updating the list size, making it easy to maintain ordered collections.
